2017-07-17 170 views
1

目的:要使用for循環生成100個barplots,並顯示輸出作爲副區圖像用for循環生成多個圖;在matplotlib副區顯示輸出

數據格式:數據文件與101列。最後一列是X變量;剩餘的100列是Y變量,對其繪製x。

期望輸出:Barplots在5×20陣列的插曲,如在本實施例中圖像:Subplot - 3 x 2 array example

當前方法:我在seaborn,其產生的nx 1陣列被使用PairGrid:example output

where input == dataframe; input3 ==從哪列列表被調用的列表:

for i in input3: 
    plt.figure(i) 
    g = sns.PairGrid(input, 
      x_vars=["key_variable"], 
      y_vars=i, 
      aspect=.75, size=3.5) 
    g.map(sns.barplot, palette="pastel") 

有沒有人有任何想法如何解決這個問題?

回答

0

您可以嘗試使用matplotlob的subplots來創建繪圖網格並將軸傳遞給barplot。軸分度,你可以做使用嵌套循環...

0

要給出瞭如何在20×5子繪圖網格圖100列數據幀的例子:

import matplotlib.pyplot as plt 
import pandas as pd 
import numpy as np 

data = np.random.rand(3,101) 
data[:,0] = np.arange(2,7,2) 
df = pd.DataFrame(data) 

fig, axes = plt.subplots(nrows=5, ncols=20, figsize=(21,9), sharex=True, sharey=True) 
for i, ax in enumerate(axes.flatten()): 
    ax.bar(df.iloc[:,0], df.iloc[:,i+1]) 
    ax.set_xticks(df.iloc[:,0]) 

plt.show() 

enter image description here