绘制对数轴
我想使用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
对象之后更改比例。这也将使您能够构建一个控件,让用户在需要时选择秤。要添加的相关行是:
您可以使用
'Linarear'
切换到线性比例。这是您的代码的外观: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
函数:相关函数为
pyplot.yscale()
。如果使用面向对象的版本,请用方法 axes.set_yscale() 。请记住,您还可以使用 pyplot, 。代码> 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之前,您必须将基本/碱基作为日志基础
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
您只需要使用
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/pyplot_api.html#matplotlib.pleplot.pplot.ppleot.barbar
http://matplotlib.org/api/pyplot_api.htplot_api.html#matpllib.plotlib.pleplot.plot.plot.plot.plot.tplot.tplot.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
因此,如果您只是像我经常使用的那样(我经常在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
/code>/set_xscale 在当前轴对象上。即使对于bar-charts(也是直方图,也是因为它们只是bar-charts ), 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).