import matplotlib.pyplot as plt
import pandas as pd
# 示例数据
x = [1, 2, 3, 4, 5]
y = pd.DataFrame({
'Data1': [2, 4, 6, 8, 10],
'Data2': [3, 6, 9, 12, 15],
'Data3': [1, 3, 5, 7, 9]
})
# 绘制图形
fig, ax = plt.subplots()
lines = ax.plot(x, y)
labels = y.columns.tolist()
ax.legend(lines, labels)
plt.show()輸出結果:

lines = ax.plot(x, y)

df = pd.DataFrame(y, index=x)
y的原index= 0~4
要改為x (1~5)
沒有完全match
原本index = 0的那一row被刪除了
多了index = 5的那一row,內容為NaN

ax=df.plot()

自動加上欄標籤當legend
y.index=x
這方法才不會刪除
index不match的row 資料

推薦hahow線上學習python: https://igrape.net/30afN
![Python陣列介紹:List[ ], Tuple( ), Set{ }, Dictionary{ }, for迴圈 Python陣列介紹:List[ ], Tuple( ), Set{ }, Dictionary{ }, for迴圈](https://i2.wp.com/savingking.com.tw/wp-content/uploads/2022/01/1643329597-856443d20256b1a850089a56d07657bf.png?quality=90&zoom=2&ssl=1&resize=350%2C233)

![Python如何讀寫csv逗點分隔檔(每列內容為新光增有利現金流)?pandas.read_csv(r”路徑\檔名.副檔名”),如何移除list中的nan元素?math.isnan(),如何計算新光增有利IRR?numpy_financial(array) ;輸出csv檔時如何去掉index跟header?如何選擇要寫入的直欄columns? dfFinal.to_csv(fpath, index=False, header=None, columns=[0,1]) Python如何讀寫csv逗點分隔檔(每列內容為新光增有利現金流)?pandas.read_csv(r”路徑\檔名.副檔名”),如何移除list中的nan元素?math.isnan(),如何計算新光增有利IRR?numpy_financial(array) ;輸出csv檔時如何去掉index跟header?如何選擇要寫入的直欄columns? dfFinal.to_csv(fpath, index=False, header=None, columns=[0,1])](https://i2.wp.com/savingking.com.tw/wp-content/uploads/2022/11/20221110122900_3.png?quality=90&zoom=2&ssl=1&resize=350%2C233)







近期留言