Change plot size in Matplotlib
0 905
Adjusting Plot Dimensions in Matplotlib
import matplotlib.pyplot as plt
# Sample data
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
# Default plot
plt.plot(x, y)
plt.title("Default Plot")
plt.show()
# Customizing figure size
plt.figure(figsize=(10, 5))
plt.plot(x, y)
plt.title("Customized Plot Size")
plt.show()
Utilizing set_figwidth() and set_figheight()
import matplotlib.pyplot as plt
# Sample data
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
# Create figure and axes
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y)
# Adjusting width and height
fig.set_figwidth(12)
fig.set_figheight(6)
plt.title("Adjusted Width and Height")
plt.show()
Setting Default Plot Size with rcParams
import matplotlib.pyplot as plt
import matplotlib as mpl
# Set default figure size
mpl.rcParams['figure.figsize'] = [8, 4]
# Sample data
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
# Plot with default size
plt.plot(x, y)
plt.title("Plot with Default Size Set by rcParams")
plt.show()
Conclusion
Adjusting the size of plots in Matplotlib is crucial for clarity and presentation. By using methods like figsize, set_figwidth(), set_figheight(), and modifying rcParams, you can tailor the dimensions of your plots to suit various needs, ensuring your data is displayed effectively.
If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!
Share:


Comments
Waiting for your comments