Lesson 43 · Python for Data Science
Master Styling, Annotations & Saving Techniques for Python Data Visualization
Welcome! In this lesson, we will learn how to make beautiful plots using Python. We will explore how to style, label, annotate, and save plots so they are…
- CoursePython for Data Science
- Lesson43 of 38
- Video13 min
- FormatJupyter notebook · 18 code cells
What you'll learn
Data
No separate download needed — the notebook creates or downloads everything it uses.
📓 Full notebook
Download .ipynb
Styling, Annotations, and Saving Plots in Python#
Welcome! In this lesson, we will learn how to make beautiful plots using Python. We will explore how to style, label, annotate, and save plots so they are useful and easy to understand.
Plots help us see trends and tell stories with data. By learning how to style and save them, we can communicate our ideas clearly to others.
# First, let us import the library we need.
import matplotlib.pyplot as plt
Why Style Plots?#
Good styling makes a plot easy to read and understand. It helps your audience focus on what matters most.
We will cover colors, line styles, labels, titles, legends, and more.
# Let us plot a simple line.
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
plt.plot(x, y)
plt.show()
# Let us add color and line style.
plt.plot(x, y, color='red', linestyle='--')
plt.show()
# Now we will add markers at each data point.
plt.plot(x, y, marker='o', color='blue', linestyle='-')
plt.show()
Adding Titles and Labels#
Titles and labels tell others what your plot shows. Always label your axes so people know what the numbers mean.
# Let us add a title and axis labels.
plt.plot(x, y, color='green', marker='s', linestyle='--')
plt.title('Simple Square Numbers Plot')
plt.xlabel('Input Value')
plt.ylabel('Square of Value')
plt.show()
# Let us add a legend to explain our data.
plt.plot(x, y, color='purple', marker='o', label='y is x squared')
plt.legend()
plt.title('Plot with Legend')
plt.xlabel('x')
plt.ylabel('y')
plt.show()
Customizing Fonts and Sizes#
You can make your text bigger or choose different fonts for clarity. This is helpful for presentations or sharing plots online.
# Let us change the font size and style of labels.
plt.plot(x, y, color='orange', marker='^', label='x squared')
plt.title('Big Title', fontsize=20, fontname='Comic Sans MS')
plt.xlabel('Value', fontsize=14)
plt.ylabel('Squared Value', fontsize=14)
plt.legend(fontsize=12)
plt.show()
# Let us style the background and grid.
plt.style.use('ggplot')
plt.plot(x, y, color='blue', label='Nice Style')
plt.title('Styled with ggplot')
plt.grid(True)
plt.legend()
plt.show()
Annotating Points#
Annotations let you call attention to special points or trends in your plot. This adds notes directly to the graph so your viewers notice what is important.
# Let us annotate the highest point.
plt.plot(x, y, marker='o')
plt.title('With Annotation')
plt.xlabel('x')
plt.ylabel('y')
plt.annotate('Biggest value', xy=(x[-1], y[-1]), xytext=(3, 20),
arrowprops={'arrowstyle':'->', 'color':'red'}, color='red')
plt.show()
# Let us annotate multiple points with loops.
plt.plot(x, y, marker='*', color='brown')
for i, val in enumerate(x):
plt.annotate(f'{val},{y[i]}', xy=(val, y[i]), xytext=(val+0.1, y[i]+0.5))
plt.title('Labeling All Points')
plt.show()
# Let us save our plot as an image file.
plt.plot(x, y, color='magenta')
plt.title('Save This Plot!')
plt.xlabel('Input')
plt.ylabel('Output')
plt.savefig('my_first_plot.png')
plt.show()
Choosing File Types#
Use .png for most images. Use .pdf or .svg for high quality or printed work.
Always be sure to use a file extension matching your chosen format.
# Let us ask the user for a filename to save to.
filename = input('Enter a filename for your plot (for example: plot2.png): ')
plt.plot(x, y, color='teal')
plt.title('Custom Filename Example')
plt.savefig(filename)
plt.show()
# Let us save a plot without showing it, useful for scripts.
plt.plot(x, y, color='navy')
plt.title('Saved, Not Shown')
plt.savefig('no_show_plot.png')
plt.close()
Mini-Project: Daily Temperatures#
We will combine everything learned to make a nice plot of daily temperatures. We will use styling, labels, annotation, and save the result.
# Sample data for temperatures over a week.
days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
temps = [22, 21, 23, 25, 24, 20, 19]
plt.figure(figsize=(8,5))
plt.plot(days, temps, marker='o', color='tomato', linestyle='-', label='Temperature (C)')
plt.title('Daily High Temperatures', fontsize=16)
plt.xlabel('Day of Week')
plt.ylabel('Temperature (C)')
plt.grid(True)
plt.legend()
plt.annotate('Warmest', xy=('Thu',25), xytext=('Wed',26.5),
arrowprops={'arrowstyle':'->', 'color':'red'}, color='red')
plt.savefig('weekly_temps.png')
plt.show()
# Challenge: Label days colder than 21 degrees with a note.
plt.plot(days, temps, marker='o', color='navy', label='Temp')
for i, val in enumerate(temps):
if val < 21:
plt.annotate('Cold', xy=(days[i], val), xytext=(days[i], val-1))
plt.title('Colder Days Labeled')
plt.xlabel('Day')
plt.ylabel('Temp (C)')
plt.grid(True)
plt.legend()
plt.show()
Extra Tips#
- Use
plt.tight_layout()beforeshow()to automatically adjust spacing. - Call
plt.close()after saving many plots to avoid memory errors. - Save with
dpi=300for higher quality images. - Try
plt.rcParamsto set styles for all plots in one place.
# Let us check our knowledge: Can you add an annotation, customize style, and save?
# Try using a different dataset or styling option!
Recap#
- We learned how to style plots with different colors, lines, and markers.
- We annotated data points and highlighted trends.
- We saved our plots as images for sharing.
The more you practice, the more creative you can be!
Thank You for Watching!#
If you enjoyed this lesson, please:
- Like the video
- Leave a comment
- Subscribe for more tutorials
- Share with friends
Happy plotting!
Found this useful?
All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.



