4
Lesson 4
Customizing Plots
The size of a figure can be controlled using the figsize parameter in the figure() function or the plt.subplots() function. The figsize parameter takes a tuple of two values: the width and height of the figure in inches.
Syntax:
Syntax:
plt.figure(figsize=(width, height))
Example:
import matplotlib.pyplot as plt # Create a figure with a specific size plt.figure(figsize=(10, 5)) # Width of 10 inches and height of 5 inches # Sample data x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10] # Create a line plot plt.plot(x, y) # Add title and labels plt.title('Custom Figure Size') plt.xlabel('X-axis') plt.ylabel('Y-axis') # Show the plot plt.show()
In this example, the figure is set to be wider, which can help make the data points more distinguishable.
Modifying axes
Matplotlib provides several functions to modify the axes of a plot. These functions allow you to set the limits of the axes and customize the ticks displayed on them.
a. Setting axis limits
You can modify the limits of the x-axis and y-axis using xlim() and ylim(), respectively.
Example:
import matplotlib.pyplot as plt # Sample data x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10] # Create a line plot plt.plot(x, y) # Set x-axis and y-axis limits plt.xlim(0, 6) # x-axis from 0 to 6 plt.ylim(0, 12) # y-axis from 0 to 12 # Add title and labels plt.title('Modified Axis Limits') plt.xlabel('X-axis') plt.ylabel('Y-axis') # Show the plot plt.show()
In this example, the x-axis limits are set from 0 to 6, and the y-axis limits are set from 0 to 12.
b. Setting axis ticks
You can modify the ticks on the axes using xticks() and yticks(). These functions allow you to specify the location and labels of the ticks.
Example:
import matplotlib.pyplot as plt # Sample data x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10] # Create a line plot plt.plot(x, y) # Set custom ticks for the x-axis and y-axis plt.xticks([1, 2, 3, 4, 5]) # Set x-ticks plt.yticks([0, 2, 4, 6, 8, 10]) # Set y-ticks # Add title and labels plt.title('Custom Axis Ticks') plt.xlabel('X-axis') plt.ylabel('Y-axis') # Show the plot plt.show()
In this example, we specify the tick locations for both the x-axis and y-axis to provide a clearer representation of the data.
Adding grid lines
Grid lines can help improve the readability of plots by making it easier to see where data points fall in relation to the axes. You can add grid lines using the grid() function.
Example:
import matplotlib.pyplot as plt # Sample data x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10] # Create a line plot plt.plot(x, y) # Add grid lines plt.grid(True) # Enable grid # Add title and labels plt.title('Grid Lines in a Plot') plt.xlabel('X-axis') plt.ylabel('Y-axis') # Show the plot plt.show()
In this example, grid lines are enabled for the plot, which helps to visualize the data points in relation to the axes.
Conclusion
In this lesson, we learned how to customize plots in Matplotlib by adjusting the figure size, modifying the limits and ticks of the axes, and adding grid lines. These customizations enhance the readability and presentation of your visualizations. In the next lesson, we will explore more advanced customization options, including formatting text, colors, and annotations.