【发布时间】:2013-05-13 18:49:04
【问题描述】:
我想在频率上绘制一些图。我想要一个带有上标符号的 x 轴,例如 here。 此外,我需要带有垂直注释的垂直线,将千赫兹和兆赫兹分开。
import numpy as np
import matplotlib.pyplot as plt
band = np.linspace(0,10**12,100)
y = band
plt.plot(band,y)
plt.xlabel('Frequencies')
plt.vlines(10**3, min(y), max(y),colors = 'black', label = 'kilo Hz')
plt.vlines(10**6, min(y), max(y),colors = 'black', label = 'mega Hz')
plt.legend()
plt.show()
我尝试使用ticker,但不知道如何设置它。我尝试关注some 的示例,但遇到了AttributeError: 'Figure' object has no attribute 'ticklabel_format' 之类的错误
已经花了不少时间了,不知道怎么往前走。
一般来说,我需要以类似的方式格式化 x 轴,而不是 plt.xscale('log'),但我想保持线性比例。
【问题讨论】:
-
你能展示使用不起作用的代码吗?
-
另外,使用
axvline而不是vlines作为垂直线。 -
我尝试将
matplotlib.ticker.Formatter与不同的配置一起使用,并且尝试了很多不再存在的行。对于set_major_formatter,我也会收到类似object has no attribute的错误 -
只是一个想法;如果您要使用 linear 比例的 x 轴跨越值(至少)从 103 到 106,则 105 和 106 将是 104 和 105 之间距离的 10 倍,以及 103 和 104 之间距离的 100 倍 (!!)。在您链接到的示例中,x 轴 是 实际上是对数刻度。
-
但是您尝试
set_major_formatter的对象是什么?这是axis(不是axes)对象的方法。例如:yaxis = ax.get_yaxis()
标签: python matplotlib