Skip to content

Instantly share code, notes, and snippets.

@joonro
Last active June 9, 2021 16:08
Show Gist options
  • Save joonro/f3cce66500c65a7376774a78d316fd00 to your computer and use it in GitHub Desktop.
Save joonro/f3cce66500c65a7376774a78d316fd00 to your computer and use it in GitHub Desktop.
[pandas/matplotlib bar chart] #python #plot #matplotlib #pandas
# 2021-06-09 This is older way
import matplotlib.pyplot as plt
def plot_bar_chart_interaction(
means_var1_low,
means_var1_high,
var1_name="",
var2_name="",
title="",
ylabel="",
):
num_groups = 2
# create plot
fig, ax = plt.subplots(figsize=(10, 5))
index = np.arange(num_groups)
bar_width = 0.35
opacity = 0.8
rects1 = plt.bar(
index, means_var1_low, bar_width,
alpha=opacity,
label='Low {}'.format(var1_name)
)
rects2 = plt.bar(
index + bar_width, means_var1_high, bar_width,
alpha=opacity,
label='High {}'.format(var1_name)
)
plt.title(title)
plt.xlabel('')
plt.ylabel(ylabel)
plt.xticks(
list(index + bar_width / 2),
('Low {}'.format(var2_name), 'High {}'.format(var2_name))
)
plt.legend()
plt.tight_layout()
plt.show()
var_name = 'counter_accepted_status'
ax = des_stats.loc[var_name, 'mean'].plot.bar(figsize=(8,4), color=['tab:blue', 'tab:red'])
ax.set_title(var_name, fontsize=15)
ylim = (0, des_stats.loc[var_name, 'mean'].max() * 1.2)
ax.set_ylim(*ylim)
x_offset = -0.1
y_offset = 0.01 * ylim[1]
for p in ax.patches:
b = p.get_bbox()
val = "{:.2f}".format(b.y1 + b.y0)
ax.annotate(val, ((b.x0 + b.x1)/2 + x_offset, b.y1 + y_offset), fontsize=15)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment