绘制对数轴
我想使用 matplotlib 绘制一张带有一个对数轴的图。
示例程序:
import matplotlib.pyplot as plt
a = [pow(10, i) for i in range(10)] # exponential
fig = plt.figure()
ax = fig.add_subplot(2, 1, 1)
line, = ax.plot(a, color='blue', lw=2)
plt.show()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以使用
Axes.set_yscale
< /a> 方法。 这允许您在创建 Axes 对象后更改比例。 这还允许您构建一个控件,让用户在需要时选择比例。要添加的相关行是:
您可以使用
'线性'
切换回线性比例。 您的代码如下所示:You can use the
Axes.set_yscale
method. That allows you to change the scale after theAxes
object is created. That would also allow you to build a control to let the user pick the scale if you needed to.The relevant line to add is:
You can use
'linear'
to switch back to a linear scale. Here's what your code would look like:首先,混合
pylab
和pyplot
代码不太整洁。 更重要的是, pyplot 风格优于使用pylab。这是一个稍微清理过的代码,仅使用 pyplot 函数:
相关函数是
pyplot.yscale()
。 如果您使用面向对象的版本,请将其替换为方法 <代码>Axes.set_yscale()。 请记住,您还可以使用pyplot 更改 X 轴的比例.xscale()
(或 <代码>Axes.set_xscale())。检查我的问题“log”和“symlog”之间有什么区别? 查看 matplotlib 提供的图形比例的一些示例。
First of all, it's not very tidy to mix
pylab
andpyplot
code. What's more, pyplot style is preferred over using pylab.Here is a slightly cleaned up code, using only
pyplot
functions:The relevant function is
pyplot.yscale()
. If you use the object-oriented version, replace it by the methodAxes.set_yscale()
. Remember that you can also change the scale of X axis, usingpyplot.xscale()
(orAxes.set_xscale()
).Check my question What is the difference between ‘log’ and ‘symlog’? to see a few examples of the graph scales that matplotlib offers.
如果你想改变对数的底数,只需添加:
在 Matplotlib 3.3 之前,你必须使用 basex/basey 作为 log 的底数
if you want to change the base of logarithm, just add:
Before Matplotlib 3.3, you would have to use basex/basey as the bases of log
您只需使用 semilogy 而不是绘图:
You simply need to use semilogy instead of plot:
我知道这有点偏离主题,因为一些评论提到 ax.set_yscale('log') 是“最好的”解决方案,我认为可能需要反驳。 我不建议使用
ax.set_yscale('log')
来绘制直方图和条形图。 在我的版本(0.99.1.1)中,我遇到了一些渲染问题 - 不确定这个问题有多普遍。 然而 bar 和 hist 都有可选参数来将 y 尺度设置为日志,这可以正常工作。参考:
http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.bar
< a href="http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.hist">http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.hist
I know this is slightly off-topic, since some comments mentioned the
ax.set_yscale('log')
to be "nicest" solution I thought a rebuttal could be due. I would not recommend usingax.set_yscale('log')
for histograms and bar plots. In my version (0.99.1.1) i run into some rendering problems - not sure how general this issue is. However both bar and hist has optional arguments to set the y-scale to log, which work fine.references:
http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.bar
http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.hist
因此,如果您只是使用不复杂的 API,就像我经常使用的那样(我在 ipython 中经常使用它),那么这只是
希望这可以帮助那些寻找简单答案的人! :)。
So if you are simply using the unsophisticated API, like I often am (I use it in ipython a lot), then this is simply
Hope this helps someone looking for a simple answer! :).
此页面上给出了一些方法( semilogx,符号学,loglog)但它们都在幕后做同样的事情,即调用
set_xscale('log')
(对于 x 轴)和set_yscale('log')
(对于y 轴)。 此外,plt.yscale
/plt.scale
是状态机中的函数,它们调用set_yscale
/set_xscale 当前 Axes 对象。 即使对于条形图(以及直方图,因为它们只是条形图),
log=True
参数根据条形方向调用set_yscale('log')
/set_xscale('log')
。因此,无论您使用哪一个,它们最终都会调用相同的方法。 顺便说一下,除了能够选择日志的基础之外,您还可以在同一函数调用中设置次要刻度位置(使用
subs
kwarg)。There are a few methods given on this page (semilogx, semilogy, loglog) but they all do the same thing under the hood, which is to call
set_xscale('log')
(for x-axis) andset_yscale('log')
(for y-axis). Moreover,plt.yscale
/plt.scale
are functions in the state-machine, which make calls toset_yscale
/set_xscale
on the current Axes objects. Even for bar-charts (and histograms too since they are just bar-charts), thelog=True
parameter makes calls toset_yscale('log')
/set_xscale('log')
depending on the bar orientation.So it doesn't matter which one you use, they all end up calling the same method anyway. By the way, on top of being able to choose the base of the log, you can also set minor tick locations in the same function call (using
subs
kwarg).