python - 如何制作 x 坐标字母?

标签 python matplotlib

我用 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/

相关文章:

python - 如何在另一个 python 图中添加不同的图(作为插图)

python - Matplotlib:等高线图的数据三次插值(或 FIT)

python - 如何在 Jupyter Notebook 中模拟用户输入?

python 将 split 和 join 组合成 1 行代码

python - 获取深度自动编码器的解码器

python - TensorFlow:是否可以使用 for 循环将函数映射到数据集?

python - 读取一个 matplotlib 图中的两个数据集

python - 大小为 2GB 的数组在减法和除法运算时会引发内存错误

python - 删除 pandas plot.area 的抗锯齿

python - 使用每月数据控制 matplotlib 中的条形宽度