Overview
This lecture introduces professional plotting in Python using Matplotlib, covering basic line plots, customization, histograms, advanced subplots, 2D/3D plots, stream and vector fields, image reading, and creating animations.
Getting Started with Matplotlib
- Import Matplotlib (usually as
plt
) and Numpy for plotting.
- Use the
scienceplots
extension (plt.style.use('science', 'notebook', 'grid')
) for professional plot styling.
- Basic line plot:
plt.plot(x, y)
.
Plot Customization Basics
- Change line style (e.g.,
'--'
for dashed, 'o'
for dots).
- Adjust color with the
color
argument (e.g., 'purple'
).
- Modify line width and marker size with
linewidth
and ms
.
- Set plot shape and size with
plt.figure(figsize=(x, y))
.
Essential Plot Elements
- Always label axes with
plt.xlabel()
and plt.ylabel()
.
- Add legends for multiple data series with
plt.legend()
, and customize legend location and font size.
- Overlay multiple datasets to compare data with theoretical models.
Histograms & Density Plots
- Create histograms with
plt.hist(data)
.
- Adjust bin counts via the
bins
parameter.
- Normalize with
density=True
for comparing datasets of different sizes.
- Use
histtype='step'
for overlaid line-only histograms.
Advanced Plot Layouts
- Use
fig, ax = plt.subplots(rows, cols, figsize=(x, y))
for multiple plots.
- Access specific axes with indexing (e.g.,
axes[0,1]
).
- Set labels and titles per subplot using
ax.set_xlabel()
, ax.set_ylabel()
, and ax.set_title()
.
- Add text to plots with
ax.text(x, y, 'text', transform=ax.transAxes)
.
Fine-tuning Appearance
- Adjust tick label sizes with
ax.tick_params(labelsize=number)
.
- Add bounding boxes to text with the
bbox
argument.
- Customize colors, legend appearance, and subplot spacing.
2D Color and Contour Plots
- Use
plt.contourf(x, y, z)
for filled 2D plots; control granularity with levels
.
- Add colorbars (
plt.colorbar()
) with labels for interpretation.
- Change color maps via the
cmap
argument (e.g., 'plasma'
).
- Use
plt.contour()
for lines of constant value, and label contours with plt.clabel()
.
3D and Advanced Visualization
- Create 3D plots with
projection='3d'
in plt.subplots()
.
- Plot surfaces with
ax.plot_surface(x, y, z, cmap='coolwarm')
.
- 3D plots are best as animations to show rotation.
Stream and Vector Field Plots
- Plot vector fields using
ax.streamplot(x, y, u, v)
.
- Visualize magnitude with color (
color=speed
) or variable line width (linewidth=lw
).
- Show specific flow lines by setting seed points.
Images and Animations
- Read and display images with
plt.imread()
and plt.imshow()
.
- Create animations using
matplotlib.animation
and save as GIFs.
- Set up animation by initializing empty plot/artists, updating them in an animate function, and saving with
anim.save()
.
Saving and Exporting Figures
- Save figures with
plt.savefig('filename.png', dpi=200)
for high quality.
Key Terms & Definitions
- Matplotlib โ Python library for creating static, animated, and interactive plots.
- Histogram โ A plot showing the distribution of a dataset.
- Density Plot โ Histogram normalized so the area sums to one.
- Contour Plot โ Lines or filled regions representing constant values in 2D data.
- Stream Plot โ Visualization of vector fields showing flow lines.
- Subplot โ Multiple axes within one figure for organized visualizations.
- Animation โ Sequence of frames showing dynamic changes in a plot.
Action Items / Next Steps
- Practice customizing basic plots using different styles, colors, and markers.
- Create and overlay histograms with varying bin sizes and normalization.
- Explore subplots and add annotations or text.
- Experiment with 2D color maps, contour, and stream plots.
- Try creating and saving an animated plot or GIF using Matplotlibโs animation module.