带有上标的matplotlib刻度轴表示法 [英] matplotlib tick axis notation with superscript
问题描述
我想在频率上绘制一些图.我想有一个带有上标符号的x轴,例如此处. 另外,我还需要带有垂直注释的垂直线,分别将千赫和兆赫兹分开.
I would like to produce some plot over the frequencies. I want to have an x-axis with superscript notation like in here. In addition I need vertical lines with vertical annotation separate kilo and mega Hz.
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 ,但无法计算弄清楚如何设置它.我尝试遵循一些示例,但出现了类似AttributeError: 'Figure' object has no attribute 'ticklabel_format'
的错误
已经花了很多时间,不知道如何前进.
I tried use ticker but can't figure it out how to set up it. I tried to follow some examples but got error like AttributeError: 'Figure' object has no attribute 'ticklabel_format'
Already spend quite a lot of time on it and don't know how to move forward.
与plt.xscale('log')
相比,通常我需要以类似的方式设置x轴的格式,但是我想保持线性比例.
In general I need the x-axis formatted in the similar way, than if plt.xscale('log')
but I want to keep linear scale.
推荐答案
您可以将刻度线定义为字符串并为其分配这些标记:
You can define the tick-marks as strings and assign those:
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()
这篇关于带有上标的matplotlib刻度轴表示法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!