python - 带上标的 matplotlib 刻度轴符号

标签 python matplotlib

我想绘制一些关于频率的图。我想要一个带有上标符号的 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/

相关文章:

python - Matplotlib - Tcl_AsyncDelete : async handler deleted by the wrong thread?

python - for循环中的Matplotlib动画?

python - 按类别和点大小划分的 Pandas 散点图

python - 构建Python包时,有没有办法在源代码分发中包含文件/文件夹,而不是轮子

python - 为什么我的 os.rename 不起作用?

python - 在群图之上绘制误差线

python - 为单个系列 pandas 堆叠条形图

python - 本地使用 Z3Py

python - 按文件名日期对日期进行排序

python - "TypeError: Image data can not convert to float"在 python27 中使用 matplotlib