简体   繁体   English

matplotlib刻度轴记号和上标

[英]matplotlib tick axis notation with superscript

I would like to produce some plot over the frequencies. 我想绘制一些频率图。 I want to have an x-axis with superscript notation like in here . 我想在x轴上加上上标符号,例如此处 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()

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. 我尝试遵循一些示例,但遇到诸如AttributeError: 'Figure' object has no attribute 'ticklabel_format'错误AttributeError: 'Figure' object has no attribute 'ticklabel_format'已经花了很多时间并且不知道如何前进。

In general I need the x-axis formatted in the similar way, than if plt.xscale('log') but I want to keep linear scale. 通常,与plt.xscale('log') ,我需要以类似的方式设置x轴的格式,但我想保持线性比例。

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

I'm just shooting in the dark, but looks like xlabel takes a variety of options: 我只是在黑暗中拍摄,但看起来xlabel需要多种选择:

http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.xlabel http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.xlabel

When I went to: 当我去:

http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.text http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.text

I noticed that there is a verticalalignment option. 我注意到有一个verticalalignment选项。 Maybe that's what you need? 也许这就是您所需要的?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM