簡體   English   中英

matplotlib與熊貓的多個情節

[英]matplotlib multiple plots with pandas

我目前正在嘗試開發一個便捷函數,該函數應該為pandas數據框中的每一列創建一個基本圖,其中包含數據框中所有列的值及其數量。

def plot_value_counts(df, leave_out):
  # is supposed to create the subplots grid where I can add the plots
  fig, axs = plt.subplots(int(len(df)/2) + 1,int(len(df)/2) + 1)
  for idx, name in enumerate(list(df)):
    if name == leave_out:
      continue
    else:
      axs[idx] = df[name].value_counts().plot(kind="bar")
  return fig, axs

該代碼段永遠運行,並且永不停止。 我嘗試查看關於stackoverflow的其他類似問題,但是找不到適合我情況的特定內容。

subplots函數的用法來自以下問題: 是否可以在matplotlib中自動生成多個子圖?

下面是數據文件的簡短示例,以便每個人都可以理解該問題: https : //gist.github.com/hentschelpatrick/e0a7e1400a4b5c356ec8b0e4952f8cc1#file-train-csv

您可以在plot方法docs中傳遞axis對象。 並且您應該迭代列:

fig, axs = plt.subplots(int(len(df)/2) + 1,int(len(df)/2) + 1)
for idx, name in enumerate(df.columns):
    if name == leave_out:
        continue
    else:
        df[name].value_counts().plot(kind="bar", ax=axs[idx])

編輯 :如果您有內存問題(似乎不運行),請首先嘗試不使用子圖,並show每個圖:

for idx, name in enumerate(df.columns):
    if name == leave_out:
        continue
    else:
        df[name].value_counts().plot(kind="bar")
        plt.show()

這是我為我的項目編寫的函數,用於繪制pandas數據框中的所有列。 它將生成一個大小為nx4的網格,並將繪制所有列

def plotAllFeatures(dfData):
    plt.figure(1, figsize=(20,50))
    pos=1
    for feature in dfData.columns:
        plt.subplot(np.ceil(len(dfData.columns)/4),4,pos)
        dfData[feature].plot(title=feature)
        pos=pos+1
    plt.show()

暫無
暫無

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

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