Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to show an Axes Subplot in Python?
To show an axes subplot in Python, we can use the show() method from matplotlib. This method displays the figure window and renders all the plots that have been created. When multiple subplots are created, show() displays them together in a single window.
Basic Subplot Display
Steps
- Import matplotlib.pyplot and numpy
- Create x and y data points using numpy
- Plot x and y using
plot()method - To display the figure, use
show()method
Example
from matplotlib import pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = np.arange(10) y = np.exp(x) plt.plot(x, y) plt.show()
Multiple Subplots Example
You can create multiple subplots and display them using subplot() function ?
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2*np.pi, 100)
# Create first subplot
plt.subplot(2, 1, 1)
plt.plot(x, np.sin(x))
plt.title('Sine Wave')
# Create second subplot
plt.subplot(2, 1, 2)
plt.plot(x, np.cos(x))
plt.title('Cosine Wave')
plt.tight_layout()
plt.show()
Using Figure and Axes Objects
For more control, you can create figure and axes objects explicitly ?
import matplotlib.pyplot as plt
import numpy as np
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4))
x = np.linspace(0, 10, 50)
ax1.plot(x, x**2)
ax1.set_title('Quadratic')
ax2.plot(x, np.sqrt(x))
ax2.set_title('Square Root')
plt.show()
Key Points
-
plt.show()displays all created plots in the current figure - Use
plt.subplot()for simple subplot layouts - Use
plt.subplots()for more control over subplot arrangement -
plt.tight_layout()adjusts spacing between subplots automatically
Conclusion
Use plt.show() to display matplotlib subplots. For multiple subplots, use subplot() for simple layouts or subplots() for better control over the figure structure.
Advertisements
