Skip to content

Instantly share code, notes, and snippets.

@Ze1598
Last active March 13, 2023 19:02
Show Gist options
  • Save Ze1598/23f98d9082d36f57da4fecb97ca64d54 to your computer and use it in GitHub Desktop.
Save Ze1598/23f98d9082d36f57da4fecb97ca64d54 to your computer and use it in GitHub Desktop.
freeCodeCamp grouped bar chart visualization: data visualization
# Pivot the DF so that there's a column for each month, each row\
# represents a year, and the cells have the mean page views for the\
# respective year and month
df_pivot = pd.pivot_table(
df,
values="page_views",
index="year",
columns="month",
aggfunc=np.mean
)
# Plot a bar chart using the DF
ax = df_pivot.plot(kind="bar")
# Get a Matplotlib figure from the axes object for formatting purposes
fig = ax.get_figure()
# Change the plot dimensions (width, height)
fig.set_size_inches(7, 6)
# Change the axes labels
ax.set_xlabel("Years")
ax.set_ylabel("Average Page Views")
# Use this to show the plot in a new window
# plt.show()
# Export the plot as a PNG file
fig.savefig("page_views_barplot.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment