我用 pyplot/matplotlib 在 python 中制作了一个折线图:
import matplotlib.pyplot as plt
import math
import numpy as np
alphabet = range(0, 25)
firstLine = [letter + 65 for letter in alphabet]
secondLine = [letter + 97 for letter in alphabet]
plt.plot(alphabet, firstLine, '-b', label='ASCII value of capital.')
plt.plot(alphabet, secondLine, '--g', label='ASCII value of lowercase.')
plt.xlabel('Letter in Alphabet')
plt.ylabel('ASCII Value')
plt.title('ASCII value vs. Letter')
plt.legend()
plt.show()
在我的 x 轴上,它当前按数字缩放。但是,我希望 x 轴上的增量用字母 (a、b、c、d) 来标记,而不是说 0、5、10...具体来说,我希望字母“a”映射到 0,' b' 映射到 1,等等。
我如何让 pyplot 做到这一点?
最佳答案
使用xticks
函数。如果你执行 pyplot.xticks([0, 1, 2, 3], ['a', 'b', 'c', 'd'])
那么它将在 0 处有轴标记, 1, 2, 和 3,它们将被标记为 a、b、c 和 d。您还可以使用 np.arange
快速创建您想要的数字范围。
关于python - 如何制作 x 坐标字母?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12595174/