Pandas / Matplotlib: Faceting Bar Plots
Solution 1:
Second example here: http://pandas-docs.github.io/pandas-docs-travis/visualization.html#bar-plots
Anyway, you can always do that by hand, as you did yourself.
EDIT: BTW, you can always use rpy2 in python, so you can do all the same things as in R.
Also, have a look at this: https://pandas.pydata.org/pandas-docs/version/0.14.1/rplot.html I am not sure, but it should be helpful for creating plots over many panels, though might require further reading.
Solution 2:
@tcasell suggested the bar
call in the loop. Here is a working, if not elegant, example.
## second try--facet by county
N = 100
industry = ['a','b','c']
city = ['x','y','z']
ind = np.random.choice(industry, N)
cty = np.random.choice(city, N)
jobs = np.random.randint(low=1,high=250,size=N)
df_city =pd.DataFrame({'industry':ind,'city':cty,'jobs':jobs})
## how many panels do we need?
cols =df_city.city.value_counts().shape[0]
fig, axes = plt.subplots(1, cols, figsize=(8, 8))
for x, city inenumerate(df_city.city.value_counts().index.values):
data = df_city[(df_city['city'] == city)]
data = data.groupby(['industry']).jobs.sum()
print (data)
printtype(data.index)
left= [k[0] for k inenumerate(data)]
right= [k[1] for k inenumerate(data)]
axes[x].bar(left,right,label="%s" % (city))
axes[x].set_xticks(left, minor=False)
axes[x].set_xticklabels(data.index.values)
axes[x].legend(loc='best')
axes[x].grid(True)
fig.suptitle('Employment By Industry By City', fontsize=20)
Solution 3:
The Seaborn library, which is built on Matplotlib and could be considered a superset of it, has flexible and powerful plotting options for facet plots--they even use similar terminology to R. Scroll down on this page for multiple examples.
Post a Comment for "Pandas / Matplotlib: Faceting Bar Plots"