Figure Size and Grid Lines

Basic Gridlines in Matplotlib

Grid lines in Matplotlib are horizontal and vertical lines that form a grid overlaying the plot area. They serve as visual aids to help interpret data points and their relationships within the plot.

In Matplotlib, you can enable grid lines using the grid() function. By default, grid lines are not displayed, but you can turn them on by passing True as an argument to the function

# Adding grid lines to the plot
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
plt.grid(True)  # This magic line adds grid lines!
plt.show()

Output:

Customizing Grid Lines

You can customize the style of grid lines, including their color, line style, and width, using parameters such as color, linestyle, and linewidth in the grid() function.

Example:

# Customizing grid lines
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
plt.grid(True, color='red', linestyle='--', linewidth=1)
plt.show()

Output:

Show Only on the X-axis or Y-axis

By using the axis parameter, you can customize them to show up only on the X-axis or Y-axis. Let’s check it out:

Example:

# Grid lines on specific axes
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
plt.grid(True, axis='x', color='blue', linestyle='-', linewidth=0.8)
plt.show()

Output:

Line plot with customized grid lines in red dashed style and width 1. The plot shows a line connecting points (1, 1), (2, 4), (3, 9), and (4, 16).

Change the Figure Size

In Matplotlib, the figure() function helps you adjust your plot’s dimensions to fit specific requirements or improve its visual presentation.

You can set the size of the figure using the figsize parameter in the figure() function. This parameter takes a tuple of (width, height) in inches, specifying the dimensions of the figure.

Example:

import matplotlib.pyplot as plt

# Creating a figure with a specified size (width, height)
plt.figure(figsize=(8, 6))  # 8 units wide, 6 units high
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
plt.title('Small Canvas')
plt.show()

plt.figure(figsize=(12, 8))  # 12 units wide, 8 units high
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
plt.title('Larger Canvas')
plt.show()

Output:

The first plot has a smaller canvas (8 units wide, 6 units high) and displays a line graph with points (1, 1), (2, 4), (3, 9), and (4, 16). The title is "Small Canvas".
The second plot has a larger canvas (12 units wide, 8 units high) and shows the same line graph with the same points. The title is "Larger Canvas".

Controlling Resolution

In Matplotlib, resolution determines the level of detail and quality in your graph. You can control the resolution using the dpi parameter within the plt.figure() function.

DPI stands for “dots per inch,” representing the number of pixels in an inch of your graph. Higher DPI means sharper and clearer visuals, perfect for high-quality printouts or detailed online displays.

Example:

import matplotlib.pyplot as plt

# Setting a custom figure size and resolution (dpi)
plt.figure(figsize=(8, 6), dpi=100)  # 8 inches wide, 6 inches high, 100 dots per inch
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
plt.title('High-Resolution Graph')
plt.show()

Output:

Matplotlib graph with a custom figure size of 8 inches wide and 6 inches high, rendered at 100 dots per inch resolution. The graph displays a line plot with points (1, 1), (2, 4), (3, 9), and (4, 16), titled 'High-Resolution Graph'.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *