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

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

问题描述

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

我一直在阅读文档,但无法弄清楚语法.我知道在情节参数中它可能很简单,例如 'scale=linear',但我似乎无法正确

示例程序:

导入pylab导入 matplotlib.pyplot 作为 plta = [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()

解决方案

您可以使用

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

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

解决方案

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

这篇关于在 python 中使用 matplotlib 绘制对数轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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