本文介绍了Matplotlib-当我切换到半对数图时数据消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用pyplot和matplotlib绘制基本的半对数图,y轴为对数刻度.我正在使用以下代码:

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?

谢谢!
彼得

推荐答案

该问题提到了半对数图存在问题,但代码说明了纯对数图.无论如何,绘图或绘图部分消失的常见问题是由于数据点具有零或负值-因为这些数据点无法使用实数表示,因此从绘图中将其省略.但是可以通过使用matplotlib的 symlog 量表,它使用阈值 linthreshy 改变对数和线性量表,例如:

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.

这篇关于Matplotlib-当我切换到半对数图时数据消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 09:01