Last active
March 13, 2023 19:02
-
-
Save Ze1598/23f98d9082d36f57da4fecb97ca64d54 to your computer and use it in GitHub Desktop.
freeCodeCamp grouped bar chart visualization: data visualization
This file contains hidden or 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
# 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