![](/img/trans.png)
[英]How to plot specific rows and columns of pandas dataframe (based on name of row and name of column) in bar plot with error bars?
[英]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.