Last modified: Dec 14, 2024 By Alexander Williams
Master Matplotlib plt.figure(): Create Custom Plots
The plt.figure()
function is a fundamental component in Matplotlib that creates a new figure - the top-level container for all plot elements. Understanding how to use it effectively is crucial for creating professional visualizations.
Basic Usage of plt.figure()
At its simplest form, calling plt.figure()
creates a new figure window. This is particularly useful when you need to create multiple independent plots in your program.
import matplotlib.pyplot as plt
import numpy as np
# Create a new figure
plt.figure()
x = np.linspace(0, 10, 100)
plt.plot(x, np.sin(x))
plt.title('Simple Sine Wave')
plt.show()
Customizing Figure Size and Resolution
Figure size and DPI (dots per inch) are crucial parameters that affect the output quality of your plots. You can control these using the figsize and dpi parameters.
# Create a figure with custom size and DPI
plt.figure(figsize=(10, 6), dpi=100) # Width=10 inches, Height=6 inches
x = np.linspace(0, 10, 100)
plt.plot(x, np.cos(x))
plt.title('Cosine Wave with Custom Figure Size')
plt.show()
Working with Multiple Figures
When creating multiple plots, you can use figure numbers or labels to switch between different figures. This is especially useful for complex visualizations.
# Create multiple figures
plt.figure(1)
plt.plot([1, 2, 3], [1, 2, 3], 'r-')
plt.title('Figure 1')
plt.figure(2)
plt.plot([1, 2, 3], [1, 4, 9], 'b-')
plt.title('Figure 2')
plt.show()
Advanced Figure Properties
The plt.figure()
function offers various parameters to customize the appearance of your plots. You can set background color, frame properties, and more.
# Create a figure with custom background and frame
fig = plt.figure(
figsize=(8, 6),
facecolor='lightgray',
edgecolor='blue',
linewidth=2
)
plt.plot([1, 2, 3], [1, 2, 3])
plt.title('Customized Figure Appearance')
plt.show()
Integration with Subplots
The plt.figure()
function works seamlessly with subplots, allowing you to create complex layouts. For more detailed information about subplots, check out our guide on Matplotlib plt.subplots.
# Create a figure with multiple subplots
fig = plt.figure(figsize=(12, 4))
# Add three subplots
ax1 = fig.add_subplot(131)
ax2 = fig.add_subplot(132)
ax3 = fig.add_subplot(133)
# Plot different data in each subplot
ax1.plot([1, 2, 3], [1, 2, 3], 'r-')
ax2.plot([1, 2, 3], [1, 4, 9], 'g-')
ax3.plot([1, 2, 3], [1, 8, 27], 'b-')
plt.show()
Common Use Cases and Best Practices
When working with plt.figure()
, it's important to consider memory management, especially when creating multiple figures. Close unused figures to free up memory.
# Memory management with figures
plt.figure()
plt.plot([1, 2, 3], [1, 2, 3])
plt.savefig('plot.png') # Save the figure
plt.close() # Close the figure to free memory
For more advanced visualizations, you might want to explore our guide on displaying plots effectively with plt.show().
Conclusion
plt.figure()
is a powerful function that forms the foundation of Matplotlib plotting. Understanding its parameters and proper usage is essential for creating professional-quality visualizations.
For more complex visualizations, you might want to explore creating multiple plots with subplot.