1
votes

I am ploting the same values in python and in MATLAB ( The data is exactly the same).

just doing

import scipy.io as sio
zzz = sio.loadmat(pathToData)
plot(zzz['zzz'][:,0]) #python

In matlab

plot(zzz)        %matlab

I am trying to get matplotlib to create an image that looks the same as MATLAB figure, like this example.

  • The following images where created with screen capture so to avoid any post processing of the image. I made sure both of the figures are in the same size. then I put one next to the other and created a screencapture of them both at the same time

I also put the lines one next to the other: enter image description here

enter image description here

It is a bit hard to see but the MATLAB image is much more noisy and it looks like the python image is after a moving average of something like this ( especially in the start of plot)

Do you have any advice of how to make the python figure look exactly like the MATLAB one?

( I uploaded the data if anyone interested in some experiments )

EDIT

I tried to do antialiased = False, the results are a bit better, but the linewidth is bigger no matter how small I try to do it (original screencapture )

enter image description here

1
Could you include the full code (including loading of mat file into python)? I am sure many experienced python programmers would otherwise skip this question because they have no idea how to deal with a matlab specific file format.Daniel
Assuming, you have stated, the pieces of data ( == zzz ) are the same, so the data-processing-phase is the point where to start from. However, if the zzz inputs are not the same for MATLAB / python processing, check rather the data-generation-phase for any difference(s). You may also plot difference between the pair of zzz-datapoints ( one from MATLAB the other from python data-source ) to get more important details, better readable than from observing just the zoomed-in pixelised-GUI representations )user3666197
Could it be that the python line is just thicker?Dev-iL
Uhm... How do you superimpose the two figures?! Is there any scaling involved? And do you compare screenshots or figures saved to disk?hitzg
Only half joking: why bother? Anti-aliased pictures usually look better, so why are you trying to make a nice Matplotlib graph as ugly as a Matlab one? A more interesting question would be how to get Matlab to use anti-aliasing by default :)Bas Swinckels

1 Answers

3
votes

I think this is due to the fact that matplotlib uses anti aliasing when producing the figure. However, this is an artist property which you can set:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 2*np.pi, 500)

plt.plot(x, np.sin(x), label='with anti-aliasing')
plt.plot(x, np.cos(x), antialiased=False, label='without anti-aliasing')
plt.legend()
plt.show()

Result:

enter image description here

Zoom on top left corner:

enter image description here

So by switching off the anti-aliasing filter you might get very similar results to the Matlab version.