用Matplotlib給子圖添加圖例時出錯ax.legend() TypeError zip argument #2 must support iteration

畫圖時添加圖例

import matplotlib.pyplot as plt
line_up, = plt.plot([1,2,3])
plt.legend(line_up, 'Up')
plt.show()

報錯zip argument #2 must support iteration
應該是legend的參數需要zip形式。
(for label,handle in zip (labels,handles)
所有改寫成下面形式就好了

import matplotlib.pyplot as plt
line_up, = plt.plot([1,2,3])
plt.legend((line_up,), ('Up',))
plt.show()

參考: lhttps://stackoverflow.com/questions/43326938/typeerror-zip-argument-2-must-support-iteration-with-matplotlib-pyplot-legend.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章