Skip to content

Instantly share code, notes, and snippets.

@audhiaprilliant
Created November 11, 2022 17:04
Show Gist options
  • Save audhiaprilliant/ea18433b701003538564e46d13f3888a to your computer and use it in GitHub Desktop.
Save audhiaprilliant/ea18433b701003538564e46d13f3888a to your computer and use it in GitHub Desktop.
Matplotlib 102 - Basic Introduction to Multiplot, Subplot and Gridspec
# ---------- SUBPLOT WITH GRIDSPEC ----------
# Figure size
fig = plt.figure(figsize = (10, 8))
# Create a grid for plots (2 rows & 3 columns)
gs = gridspec.GridSpec(nrows = 2, ncols = 3, figure = fig)
ax1 = fig.add_subplot(gs[0, :]);
ax1.set_title('# Customer by payment method');
ax2 = fig.add_subplot(gs[1, 0]);
ax2.set_title('# Customer by gender');
ax3 = fig.add_subplot(gs[1, 1]);
ax3.set_title('# Customer by paperless bill status');
ax4 = fig.add_subplot(gs[1, 2]);
ax4.set_title('# Customer by churn status');
# Bar plot - 1
ax1.bar(
x = 'PaymentMethod',
height = 'customerID',
data = df_group_1,
color = 'orange'
);
# Bar plot - 2
ax2.bar(
x = 'gender',
height = 'customerID',
data = df_group_2,
color = 'red'
);
# Bar plot - 3
ax3.bar(
x = 'PaperlessBilling',
height = 'customerID',
data = df_group_4,
color = 'green'
);
# Bar plot - 4
ax4.bar(
x = 'Churn',
height = 'customerID',
data = df_group_5,
color = 'purple'
);
# Remove the overlaps between the axis names and the titles
plt.tight_layout();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment