Skip to content

Instantly share code, notes, and snippets.

@alisatl
Last active June 3, 2020 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alisatl/dff8d92ca08dc04375ca9183ec7f1d5c to your computer and use it in GitHub Desktop.
Save alisatl/dff8d92ca08dc04375ca9183ec7f1d5c to your computer and use it in GitHub Desktop.
Bar plot ordered by categories in Pandas
import pandas as pd
judgements = ['strong', 'attribute mismatch', 'weak/related', 'unrelated', 'unjudged']
for key, report in reports.items():
report['judgement'] = pd.Categorical(report['judgement'], categories=judgements, ordered=True)
%matplotlib inline
import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
fig, axes = plt.subplots(3, 1, figsize=(11, 11))
axes[0].bar(judgements, reports['Nov2019_baseln']['judgement'].value_counts().sort_index(), label='Nov2019_baseln', alpha=0.8, width=0.4)
axes[0].bar(judgements, reports['Nov2019_exp']['judgement'].value_counts().sort_index(), label='Nov2019_exp', alpha=0.7, align='edge', width=0.4)
axes[0].set_title('Nov2019')
axes[0].legend()
axes[1].bar(judgements, reports['Feb2020_baseln']['judgement'].value_counts().sort_index(), label='Feb2020_baseln', alpha=0.8, width=0.4)
axes[1].bar(judgements, reports['Feb2020_exp']['judgement'].value_counts().sort_index(), label='Feb2020_exp', alpha=0.7, align='edge', width=0.4)
axes[1].set_title('Feb2020')
axes[1].legend()
axes[2].bar(judgements, reports['Mar2020_baseln']['judgement'].value_counts().sort_index(), label='Mar2020_baseln', alpha=0.8, width=0.4)
axes[2].bar(judgements, reports['Mar2020_exp']['judgement'].value_counts().sort_index(), label='Mar2020_exp', alpha=0.7, align='edge', width=0.4)
axes[2].set_title('Mar2020')
axes[2].legend()
for i in range(len(axes)):
axes[i].set_ylim([0, 3500])
#axes[i].xaxis.set_major_locator(mdates.DayLocator(interval=2))
# Format x-tick labels as 3-letter month name and day number
#axes[i].xaxis.set_major_formatter(mdates.DateFormatter('%b %d'));
#axes[i].xaxis.set_tick_params(rotation=45)
fig.suptitle('Model comparison', fontsize=16)
fig.tight_layout(pad=3.0)
fig.savefig('./figures/analysis.png', dpi=300)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment