所以我的想法是在底部創建類似的標簽,它是“第 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 mathimport matplotlib.pyplot as pltimport matplotlib.patches as mpatchesfig, 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()非常感激!
1 回答

阿波羅的戰車
TA貢獻1862條經驗 獲得超6個贊
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'
添加回答
舉報
0/150
提交
取消