[pandas/matplotlib bar chart] #python #plot #matplotlib #pandas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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