所以我的想法是在底部创建类似标签的标签,即“第1天,第2天,第3天...”,我想也许可以在一定范围内做到这一点,而不必编写单独的标签,但是我尝试了单独的标签方法并指出错误(参见图1),整个代码位于(图2)
它引起的错误(图1)
Traceback (most recent call last):
File "C:/Users/willi/AppData/Local/Programs/Python/Python38-32/kjhj.py", line 20, in <module>
labels[6] = 'Day 5'
IndexError: list assignment index out of range
代码如下:(图2)
from numpy import *
import math
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
fig, ax = plt.subplots()
a = [20, 40, 60, 78, 90, 120, 220, 300, 400]
b = [40, 80, 90, 200, 400, 500, 600, 780, 900]
orange_patch = mpatches.Patch(color='orange', label='Orange Label')
plt.legend(handles=[orange_patch])
fig.canvas.draw()
labels = [item.get_text() for item in ax.get_xticklabels()]
labels[1] = 'Day 0'
labels[2] = 'Day 1'
labels[3] = 'Day 2'
labels[4] = 'Day 3'
labels[5] = 'Day 4'
labels[6] = 'Day 5'
ax.set_xticklabels(labels)
plt.plot(a)
plt.plot(b)
plt.show()
非常感谢!
答案 0 :(得分:1)
Python中的索引从0开始,而不是1。 如果您这样替换代码,它应该可以工作:
labels[0] = 'Day 0'
labels[1] = 'Day 1'
labels[2] = 'Day 2'
labels[3] = 'Day 3'
labels[4] = 'Day 4'
labels[5] = 'Day 5'