Lesson 33 of 60 · python
Customizing Plots: Styles, Labels, Legends, and Themes
Duration: 20 minutes
Customizing Your Visuals
A polished plot conveys information more effectively.
Changing style globally
plt.style.use('seaborn-darkgrid') # or 'ggplot', 'fivethirtyeight'
Adding titles and axis labels
plt.plot(x, y)
plt.title('My Fancy Plot', fontsize=14, fontweight='bold')
plt.xlabel('X‑axis label', fontsize=12)
plt.ylabel('Y‑axis label', fontsize=12)
Adding a legend
plt.plot(x, y, label='Series 1')
plt.plot(x, y2, label='Series 2')
plt.legend(loc='upper left', fontsize=10, frameon=True)
Customizing ticks
plt.xticks(ticks=[0, 0.5, 1.0], labels=['Zero', 'Half', 'One'])
plt.yticks(rotation=45)
Adding annotations
plt.annotate('Peak', xy=(2, 9), xytext=(3, 12),
arrowprops=dict(facecolor='black', shrink=0.05))
Using a color map for continuous data
plt.scatter(x, y, c=y, cmap='viridis')
plt.colorbar(label='Intensity')
Exporting at high resolution
plt.savefig('my_plot.pdf', format='pdf', dpi=300)