在python中使用matplotlib绘制对数坐标轴

时间:2022-04-17 23:43:59

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

我想用一个对数轴来画一个图。

I've been reading the docs, but can't figure out the syntax. I know that it's probably something simple like 'scale=linear' in the plot arguments, but I can't seem to get it right

我一直在看文档,但搞不懂语法。我知道它可能是一些简单的东西,比如" scale=linear "在情节论证中,但我似乎搞不清楚

Sample program:

示例程序:

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)
pylab.show()

6 个解决方案

#1


271  

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.

你可以用坐标轴。set_yscale方法。这允许您在创建坐标轴对象之后更改比例。这也允许你建立一个控件,让用户选择你需要的规模。

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()

在python中使用matplotlib绘制对数坐标轴

#2


234  

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

首先,把pylab代码和pyplot代码混合起来不是很整洁。此外,pyplot风格比使用pylab更受欢迎。

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

这里有一个稍微清理过的代码,只使用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()

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()).

相关函数是pyplot.yscale()。如果您使用的是面向对象的版本,可以用方法ax .set_yscale()替换它。记住,还可以使用pyplot.xscale()(或ax .set_xscale())更改X轴的比例。

Check my question What is the difference between ‘log’ and ‘symlog’? to see a few examples of the graph scales that matplotlib offers.

检查我的问题“log”和“symlog”的区别是什么?要查看一些图缩放的例子,matplotlib提供。

#3


55  

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()

#4


8  

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.

我知道这有点偏离主题,因为一些评论提到了ax.set_yscale('log')是最好的解决方案,我认为应该对此进行反驳。我不建议对直方图和条形图使用ax.set_yscale('log')。在我的版本(0.99.1.1)中,我遇到了一些呈现问题——不确定这个问题有多普遍。但是bar和hist都有可选参数来将y标度设置为log,这很好。

references: http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.bar

引用:http://matplotlib.org/api/pyplot_api.html # matplotlib.pyplot.bar

http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.hist

http://matplotlib.org/api/pyplot_api.html matplotlib.pyplot.hist

#5


6  

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

如果你想改变对数的底,只要加上:

plt.yscale('log',basey=2) 
# where basex or basey are the bases of log

#6


4  

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

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

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

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

希望这能帮助人们寻找一个简单的答案!:)。

#1


271  

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.

你可以用坐标轴。set_yscale方法。这允许您在创建坐标轴对象之后更改比例。这也允许你建立一个控件,让用户选择你需要的规模。

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()

在python中使用matplotlib绘制对数坐标轴

#2


234  

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

首先,把pylab代码和pyplot代码混合起来不是很整洁。此外,pyplot风格比使用pylab更受欢迎。

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

这里有一个稍微清理过的代码,只使用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()

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()).

相关函数是pyplot.yscale()。如果您使用的是面向对象的版本,可以用方法ax .set_yscale()替换它。记住,还可以使用pyplot.xscale()(或ax .set_xscale())更改X轴的比例。

Check my question What is the difference between ‘log’ and ‘symlog’? to see a few examples of the graph scales that matplotlib offers.

检查我的问题“log”和“symlog”的区别是什么?要查看一些图缩放的例子,matplotlib提供。

#3


55  

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()

#4


8  

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.

我知道这有点偏离主题,因为一些评论提到了ax.set_yscale('log')是最好的解决方案,我认为应该对此进行反驳。我不建议对直方图和条形图使用ax.set_yscale('log')。在我的版本(0.99.1.1)中,我遇到了一些呈现问题——不确定这个问题有多普遍。但是bar和hist都有可选参数来将y标度设置为log,这很好。

references: http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.bar

引用:http://matplotlib.org/api/pyplot_api.html # matplotlib.pyplot.bar

http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.hist

http://matplotlib.org/api/pyplot_api.html matplotlib.pyplot.hist

#5


6  

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

如果你想改变对数的底,只要加上:

plt.yscale('log',basey=2) 
# where basex or basey are the bases of log

#6


4  

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

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

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

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

希望这能帮助人们寻找一个简单的答案!:)。