2017-06-09 11 views
0

私はマルチパネル棒グラフを各パネルごとに異なるカテゴリで、Pythonで作成したいと思います。以下では、Rでggplot2を使ってどのように実行できるかの例を示します。同等の処理を行うことができるPythonのアプローチを探しています。これまでのところ私はpython-ggplotとseabornとbase matplotlibでこれを行うのに苦労しました。python - パネルごとに異なるカテゴリのマルチパネル棒グラフを作成する

あなたは私の関連記事で、この時に、以前の試みを見ることができます:私は探していますプロットのようなものを作るために、すべてのどのような方法がある場合は、この記事で using facet_wrap with categorical variables that differ between facet panes

は、私が今求めています(単に特定のアプローチを働かせようとするのではなく)

OK:Rの例:

animal = c('sheep', 'sheep', 'cow', 'cow', 'horse', 'horse', 'horse') 
attribute = c('standard', 'woolly', 'brown', 'spotted', 'red', 'brown', 'grey') 
population = c(12, 2, 7, 3, 2, 4, 5) 
animalCounts = data.frame(animal,attribute,population) 

ggplot(aes(x = attribute, weight = population), data = animalCounts) + geom_bar() + 
facet_wrap(~animal, scales = "free") + scale_y_continuous (limits= c(0,12)) 

barchart that I would like to make in python

は、私はPythonで同様のデータフレームを作成することができます

animal = pd.Series(['sheep', 'sheep', 'cow', 'cow', 'horse', 'horse', 'horse'], dtype = 'category') 
attribute = pd.Series(['standard', 'woolly', 'brown', 'spotted', 'red', 'brown', 'grey'], dtype = 'category') 
population = pd.Series([12, 2, 7, 3, 2, 4, 5]) 
animalCounts = pd.DataFrame({'animal' : animal, 'attribute' : attribute, 'population': population}) 

の高いだろうPythonで同等の数値を取得して任意のヘルプ感謝。私がrpy2を使う必要がなければ、想像上のボーナスポイント。

答えて

1

すでにlast questionに記載されている問題のため、python ggplotはfacet_wrapを使用できません。

したがって、標準のpandas/matplotlib技術を使用することはオプションになります。

import matplotlib.pyplot as plt 
import pandas as pd 

animal = pd.Series(['sheep', 'sheep', 'cow', 'cow', 'horse', 'horse', 'horse'], dtype = 'category') 
attribute = pd.Series(['standard', 'woolly', 'brown', 'spotted', 'red', 'brown', 'grey'], dtype = 'category') 
population = pd.Series([12, 2, 7, 3, 2, 4, 5]) 
df = pd.DataFrame({'animal' : animal, 'attribute' : attribute, 'population': population}) 

fig, axes = plt.subplots(ncols=3) 
for i, (name, group) in enumerate(df.groupby("animal")): 
    axes[i].set_title(name) 
    group.plot(kind="bar", x = "attribute", y="population", ax=axes[i], legend=False) 
    axes[i].set_ylabel("count") 
    axes[i].set_xlabel("") 

axes[1].set_xlabel("attribute")  
plt.tight_layout() 
plt.show() 

enter image description here

関連する問題