簡體   English   中英

如何 pandas dataframe 的 plot 特定列?

[英]How to plot specific column of pandas dataframe?

不幸的是它不起作用:我有一個名為df的 dataframe

這包括 5 列和 100 行。

我想 plot 在 x 軸第 0 列(時間)和 y 軸上對應的值。

我試過了:

figure, ax1 = plt.subplots()
ax1.plot(df.columns[0],df.columns[1],linewidth=0.5,zorder=1, label = "Force1")
ax1.plot(df.columns[0],df.columns[2],linewidth=0.5,zorder=1, label = "Force2")

但這不起作用。

我不能直接處理列名 - 我只能使用列的編號(如 1、2 或數字 3)。

謝謝你的幫助!!!

赫爾穆特

您可以使用.iloc[]和列 position 或通過.columns作為參數傳遞:

figure, ax1 = plt.subplots()
ax1.plot(df[df.columns[0]],df[df.columns[1]],linewidth=0.5,zorder=1, label = "Force1")
ax1.plot(df[df.columns[0]],df[df.columns[2]],linewidth=0.5,zorder=1, label = "Force2")

或使用.iloc[]

figure, ax1 = plt.subplots()
ax1.plot(df.iloc[:,0],df.iloc[:,1],linewidth=0.5,zorder=1, label = "Force1")
ax1.plot(df.iloc[:,0],df.iloc[:,2],linewidth=0.5,zorder=1, label = "Force2")

或者定義列名稱列表,然后傳遞其索引(與第一種方法相同):

cols = df.columns
figure, ax1 = plt.subplots()
ax1.plot(df[cols[0]],df[cols[1]],linewidth=0.5,zorder=1, label = "Force1")
ax1.plot(df[cols[0]],df[cols[2]],linewidth=0.5,zorder=1, label = "Force2")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM