To change the size of a figure drawn with Matplotlib, you can set the figure size using the plt.figure()
function before plotting your data. Here's an example:
python
import matplotlib.pyplot as plt
# Create a figure with a specific size
plt.figure(figsize=(8, 6)) # width=8 inches, height=6 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('Sample Plot')
# Show the plot
plt.show()
In this example:
- We use
plt.figure(figsize=(8, 6))
to create a figure with a width of 8 inches and a height of 6 inches. - We then plot some sample data using the
plt.plot()
function. - Additional formatting is done using functions like
plt.xlabel()
,plt.ylabel()
, andplt.title()
to add labels and a title to the plot. - Finally,
plt.show()
displays the plot.
You can adjust the figsize
parameter values to change the size of the figure as needed. The values are in inches, and the default DPI (dots per inch) is 100, so a figure size of (8, 6)
results in an 800x600 pixel display (8 * 100 x 6 * 100).
Keep in mind that setting the figure size before plotting is important because the figure size affects the layout of the plot elements.
Comments
Post a Comment