Matplotlib - Data disappears when I switch to a se

2019-07-04 01:25发布

问题:

I am trying to plot do a basic semilog plot using pyplot and matplotlib, with the y-axis being the logarithmic scale. I am using the following code:

pylab.figure(num=None,figsize=(8,6))  
pylab.plot(x_loc,var1,x_loc,var2)  
\#pylab.yscale('log')  
pylab.xlabel('$y/L_{1/2}$',fontsize=18)  
pylab.ylabel('$n/n_{max}$',fontsize=18)  
pylab.title('Particle Concentration vs. Position',fontsize=18)  
pylab.show() 

This gives me a fine linear plot with the third line commented as above, but when I uncomment this line and rerun, the data disappears from the plot. The plot window has the correct limits, but there is no data anymore.

Has anyone come across this problem before?

Thanks!
Peter

回答1:

By using set_yscale('log') you can change that property of the yscale when an object has already been created. Give it a try and see if it fixes your problem. The docs for this are here: http://matplotlib.sourceforge.net/api/axes_api.html#matplotlib.axes.Axes.set%5Fyscale

also, try changing your render. do a:

from matplotlib import use
use('TkAgg')
import pylab

(only import pylab after the use command)



回答2:

The question mentions that there are issues with a semilog plot but the code illustrates a plain log plot. In any case the usual problem with plots or parts of plot disappearing is due to the data points having zero or negative values - as these are not representable using real numbers so they are omitted from the plots. But it is possible to get matplotlib to plot these values by using its symlog scale, which combines log and linear scales using a threshold linthreshy at which they change e.g.:

 pylab.yscale('symlog',linthreshy=1)

For a more detailed explanation of the difference between symlog and log plots see this answer.



回答3:

It looks like it's a bug in the EPDLab package I am using (from Enthought). If I run this command from IPython or the terminal (python 'filename'), the plot is output correctly.