Install:
pip install matplotlib
Navigate the world of data visualization with this comprehensive Matplotlib cheatsheet, covering fundamental plotting, customization, and advanced techniques.
Install:
|
Standard Import:
|
Enable interactive plots (e.g., in Jupyter):
|
Check version:
|
Clearing the current figure/axes:
|
Figure: The top-level container for all plot elements. It holds one or more Axes. |
Axes: The area where the data is plotted. Each Axes has an x-axis, y-axis (and potentially z-axis), titles, labels, and tick marks. A Figure can contain multiple Axes (subplots). |
Axis: These are the number-line objects that control the limits of the graph (e.g., x-axis, y-axis). They contain ticks and ticklabels. |
Artist: Everything visible on the Figure is an Artist (Title, Labels, Lines, Text, etc.). Most Artists are tied to an Axes. |
Recommended Practice: Use the Object-Oriented (OO) interface where you explicitly create Figure and Axes objects, rather than relying solely on the state-based |
Simple Line Plot |
|
Plotting x vs y |
|
Plotting multiple lines |
|
Creating a new figure |
|
Showing the plot |
|
Adding grid |
|
Create Figure and one Axes |
|
Create Figure and multiple Axes (2x2 grid) |
|
Set size and DPI |
|
Plotting on an Axes object |
|
Adding a single Axes to an existing Figure |
|
Figure Title ( |
|
Axes Title ( |
|
X/Y Labels ( |
|
Figure Title (OO) |
|
Axes Title (OO) |
|
X/Y Labels (OO) |
|
Adding Legend ( |
|
Adding Legend (OO) |
|
Basic plot with customizations |
|
Short-hand format string |
|
Line Style options |
|
Marker Style options |
|
Color options |
|
Adjusting opacity (alpha) |
|
Basic Scatter Plot ( |
|
Scatter Plot with size and color variation ( |
|
Basic Scatter Plot (OO) |
|
Scatter Plot with size and color variation (OO) |
|
Adding markers/styles (similar to |
|
Basic Vertical Bar Chart ( |
|
Basic Horizontal Bar Chart ( |
|
Customizing Bars |
|
Basic Bar Chart (OO) |
|
Grouped Bar Chart Example |
|
Stacked Bar Chart Example |
|
Basic Histogram ( |
|
Customizing Bins & Density |
|
Basic Histogram (OO) |
|
Plotting multiple histograms |
|
Basic Box Plot ( |
|
Horizontal Box Plot |
|
Customizing Box Plots |
|
Basic Box Plot (OO) |
|
Basic Pie Chart ( |
|
Customizing Pie Chart |
|
Basic Pie Chart (OO) |
|
Displaying an image ( |
|
Displaying an array as image/heatmap ( |
|
Displaying an array as image/heatmap (OO) |
|
Setting aspect ratio |
|
Common colormaps ( |
|
Setting color by name or hex |
|
Setting marker style |
|
Setting linestyle |
|
Setting linewidth |
|
Setting opacity |
|
Using colormaps for line segments |
|
Set X/Y limits ( |
|
Set X/Y limits (OO) |
|
Set X/Y ticks ( |
|
Set X/Y ticks (OO) |
|
Set tick labels |
|
Logarithmic scale |
|
Adding minor ticks |
|
Add simple text ( |
|
Add simple text (OO) |
|
Add annotation with arrow ( |
|
Add annotation with arrow (OO) |
|
Text alignment ( |
|
Using TeX for text |
|
Using |
|
Using |
|
Using |
|
Sharing axes |
|
Adjusting layout to prevent overlap |
|
Adding space between subplots |
|
Creating a twin Y-axis ( |
|
Creating a twin X-axis (less common) |
|
Saving the current figure ( |
|
Saving a specific figure (OO) |
|
Specifying format (inferred from extension) |
Formats: |
Controlling DPI |
|
Transparent background |
|
Removing whitespace around plot |
|
The |
With OO, you work directly with |
|
Example comparison: pyplot:
OO:
The OO version is clearer when dealing with multiple subplots or more complex figures. |
Listing available styles |
|
Using a specific style |
|
Using multiple styles (layered) |
|
Applying a style temporarily |
|
Resetting style |
|
Importing modules |
|
Creating date data |
|
Basic date plot |
|
Formatting date ticks |
|
Setting date intervals for major/minor ticks |
|
Matplotlib uses a configuration file ( |
Location:
|
Edit this file (or create a copy in your current directory) to change defaults like figure size, font size, line width, colors, etc. |
Example entry in
|
You can also change settings programmatically for the current session using
|
Downsampling: Reduce the number of data points plotted, especially for line plots (e.g., only plot every Nth point or average points in bins). |
Using |
Hexbin Plots: Good for showing density of points in 2D. |
2D Histograms: Similar to hexbin, shows density in rectangular bins. |
Aggregating Data: Group data into bins and plot aggregated statistics (mean, count, etc.) as a bar chart or line plot. |
Using Libraries like Datashader: For extremely large datasets, libraries like Datashader can perform rasterization off-GPU before passing aggregated data to Matplotlib for rendering. |
Plots not showing: Ensure you call |
Plots overlapping: Use |
Labels/Titles too small/large: Adjust |
Incorrect date format: Use |
Legends not appearing: Ensure you add the |
Saving blank plots: Call |