我想绘制一些关于频率的图。我想要一个带有上标符号的 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'
的错误
已经在上面花费了大量时间,但不知道如何继续。
一般来说,我需要以与 plt.xscale('log')
类似的方式格式化 x 轴,但我想保持线性比例。
最佳答案
您可以将刻度线定义为字符串并分配它们:
mport 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')
string_labels = []
for i in range(0,len(y),10):
string_labels.append(r"$10^{%02d}$" % (i/10.0))
plt.xticks(np.linspace(0,10**12,10),string_labels)
plt.legend()
plt.show()
关于python - 带上标的 matplotlib 刻度轴符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16529038/