如何制作在X轴上带有重复标签的图?

时间:2019-01-27 13:32:02

标签: python matplotlib

由于前面的代码,我有两个列表。 如何在X轴上使用“实验室”的字母(一个接一个)和列表val中的对应数字-在Y轴上绘制图?

我尝试过:

lab = ['a', 'b', 'c', 'a', 'b', 'c']
val = [1, 2, 4, 1, 2, 4]

from matplotlib import pyplot as plt

plt.bar(lab, val)

每个字母只绘制一次。
enter image description here 而且我希望它逐个字母地绘制整个序列。

1 个答案:

答案 0 :(得分:1)

您可以使用索引,然后只需设置xticks标签即可:

lab = ['G', 'U', 'U', 'U', 'U', 'U', 'C', 'A', 'U', 'U', 'U', 'R', 'G', 'C', 'N']
lab_x = [i for i in range(len(lab))]
val = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 4]

from matplotlib import pyplot as plt

plt.bar(lab_x, val)
plt.xticks(lab_x, lab)
相关问题