📅  最后修改于: 2023-12-03 15:18:36.578000             🧑  作者: Mango
plt.axes
multiple plotsThe plt.axes
function in matplotlib is used to create multiple plots in the same figure. It allows you to have more control over the layout and arrangement of subplots.
When creating multiple plots using plt.axes
, you can specify the position and size of each subplot within the figure. This function provides a flexible way to create custom arrangements of subplots.
The basic syntax of plt.axes
function is:
plt.axes(rect, projection=None, polar=False)
rect
: A sequence of 4 floats representing the position and size of the subplot (left, bottom, width, height) in normalized (0 to 1) coordinates.projection
(optional): The type of projection for the subplot, such as 'polar' for a polar plot.polar
(optional): Boolean value indicating whether to create a polar plot.Here is an example that demonstrates the usage of plt.axes
to create multiple plots:
import matplotlib.pyplot as plt
import numpy as np
# Create a figure and a set of subplots using plt.axes
fig = plt.figure()
# Create the first subplot
ax1 = fig.add_axes([0.1, 0.1, 0.4, 0.8]) # left, bottom, width, height
ax1.plot([1, 2, 3, 4], [1, 4, 2, 3])
# Create the second subplot
ax2 = fig.add_axes([0.6, 0.1, 0.3, 0.4])
x = np.linspace(0, 2 * np.pi, 100)
ax2.plot(x, np.sin(x))
# Create the third polar subplot
ax3 = fig.add_axes([0.6, 0.6, 0.3, 0.3], polar=True)
ax3.plot(x, np.cos(4 * x))
# Show the figure with all subplots
plt.show()
This example creates a figure with three subplots: one regular 2D plot, one smaller 2D plot, and one polar plot. The plt.axes
function is used to define the position and size of each subplot within the figure.
With plt.axes
, you can easily create and arrange multiple plots in matplotlib figures. By specifying the position and size of each subplot, you have complete control over the layout of your plots. This function is especially useful when you need to create more complex arrangements or combine different types of plots in a single figure.