绘制对数轴

发布于 2025-02-05 00:14:56 字数 290 浏览 3 评论 0 原文

我想使用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()

I want to plot a graph with one logarithmic axis using matplotlib.

Sample program:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

听闻余生 2025-02-12 00:14:56

您可以使用 方法。这使您可以在创建 axes 对象之后更改比例。这也将使您能够构建一个控件,让用户在需要时选择秤。

要添加的相关行是:

ax.set_yscale('log')

您可以使用'Linarear'切换到线性比例。这是您的代码的外观:

import pylab
import matplotlib.pyplot as plt
a = [pow(10, i) for i in range(10)]
fig = plt.figure()
ax = fig.add_subplot(2, 1, 1)

line, = ax.plot(a, color='blue', lw=2)

ax.set_yscale('log')

pylab.show()

“结果图”

You can use the Axes.set_yscale method. That allows you to change the scale after the Axes 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:

ax.set_yscale('log')

You can use 'linear' to switch back to a linear scale. Here's what your code would look like:

import pylab
import matplotlib.pyplot as plt
a = [pow(10, i) for i in range(10)]
fig = plt.figure()
ax = fig.add_subplot(2, 1, 1)

line, = ax.plot(a, color='blue', lw=2)

ax.set_yscale('log')

pylab.show()

result chart

拥抱影子 2025-02-12 00:14:56

首先,混合 pylab pyplot 代码不是很整洁。更重要的是,

这是一个稍有清洁的代码,仅使用 pyplot 函数:

from matplotlib import pyplot

a = [ pow(10,i) for i in range(10) ]

pyplot.subplot(2,1,1)
pyplot.plot(a, color='blue', lw=2)
pyplot.yscale('log')
pyplot.show()

相关函数为 pyplot.yscale() 。如果使用面向对象的版本,请用方法 axes.set_yscale() 。请记住,您还可以使用 pyplot, 。代码> axes.set_xscale() )。

检查我的问题'log'和symlog'之间有什么区别? 查看Matplotlib提供的图形尺度的一些示例。

First of all, it's not very tidy to mix pylab and pyplot code. What's more, pyplot style is preferred over using pylab.

Here is a slightly cleaned up code, using only pyplot functions:

from matplotlib import pyplot

a = [ pow(10,i) for i in range(10) ]

pyplot.subplot(2,1,1)
pyplot.plot(a, color='blue', lw=2)
pyplot.yscale('log')
pyplot.show()

The relevant function is pyplot.yscale(). If you use the object-oriented version, replace it by the method Axes.set_yscale(). Remember that you can also change the scale of X axis, using pyplot.xscale() (or Axes.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.

幸福丶如此 2025-02-12 00:14:56

如果要更改对数的基础,只需添加:

plt.yscale('log',base=2) 

在matplotlib 3.3之前,您必须将基本/碱基作为日志基础

if you want to change the base of logarithm, just add:

plt.yscale('log',base=2) 

Before Matplotlib 3.3, you would have to use basex/basey as the bases of log

贵在坚持 2025-02-12 00:14:56

您只需要使用

from pylab import *
import matplotlib.pyplot  as pyplot
a = [ pow(10,i) for i in range(10) ]
fig = pyplot.figure()
ax = fig.add_subplot(2,1,1)

line, = ax.semilogy(a, color='blue', lw=2)
show()

You simply need to use semilogy instead of plot:

from pylab import *
import matplotlib.pyplot  as pyplot
a = [ pow(10,i) for i in range(10) ]
fig = pyplot.figure()
ax = fig.add_subplot(2,1,1)

line, = ax.semilogy(a, color='blue', lw=2)
show()
若相惜即相离 2025-02-12 00:14:56

我知道这是有点偏离主题的,因为一些评论提到 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 using ax.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

最好是你 2025-02-12 00:14:56

因此,如果您只是像我经常使用的那样(我经常在ipython中使用它),那么这只是

yscale('log')
plot(...)

希望这可以帮助有人寻找简单的答案! :)。

So if you are simply using the unsophisticated API, like I often am (I use it in ipython a lot), then this is simply

yscale('log')
plot(...)

Hope this helps someone looking for a simple answer! :).

七禾 2025-02-12 00:14:56

此页面上有几种方法( 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)。

data = np.random.choice(np.logspace(-0.5, 1, base=20), 10)
plt.plot(data)
plt.yscale('log', base=10, subs=[10**x for x in (0.25, 0.5, 0.75)], nonpositive='mask')
#                          ^^^ <-- 3 equal-spaced minor ticks       ^^^^ mask invalid values

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) and set_yscale('log') (for y-axis). Moreover, plt.yscale/plt.scale are functions in the state-machine, which make calls to set_yscale/set_xscale on the current Axes objects. Even for bar-charts (and histograms too since they are just bar-charts), the log=True parameter makes calls to set_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).

data = np.random.choice(np.logspace(-0.5, 1, base=20), 10)
plt.plot(data)
plt.yscale('log', base=10, subs=[10**x for x in (0.25, 0.5, 0.75)], nonpositive='mask')
#                          ^^^ <-- 3 equal-spaced minor ticks       ^^^^ mask invalid values

result

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文