|
| 1 | +import matplotlib.pyplot as plt |
| 2 | +import pandas as pd |
| 3 | +import numpy as np |
| 4 | + |
| 5 | +N = 100 |
| 6 | +industry = ['a','b','c'] |
| 7 | +city = ['x','y','z'] |
| 8 | +ind = np.random.choice(industry, N) |
| 9 | +cty = np.random.choice(city, N) |
| 10 | +jobs = np.random.randint(low=1,high=250,size=N) |
| 11 | +df_city =pd.DataFrame({'industry':ind,'city':cty,'jobs':jobs}) |
| 12 | + |
| 13 | +## how many panels do we need? |
| 14 | +cols =df_city.city.value_counts().shape[0] |
| 15 | +fig, axes = plt.subplots(1, cols, figsize=(8, 8)) |
| 16 | + |
| 17 | +for x, city in enumerate(df_city.city.value_counts().index.values): |
| 18 | + data = df_city[(df_city['city'] == city)] |
| 19 | + data = data.groupby(['industry']).jobs.sum() |
| 20 | + print (data) |
| 21 | + print type(data.index) |
| 22 | + left= [k[0] for k in enumerate(data)] |
| 23 | + right= [k[1] for k in enumerate(data)] |
| 24 | + |
| 25 | + axes[x].bar(left,right,label="%s" % (city)) |
| 26 | + axes[x].set_xticks(left, minor=False) |
| 27 | + axes[x].set_xticklabels(data.index.values) |
| 28 | + |
| 29 | + axes[x].legend(loc='best') |
| 30 | + axes[x].grid(True) |
| 31 | + fig.suptitle('Employment By Industry By City', fontsize=20) |
| 32 | +plt.show(1) |
| 33 | + |
| 34 | +from ggplot import * |
| 35 | + |
| 36 | +print ggplot(aes(x='city'), data=df_city) + geom_bar() + facet_grid(x=None, y="city", nrow=1) |
0 commit comments