2016-09-21 94 views
1

我有一個熊貓數據框pandas_df與6輸入列:column_1, column_2, ... , column_6,和一個結果列result。現在我用下面的代碼繪製每兩個輸入列對的散點圖(所以我有6 * 5/2 = 15個數字)。我做了下面的代碼15次,每次都產生了一個大數字。matplotlib:繪製多個小數字在一個大的陰謀

我想知道是否有一種方法可以遍歷所有可能的列對,並將所有15位數字作爲小數字繪製在一個大圖中?謝謝!

%matplotlib notebook 
import matplotlib.pyplot as plt 
import matplotlib 
matplotlib.style.use('ggplot') 

pandas_df.plot(x='column_1', y='column_2', kind = 'scatter', c = 'result') 
+0

你有沒有通過'seaborn' [API]看着(https://stanford.edu/~mwaskom/software/seaborn/api.html)/ [圖庫](HTTPS:/ /stanford.edu/~mwaskom/software/seaborn/examples/index.html)? [PairGrid](https://stanford.edu/~mwaskom/software/seaborn/generated/seaborn.PairGrid.html#seaborn.PairGrid)特別看起來像你所描述的。 – lanery

回答

4

考慮數據框df

df = pd.DataFrame(np.random.rand(10, 6), columns=pd.Series(list('123456')).radd('C')) 
df 

enter image description here


解決方案
使用itertoolsmatplotlib.pyplot.subplots

from itertools import combinations 
import matplotlib.pyplot as plt 

pairs = list(combinations(df.columns, 2)) 

fig, axes = plt.subplots(len(pairs) // 3, 3, figsize=(15, 12)) 
for i, pair in enumerate(pairs): 
    d = df[list(pair)] 
    ax = axes[i // 3, i % 3] 
    d.plot.scatter(*pair, ax=ax) 

fig.tight_layout() 

enter image description here

+0

@piRSuared:在fig中,axes = plt.subplots(len(pair)// 3,3,figsize =(15,12)),其中是「paired」的定義?謝謝。 – Edamame

+0

@Edamame道歉,我更新了帖子 – piRSquared