How do I change the size of figures drawn with Matplotlib?

 

To change the size of figures drawn with Matplotlib, you can use the plt.figure() function and set the figsize parameter to control the width and height of the figure. Here's an example:

python
import matplotlib.pyplot as plt # Create a figure with a larger size plt.figure(figsize=(10, 8)) # width=10 inches, height=8 inches # Sample data x = [1, 2, 3, 4, 5] y = [10, 20, 15, 25, 30] # Plot the data plt.plot(x, y, marker='o') # Add labels and title plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.title('Larger Figure Size') # Show the plot plt.show()

In this example, the plt.figure(figsize=(10, 8)) call creates a figure with a width of 10 inches and a height of 8 inches. You can adjust the figsize parameter values to set the desired width and height for your figure.

Remember that the size of the figure affects the layout of plot elements, such as labels, legends, and titles. Adjusting the figure size can help you present your data more effectively in different contexts.

Comments