用python中的matplotlib绘制对数轴 [英] Plot logarithmic axes with matplotlib in python

查看:140
本文介绍了用python中的matplotlib绘制对数轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用matplotlib绘制一个具有一个对数轴的图形.

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

我一直在阅读文档,但无法弄清楚语法.我知道在plot参数中可能像'scale=linear'一样简单,但是我似乎无法正确理解

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

示例程序:

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

推荐答案

您可以使用 Axes.set_yscale 方法.这样,您可以在创建Axes对象后更改比例.这样一来,您还可以构建一个控件,让用户根据需要选择比例.

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.

要添加的相关行是:

ax.set_yscale('log')

您可以使用'linear'切换回线性刻度.这是您的代码的样子:

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绘制对数轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆