Introduction
In this lab, we will explore the lifecycle of a plot using Matplotlib. We will start with raw data and end by saving a customized visualization. We will learn how to create a plot, control its style, customize its appearance, combine multiple visualizations, and save the plot to disk.
VM Tips
After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.
Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.
If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.
Import the necessary modules
First, we need to import the required modules: Matplotlib and NumPy.
import matplotlib.pyplot as plt
import numpy as np
Prepare the data
We will use a sample dataset that contains sales information for different companies. Here is an example of the data:
data = {'Barton LLC': 109438.50,
'Frami, Hills and Schmidt': 103569.59,
'Fritsch, Russel and Anderson': 112214.71,
'Jerde-Hilpert': 112591.43,
'Keeling LLC': 100934.30,
'Koepp Ltd': 103660.54,
'Kulas Inc': 137351.96,
'Trantow-Barrows': 123381.38,
'White-Trantow': 135841.99,
'Will LLC': 104437.60}
group_data = list(data.values())
group_names = list(data.keys())
group_mean = np.mean(group_data)
Create the plot
We will use the barplot visualization to represent the sales data. Follow these steps:
- Create a figure and an axis object using
plt.subplots().
fig, ax = plt.subplots()
- Plot the data using the
barh()method of the axis object.
ax.barh(group_names, group_data)
Customize the plot style
We can change the style of our plot to make it more visually appealing. Follow these steps:
- Print the list of available styles using
print(plt.style.available).
print(plt.style.available)
- Choose a style and apply it using
plt.style.use(style_name).
plt.style.use('fivethirtyeight')
- Let's show the plot again.
fig, ax = plt.subplots()
ax.barh(group_names, group_data)
Customize the plot appearance
We can further customize the appearance of our plot. Follow these steps:
- Rotate the x-axis labels to make them more readable.
labels = ax.get_xticklabels()
plt.setp(labels, rotation=45, horizontalalignment='right')
- Set the x-axis and y-axis limits, labels, and title.
ax.set(xlim=[-10000, 140000],
xlabel='Total Revenue',
ylabel='Company',
title='Company Revenue')
- Show the plot again.
fig, ax = plt.subplots()
ax.barh(group_names, group_data)
labels = ax.get_xticklabels()
plt.setp(labels, rotation=45, horizontalalignment='right')
ax.set(xlim=[-10000, 140000], xlabel='Total Revenue', ylabel='Company',
title='Company Revenue')
Combine multiple visualizations
We can add additional plot elements to our visualization. Follow these steps:
- Add a vertical line representing the mean of the sales data.
ax.axvline(group_mean, ls='--', color='r')
- Annotate new companies on the plot.
for group in [3, 5, 8]:
ax.text(145000, group, "New Company", fontsize=10, verticalalignment="center")
- Adjust the position of the plot title.
ax.title.set(y=1.05)
- The full code is shown below.
fig, ax = plt.subplots(figsize=(8, 8))
ax.barh(group_names, group_data)
labels = ax.get_xticklabels()
plt.setp(labels, rotation=45, horizontalalignment='right')
## Add a vertical line, here we set the style in the function call
ax.axvline(group_mean, ls='--', color='r')
## Annotate new companies
for group in [3, 5, 8]:
ax.text(145000, group, "New Company", fontsize=10,
verticalalignment="center")
## Now we move our title up since it's getting a little cramped
ax.title.set(y=1.05)
ax.set(xlim=[-10000, 140000], xlabel='Total Revenue', ylabel='Company',
title='Company Revenue')
plt.show()
Save the plot
Finally, we can save our plot to disk. Follow these steps:
- Print the supported file formats using
print(fig.canvas.get_supported_filetypes()).
print(fig.canvas.get_supported_filetypes())
- Save the figure as an image file using
fig.savefig(file_path, transparent=False, dpi=80, bbox_inches="tight"). Uncomment this line to save the figure.
fig.savefig('sales.png', transparent=False, dpi=80, bbox_inches="tight")
You can open the saved image file using the file explorer in the left sidebar.
Summary
In this lab, we learned about the lifecycle of a plot using Matplotlib. We started by creating a plot, controlling its style, customizing its appearance, combining multiple visualizations, and saving the plot to disk. Matplotlib offers a wide range of customization options to create visually appealing and informative plots.