30 Quick Matplotlib Tips and Tricks for Beginners

Ayushi Trivedi 07 May, 2024
10 min read


The guide, ’30 Quick Matplotlib Tips and Tricks for Beginners,’ provides a comprehensive guide to enhance your data visualization skills. It covers various techniques like importing Matplotlib, creating simple plots, customizing line styles, adding annotations, and mastering complex visualizations like 3D and polar plots. The guide offers clear explanations and code examples, helping beginners and experts communicate insights and tell compelling stories with their data.

Matplotlib Tips

You can also enroll in our free python course today!

30 Quick Matplotlib Tips and Tricks for Beginners

Here are 30 quick Matplotlib tips and tricks for beginners to enhance their data visualization skills:

1. Import Matplotlib

In order to follow the typical convention among Matplotlib users, this tip only imports the Matplotlib library and aliases it as plt. This gives you access to the functions and classes of Matplotlib using the abbreviation plt.

import matplotlib.pyplot as plt

2. Simple Plot

A line plot is made with Matplotlib’s plt.plot() function. It displays the x-values and matching y-values on a graph after receiving two inputs.

plt.plot([1, 2, 3, 4], [1, 4, 9, 16])

This code snippet plots a basic line graph with x-values of [1, 2, 3, 4] and y-values of [1, 4, 9, 16] using Matplotlib, and then shows the plot.

3. Customizing Line Styles

Users can alter the appearance of lines in plots by applying different styles to them in matplotlib, such as solid, dashed, or dotted lines. Using the linestyle parameter in the plt.plot() function will accomplish this.

plt.plot([1, 2, 3, 4], [1, 4, 9, 16], linestyle='--')

This code sets the linestyle option in the plt.plot() function to ‘–‘ to create a line plot with dashed lines.

4. Adding Labels and Title

The label for the x-axis, which indicates the significance of the values plotted along the horizontal axis, is specified by the xlabel function. The label for the y-axis, representing the meaning of the values on the vertical axis, is set by the ylabel function.

5. Changing Marker Styles

The marker=’o’ option specifies the utilization of circular markers for every data point in the plot.

plt.plot([1, 2, 3, 4], [1, 4, 9, 16], marker='o')

This code plots a line graph with markers at each data point. The marker='o' argument specifies that circular markers should be used.

6. Setting Axis Limits

xlim() and ylim() functions are used to set the limits for the x-axis and y-axis, respectively, in a plot. They define the range of values displayed along each axis.

plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
plt.xlim(0, 5)
plt.ylim(0, 20)

This code sets the boundaries of the x-axis from 0 to 5 and the y-axis from 0 to 20 before displaying the plot.

7. Multiple Plots

To plot data on the corresponding axes, ax1 and ax2, use the functions ax1.plot() and ax2.plot(). They work similarly to plt.plot(), but they only work on the axes objects, giving you more precise control over how each subplot looks.

fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.plot([1, 2, 3, 4], [1, 4, 9, 16])
ax2.plot([1, 2, 3, 4], [1, 2, 3, 4])

This code creates a figure with two subplots arranged horizontally. Each subplot displays a line plot of different data.

8. Plotting Data Points

The scatter() function in Matplotlib creates a scatter plot, visually representing individual data points as markers at specified coordinates.

plt.scatter([1, 2, 3, 4], [1, 4, 9, 16])

This code snippet creates a scatter plot with points at coordinates (1, 1), (2, 4), (3, 9), and (4, 16), then displays the plot.

9. Adding Legends

The legend() function in Matplotlib adds a legend to a plot, providing information about the elements displayed and labeling each line with the specified labels, making it easier to distinguish between data series or categories.

plt.plot([1, 2, 3, 4], [1, 4, 9, 16], label='Line 1')
plt.plot([1, 2, 3, 4], [1, 2, 3, 4], label='Line 2')

In order to distinguish between the two lines, this code plots them and adds a legend. Once each line is labeled with a corresponding label parameter, plt.legend() displays the legend.

10. Customizing Legends

Matplotlib’s legend() function uses the loc parameter to specify the legend’s location on the plot, which can be a string or integer value, or a tuple of coordinates specifying the legend’s exact position.

plt.plot([1, 2, 3, 4], [1, 4, 9, 16], label='Line 1')
plt.plot([1, 2, 3, 4], [1, 2, 3, 4], label='Line 2')
plt.legend(loc='upper left', fontsize='small')

The legend() function in Matplotlib adds a legend to a plot, labeling lines with specified labels. The loc parameter determines the legend’s location, while fontsize sets the font size for legend labels.

11. Adding Text

plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
plt.text(2, 8, 'Sample Text', fontsize=12)

This code utilizes Matplotlib to create a line graph and annotates it with the text “Sample Text” positioned at the coordinates (2, 8) with a font size of 12.

12. Gridlines

The grid() function in Matplotlib allows users to toggle grid lines on or off in a plot, enabling them on both the x-axis and the y-axis, enhancing data visualization and interpretation.

plt.plot([1, 2, 3, 4], [1, 4, 9, 16])

This code produces a line plot with the points [1, 2, 3, 4] along the x-axis and [1, 4, 9, 16] along the y-axis. It adds grid lines to the plot using the plt.grid(True) command, which facilitates reading and interpreting the data.

13. Changing Figure Size

Matplotlib’s figsize() script sets the figure’s measurements in inches. It requires both width and height, given as a tuple (width, height). In this example, the plot figure’s width and height are set to 8 and 6 inches, respectively, using figsize=(8, 6).

plt.figure(figsize=(8, 6))
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])

This code snippet generates a plot with a custom figure size of 8 inches width and 6 inches height, displaying a line graph representing the relationship between the x-values [1, 2, 3, 4] and the corresponding y-values [1, 4, 9, 16].

14. Saving Figures

The savefig() function in Matplotlib enables the saving of the current figure to a specific file format like PNG, PDF, SVG, or JPEG.

plt.plot([1, 2, 3, 4], [1, 4, 9, 16])

The savefig() function in Matplotlib saves the current figure to a file, like “plot.png”, in the current working directory.

15. Histograms

The hist() function generates a histogram, representing data distribution, with the bins parameter specifying the number of intervals to divide the data into, like in this case, 5 bins.

data = [1, 1, 2, 2, 2, 3, 3, 4, 5]
plt.hist(data, bins=5)

This code creates a histogram of the data list, with bins divided into 5 intervals, displaying the frequency of occurrences for each interval.

16. Bar Plots

To make vertical bar charts, utilize Matplotlib’s bar() function. Two arrays or lists are required; one contains the categories (or labels) on the x-axis, and the other has the heights of the bars that correspond to those categories.

plt.bar(['A', 'B', 'C'], [10, 20, 30])

This code generates a bar chart with three bars labeled ‘A’, ‘B’, and ‘C’, where the heights of the bars are 10, 20, and 30 respectively.

17. Box Plots

A box plot is produced by Matplotlib’s boxplot() function to show the distribution of the supplied dataset. It shows the data’s quartiles, median, and outliers.

data = [1, 2, 3, 4, 5, 6, 7, 8, 9]

For the dataset’s data, the function generates a box plot. The box plot illustrates the data distribution by displaying the interquartile range (box), the median (line inside the box), and any outliers (points outside the whiskers).

18. Pie Charts

Matplotlib’s pie() function generates a pie chart where each slice represents a percentage of the total. The sizes list contains the size of each slice, while the labels list provides labels for each slice. The chart visually represents the distribution of data points relative to the total sum.

sizes = [10, 20, 30, 40]
labels = ['A', 'B', 'C', 'D']
plt.pie(sizes, labels=labels)

19. Adding Annotations

Annotations are added to a plot using Matplotlib’s annotate() function. You can use it to annotate text at particular plot points and, if desired, draw an arrow from the point to the annotation text. The function’s parameters consist of:

  • s: The text of the annotation.
  • xy: The coordinates of the point being annotated.
  • xytext: The coordinates of the text label.
  • arrowprops: A dictionary of properties specifying the style of the arrow, such as color, width, and style.
plt.scatter([1, 2, 3, 4], [1, 4, 9, 16])
plt.annotate('Point 3', xy=(3, 9), xytext=(3.5, 10),
             arrowprops=dict(facecolor='black', shrink=0.05))

With points at coordinates [1, 1], [2, 4], [3, 9], and [4, 16], this code generates a scatter plot. Next, at position (3, 9), it inserts an annotation named “Point 3” with the annotation text placed at (3.5, 10). The customized arrow connecting the annotation to the point is colored black and slightly shrunk.

20. Color Maps

Using a conventional normal distribution as a source, np.random.randn(1000) produces an array of 1000 random numbers, each of which represents a random sample.

In the hist() function:

  • x: The array of data to be plotted.
  • bins: The number of bins (intervals) to divide the data into. More bins result in a more detailed histogram.
  • cmap: The colormap to be used for coloring the bars in the histogram. It specifies the color scheme.
import numpy as np
x = np.random.randn(1000)
plt.hist(x, bins=30, cmap='cool')
  • np.random.randn(1000) generates an array of 1000 random numbers drawn from a standard normal distribution.
  • plt.hist(x, bins=30, cmap='cool') creates a histogram of the data x with 30 bins, and the cmap parameter sets the color map of the histogram to ‘cool’.
  • plt.show() displays the histogram.

21. Secondary Axes

Matplotlib’s twinx() function enables the creation of a twin Axes that shares the x-axis. This allows for displaying two separate plots with individual y-axes, while sharing the same x-axis on a single figure. When you wish to overlay plots with several y-scales for comparison, you frequently utilize it.

fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.plot([1, 2, 3, 4], [1, 4, 9, 16], 'g-')
ax2.plot([1, 2, 3, 4], [10, 20, 30, 40], 'b-')

This code creates a figure with two subplots that share the same x-axis but have different y-axes.

  • ax1 plots the green line with data points [(1,1), (2,4), (3,9), (4,16)].
  • ax2 plots the blue line with data points [(1,10), (2,20), (3,30), (4,40)].

The twinx() function creates a new y-axis on the right side of the figure for ax2.

22. Logarithmic Scale

The loglog() function in Matplotlib creates plots with logarithmic scales on both x and y axes, making them useful for visualizing data across multiple orders of magnitude. It compresses large ranges of values into a manageable display, highlighting trends and relationships that might not be easily visible on a linear scale plot.

plt.loglog([1, 10, 100], [1, 100, 10000])

Using the loglog() function in Matplotlib, we generate a plot with logarithmic scales on both the x and y axes, which is beneficial for visualizing data spanning multiple orders of magnitude. This functionality is showcased in the example where points [1, 10, 100] are plotted against [1, 100, 10000].

23. Error Bars

The errorbar() function in Matplotlib creates a plot with error bars, allowing visualization of uncertainties or variability in data points. It uses coordinates x and y, with errors representing the error or uncertainty associated with each y value, and the fmt parameter specifies the marker format.

x = [1, 2, 3, 4]
y = [1, 4, 9, 16]
errors = [0.2, 0.4, 0.6, 0.8]
plt.errorbar(x, y, yerr=errors, fmt='o')

The errorbar() function in Matplotlib creates a plot with error bars, plotting points defined by x and y lists with vertical error bars from the errors list, and adjusting the fmt=’o’ parameter to plot points as circles.

24. 3D Plots

Importing the mpl_toolkits.mplot3d module enables 3D plotting in Matplotlib. The subplot() function creates a subplot within the figure, with the projection set to ‘3d’.

from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter([1, 2, 3], [4, 5, 6], [7, 8, 9])

This code uses the mpl_toolkits.mplot3d module in Matplotlib to produce a 3D scatter plot. A figure is initialized initially, and then a subplot (ax) with a 3D projection is added. Next, it uses the scatter() method to plot the points (1, 4, 7), (2, 5, 8), and (3, 6, 9) in three dimensions. It presents the plot at the end.

25. Heatmaps

The imshow() function in Matplotlib is a tool that generates an image of a 2D array or matrix, corresponding to each element in the array as a pixel in the image, determined by the corresponding array element’s value and mapped to a specified colormap.

The colorbar() function, after imshow(), adds a colorbar to the plot, displaying the mapping between pixel values and colors in the image’s colormap, aiding viewers in understanding the range of values represented by different colors.

data = np.random.rand(10, 10)
plt.imshow(data, cmap='hot', interpolation='nearest')

This code creates a 2D heatmap using Matplotlib’s imshow() function, using a 10×10 array of NumPy random values. The colormap is set to ‘hot’, representing high values with red and low values with black. The interpolation method is specified using the ‘nearest’ parameter. A colorbar is added to the plot for color mapping.

26. Subplots with GridSpec

GridSpec is a Matplotlib module that enables complex subplot layouts by dividing a figure into a grid of cells with specific rows and columns. It allows for fine-grained control over subplot positioning and layout by adding subplots to specific cells in the grid.

import matplotlib.gridspec as gridspec
fig = plt.figure()
gs = gridspec.GridSpec(2, 2)
ax1 = fig.add_subplot(gs[0, 0])
ax2 = fig.add_subplot(gs[0, 1])
ax3 = fig.add_subplot(gs[1, :])

The code generates a figure with a grid layout of 2 rows and 2 columns using GridSpec from matplotlib.gridspec. The figure adds three subplots, positioning them using grid coordinates gs[0, 0], gs[0, 1], and gs[1, :], representing the first row and first column, first row and second column, and spanning both columns of the second row, respectively.

27. Sharing Axes

Matplotlib’s subplots() method creates a figure and a collection of subplots. Subplots(2, sharex=True) in this instance generates two vertically organized subplots with two rows each that share the same x-axis. This indicates that for both subplots, the x-axis scale and labels will be the same.

fig, (ax1, ax2) = plt.subplots(2, sharex=True)
ax1.plot([1, 2, 3, 4], [1, 4, 9, 16])
ax2.plot([1, 2, 3, 4], [1, 2, 3, 4])

This code produces a figure with two vertically ordered subplots (two rows). Because of the sharex=True argument, both subplots share the x-axis. The ax1 subplot plots curves with x-values [1, 2, 3, 4] and corresponding y-values [1, 4, 9, 16], while the ax2 subplot plots curves with x-values [1, 2, 3, 4] and corresponding y-values [1, 2, 3, 4].

28. Twin Axes

Matplotlib’s twinx() method adds a new y-axis to the other side of the previous y-axis. This feature enables plotting data with different scales on the y-axis while using the same x-axis. When comparing datasets with varied units or ranges but a common x-axis, this is helpful.

fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.plot([1, 2, 3, 4], [1, 4, 9, 16], 'g-')
ax2.plot([1, 2, 3, 4], [10, 20, 30, 40], 'b-')

Using the same x-axis, this code generates a figure and two subplots. Plotting green solid lines (‘g-‘) indicates the ax1 subplot, while blue solid lines (‘b-‘) indicate the ax2 subplot. The twinx() function enables the use of different y-axis scales on the same x-axis by creating twin Axes that share the same x-axis as ax1.

29. Polar Plots

The polar() function in Matplotlib creates polar plots, representing data in polar coordinates instead of Cartesian ones. It is commonly used to visualize cyclic or periodic data like angles or phases. The function takes two arrays, theta and r, and plots the data on a polar coordinate system.

theta = np.linspace(0, 2*np.pi, 100)
r = np.sin(theta)**2
plt.polar(theta, r)

A polar plot is produced by Matplotlib’s polar() function. In this illustration, r stands for the radial coordinate and theta for the angular coordinate, which ranges from 0 to 2π. It plots the sine squared function in polar coordinates.

30. Clearing the Plot

The plt.clf() function clears the current figure, returning it to its initial empty state. In other words, the figure will no longer have any plotted data or annotations, creating a blank space for new plots or visualizations.

plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
plt.clf()  # Clear the current plot


’30 Quick Matplotlib Tips and Tricks for Beginners’ offers a comprehensive guide for beginners to master Matplotlib, enabling them to create impactful visualizations. It covers fundamental concepts like importing Matplotlib and creating basic plots, as well as advanced techniques like customizing line styles, adding annotations, and leveraging complex visualizations. The guide provides clear explanations and practical code examples, equipping beginners with the knowledge and skills to effectively communicate insights and tell compelling stories through data visualization.

You can also enroll in our free python course today!

Ayushi Trivedi 07 May, 2024

Frequently Asked Questions

Lorem ipsum dolor sit amet, consectetur adipiscing elit,

Responses From Readers