问题描述
我目前正在使用logscale,以便有更大的可能性来绘制数据.但是,我的数据也包含零值.我知道这些零值在logscale上将不起作用,因为未定义log(0).
I am currently using logscale in order to have greater possibilities of plotting my data. Nevertheless, my data consists also of zero values. I know that these zero values will not work on logscale as log(0) is not defined.
例如,
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([0,1,2],[10,10,100],marker='o',linestyle='-')
ax.set_yscale('log')
ax.set_xscale('log')
完全忽略零值.这种行为可以接受吗?至少应该有某种警告.我只是偶然地认出了它.也许还有一种以对数刻度绘制零值数据的方法吗?
completely omits the zero value. Is this behavior acceptable? At least there should be some kind of warning. I only recognized it by accident. Is there maybe also a way of plotting zero value data in logscale?
谢谢!
P.S .:我希望这适合stackoverflow.我没有找到matplotlib的邮件列表.
P.S.: I hope this fits to stackoverflow. I did not find a mailing list of matplotlib.
推荐答案
为此目的,最简单的方法是使用符号"图.接近0的间隔是线性比例,因此可以显示0.
It's easiest to use a "symlog" plot for this purpose. The interval near 0 will be on a linear scale, so 0 can be displayed.
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([0,1,2],[10,10,100],marker='o',linestyle='-')
ax.set_yscale('symlog')
ax.set_xscale('symlog')
plt.show()
Symlog将一个小的间隔设置为零(上下左右)以使用线性刻度.这样一来,事情就可以越过0,而不会导致log(x)
爆炸(或者转到-inf).
Symlog sets a small interval near zero (both above and below) to use a linear scale. This allows things to cross 0 without causing log(x)
to explode (or go to -inf, rather).
这里有一个很好的视觉比较,可以作为一个SO答案: https://stackoverflow.com/a/3513150/325565
There's a nice visual comparison as an SO answer here: https://stackoverflow.com/a/3513150/325565
这篇关于matplotlib中具有零值的对数刻度图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!