Customizing styles in Matplotlib refers to the process of modifying the visual appearance of plots such as colors, fonts, line styles and background themes to create visually appealing and informative data visualizations. Using built-in styles, custom style files you can control the appearance of your plots to match reports, presentations or personal preferences.
Figure Class
The Figure class in Matplotlib represents the entire plotting area where one or more charts (axes) are drawn. It acts as the main container for all plots in a figure. Below is the syntax used to create a Figure object:
class matplotlib.figure.Figure( figsize=None, dpi=None, facecolor=None, edgecolor=None, linewidth=0.0, frameon=None, subplotpars=None, tight_layout=None, constrained_layout=None)
Example 1: This example creates a figure and adds one plotting area (axes) to it. A simple line graph is then drawn inside that figure.
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
# new figure with width = 5 inches and height = 4 inches
fig = plt.figure(figsize =(5, 4))
# new axes for the figure
ax = fig.add_axes([1, 1, 1, 1])
# Adding the data to be plotted
ax.plot([2, 3, 4, 5, 5, 6, 6], [5, 7, 1, 3, 4, 6 ,8])
plt.show()
Output

Example 2: This example creates one figure and adds two different axes inside it. Each axes displays its own line plot within the same figure window.
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
# new figure with width = 5 inches and height = 4 inches
fig = plt.figure(figsize =(5, 4))
# first axes for the figure
ax1 = fig.add_axes([1, 1, 1, 1])
# second axes for the figure
ax2 = fig.add_axes([1, 0.5, 0.5, 0.5])
# Adding the data to be plotted
ax1.plot([2, 3, 4, 5, 5, 6, 6], [5, 7, 1, 3, 4, 6 ,8])
ax2.plot([1, 2, 3, 4, 5], [2, 3, 4, 5, 6])
plt.show()
Output

Related Articles
- Matplotlib.figure.Figure()
- Matplotlib.figure.Figure.add_axes()
- Matplotlib.figure.Figure.clear()
- Matplotlib.figure.Figure.colorbar()
- Matplotlib.figure.Figure.get_figwidth()
- Matplotlib.figure.Figure.get_figheight()
- Matplotlib.figure.Figure.subplots()
Pyplot Class
The Pyplot module in Matplotlib is used to create and control plots using simple plotting functions. It allows you to quickly draw and display graphs. Below is the syntax of the plot() function used in Pyplot:
matplotlib.pyplot.plot(*args, scalex=True, scaley=True, data=None, **kwargs)
Example: This example uses Pyplot to draw a line graph by providing x and y values and then displays it on the screen.
import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
plt.axis([0, 6, 0, 20])
plt.show()
Output

Axes Class
The Axes class represents the plotting area inside a figure where data is drawn. Each figure can contain multiple axes, but each axes belongs to only one figure. Below is the syntax used to create and control an axes object:
matplotlib.pyplot.axes(*args, **kwargs)
Example 1: This example creates an empty axes region on the figure by specifying its position and size.
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
# Creating the axes object with argument as [left, bottom, width, height]
ax = plt.axes([1, 1, 1, 1])
Output

Example 2: This example creates a figure, adds an axes to it and plots two line datasets inside the same axes.
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
fig = plt.figure(figsize = (5, 4))
# Adding the axes to the figure
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
# plotting 1st dataset to the figure
ax1 = ax.plot([1, 2, 3, 4], [1, 2, 3, 4])
# plotting 2nd dataset to the figure
ax2 = ax.plot([1, 2, 3, 4], [2, 3, 4, 5])
plt.show()
Output

Related articles:
- Matplotlib – Axes Class
- Matplotlib.axes.Axes.update()
- Matplotlib.axes.Axes.draw()
- Matplotlib.axes.Axes.get_figure()
- Matplotlib.axes.Axes.set_figure()
- Matplotlib.axes.Axes.properties()
Setting Colors
Colors in Matplotlib control the appearance of lines, markers and plot elements to visually distinguish data on a graph.
Example 1: This example draws a line plot and applies a custom color to the line using the color parameter.
import matplotlib.pyplot as plt
# Define the Color
color = 'green'
plt.plot([1, 2, 3, 4], color=color)
plt.show()
Output

Example 2: This example plots points with circular markers and applies a different color to the marker fill.
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [1, 4, 9, 16]
plt.plot(x, y, marker='o', markerfacecolor='r')
plt.show()
Output

Related articles:
- Change Line Color in Matplotlib
- Matplotlib – Change Slider Color
- Adjust the Position of a Matplotlib Colorbar
- Listed Colormap class in Python
- Matplotlib colors to rgba()
- Change the Color of a Graph Plot in Matplotlib
Adding Text, Font and Grid lines
Text and grid lines in Matplotlib are used to label plots and show reference lines that make values easier to read. Below is the common syntax used to add text and grid lines to a plot:
plt.title(label, **style)
plt.grid(**style)
Example: This example creates a line chart and adds a title at the top along with grid lines in the background to make the plot easier to read.
import matplotlib.pyplot as plt
X = [12, 34, 23, 45, 67, 89]
Y = [1, 3, 67, 78, 7, 5]
plt.plot(X, Y)
# Add gridlines to the plot
plt.grid(color = 'green', linestyle = '--', linewidth = 0.5)
# displaying the title
plt.title(label='Number of Users of a particular Language', fontweight=10, pad='2.0')
plt.show()
Output
.png)
Related articles:
- add a grid on a figure in Matplotlib?
- Change Legend Font Size in Matplotlib?
- Change Fonts in matplotlib?
- change the font size of the Title in a Matplotlib figure ?
- How to Set Tick Labels Font Size in Matplotlib?
- Add Text Inside the Plot in Matplotlib
- How to add text to Matplotlib?
Adding Legends
Legends in Matplotlib are used to identify different plotted data by showing their labels inside a small box on the chart. Below is the basic syntax used to display a legend on a plot:
plt.legend(labels=None, loc='best', bbox_to_anchor=None, ncol=1)
Example: This example plots two lines and adds a legend so each line can be identified using its label.
import matplotlib.pyplot as plt
x = [3, 1, 3]
y = [3, 2, 1]
plt.plot(x, y)
plt.plot(y, x)
# Adding the legends
plt.legend(["blue", "orange"])
plt.show()
Output

Related articles:
- Matplotlib.pyplot.legend()
- Matplotlib.axes.Axes.legend()
- Change the legend position in Matplotlib
- How to Change Legend Font Size in Matplotlib?
- How Change the vertical spacing between legend entries in Matplotlib?
- Use multiple columns in a Matplotlib legend
- How to Create a Single Legend for All Subplots in Matplotlib?
- How to manually add a legend with a color box on a Matplotlib figure ?
- How to Place Legend Outside of the Plot in Matplotlib?
- How to Remove the Legend in Matplotlib?
- Remove the legend border in Matplotlib
Customizing Ticks and Tick Labels
Ticks and tick labels control how values are displayed along the X-axis and Y-axis in a Matplotlib plot. Below are the main methods used to control axis limits and tick labels:
ax.set_xlim(min, max)
ax.set_ylim(min, max)
ax.set_xticklabels(labels)
ax.set_yticklabels(labels)
Example: This example creates a line plot, restricts the X-axis range and assigns custom text labels to the X-axis ticks.
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
x = [3, 1, 3]
y = [3, 2, 1]
fig = plt.figure(figsize =(5, 4))
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
ax.plot(x, y)
ax.set_xlim(1, 2)
ax.set_xticklabels(["one", "two", "three", "four", "five", "six"])
plt.show()
Output

Related articles:
Applying Styles to Plots
Matplotlib styles change the visual theme of a plot such as colors, fonts and background. Below is the syntax used to apply a style to a plot:
plt.style.use(style_name)
Example: This example applies a predefined Matplotlib style and plots random data using that theme.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style
# creating an array of data for plot
data = np.random.randn(50)
# using the style for the plot
plt.style.use('Solarize_Light2')
plt.plot(data)
plt.show()
Output
.png)
Creating Multiple Subplots
Multiple subplots allow you to display more than one plot inside the same figure for side-by-side or stacked comparisons. Below are the main Matplotlib methods used to create multiple subplots:
1. add_axes() method: The add_axes() method figure module of matplotlib library is used to add an axes to the figure. Below is the syntax:
fig.add_axes([left, bottom, width, height])
Example: This example creates one figure and places two plots inside it by defining their exact positions.
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
fig = plt.figure(figsize =(5, 4))
ax1 = fig.add_axes([0.1, 0.1, 0.8, 0.8])
ax2 = fig.add_axes([0.5, 0.5, 0.3, 0.3])
ax1.plot([5, 4, 3, 2, 1], [2, 3, 4, 5, 6])
ax2.plot([1, 2, 3, 4, 5], [2, 3, 4, 5, 6])
plt.show()
Output

2. subplot() method: subplot() method adds another plot to the current figure at the specified grid position. Below is the syntax:
plt.subplot(nrows, ncols, index)
Example: This example splits the figure into two columns and places one plot in each.
import matplotlib.pyplot as plt
x = [3, 1, 3]
y = [3, 2, 1]
z = [1, 3, 1]
plt.figure()
# adding first subplot
plt.subplot(121)
plt.plot(x, y)
# adding second subplot
plt.subplot(122)
plt.plot(z, y)
Output

Note: Subplot() function have disadvantages like - It does not allow adding multiple subplots at the same time and It deletes the preexisting plot of the figure.
3. subplots() method: subplots() function is used to create figure and multiple subplots at the same time. Below is the syntax:
plt.subplots(nrows, ncols)
Example: This example creates one row with two subplots and plots data in each.
import matplotlib.pyplot as plt
fig, axes = plt.subplots(1, 2)
axes[0].plot([1, 2, 3, 4], [1, 2, 3, 4])
axes[0].plot([1, 2, 3, 4], [4, 3, 2, 1])
axes[1].plot([1, 2, 3, 4], [1, 1, 1, 1])
Output

4. subplot2grid() method: subplot2grid() function give additional flexibility in creating axes object at a specified location inside a grid. Below is the syntax:
plt.subplot2grid(shape, location, rowspan, colspan)
Example: This example creates three vertically stacked plots inside a grid layout.
import matplotlib.pyplot as plt
x = [3, 1, 3]
y = [3, 2, 1]
z = [1, 3, 1]
ax1 = plt.subplot2grid((7, 1), (0, 0), rowspan=2)
ax2 = plt.subplot2grid((7, 1), (2, 0), rowspan=2)
ax3 = plt.subplot2grid((7, 1), (4, 0), rowspan=2)
ax1.plot(x, y)
ax2.plot(x, z)
ax3.plot(z, y)
plt.show()
Output

Working with Images
Matplotlib provides image functions that allow you to read and display image files directly inside a plot window. Below is the syntax commonly used for working with images:
matplotlib.image.imread(filename)
matplotlib.pyplot.imshow(image)
Example: This example reads an image file from disk and displays it using Matplotlib.
import matplotlib.pyplot as plt
import matplotlib.image as img
im = img.imread("g4g.png")
plt.imshow(im)
plt.show()
Output
