Matplotlib - Data disappears when I switch to a semilog plot

左心房为你撑大大i 提交于 2019-12-12 14:03:49

问题


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.



来源:https://stackoverflow.com/questions/4027778/matplotlib-data-disappears-when-i-switch-to-a-semilog-plot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!