📌  相关文章
📜  如何使用Python在 Matplotlib 中为图形添加轴?

📅  最后修改于: 2022-05-13 01:55:07.955000             🧑  作者: Mango

如何使用Python在 Matplotlib 中为图形添加轴?

Matplotlib是Python中的一个库,用于创建图形并提供自定义工具。它允许绘制不同类型的数据、几何图形。在本文中,我们将看到如何在 matplotlib 中向图形添加轴。

通过在add_axes()方法中传递一个列表参数,我们可以在matplotlib中为图形添加轴。

下面是一些描述如何在matplotlib 中向图形添加轴的程序:

示例 1:

Python3
# Importing library
import matplotlib
  
# Create figure() objects
# This acts as a container
# for the different plots
fig = matplotlib.pyplot.figure()
  
# Creating axis
# add_axes([xmin,ymin,dx,dy])
axes = fig.add_axes([0.5, 1, 0.5, 1])
  
# Depict illustration
fig.show()


Python3
# Importing library
import matplotlib
  
# Create figure() objects
# This acts as a container 
# for the different plots
fig=matplotlib.pyplot.figure() 
  
# Creating two axes
# add_axes([xmin,ymin,dx,dy])
axes=fig.add_axes([0,0,2,2]) 
axes1=fig.add_axes([0,1,2,2])
  
# Depict illustration
fig.show()


Python3
# Import libraries
import matplotlib
import numpy
  
# Create figure() objects
# This acts as a container
# for the different plots
fig = matplotlib.pyplot.figure()
  
# Generate line graph
x = numpy.arange(0, 1.414*2, 0.05)
y1 = numpy.sin(x)
y2 = numpy.cos(x)
  
# Creating two axes
# add_axes([xmin,ymin,dx,dy])
axes1 = fig.add_axes([0, 0, 1, 1])
axes1.plot(x, y1)
axes2 = fig.add_axes([0, 1, 1, 1])
axes2.plot(x, y2)
  
# Show plot
plt.show()


输出:

示例 2:

蟒蛇3

# Importing library
import matplotlib
  
# Create figure() objects
# This acts as a container 
# for the different plots
fig=matplotlib.pyplot.figure() 
  
# Creating two axes
# add_axes([xmin,ymin,dx,dy])
axes=fig.add_axes([0,0,2,2]) 
axes1=fig.add_axes([0,1,2,2])
  
# Depict illustration
fig.show()

输出:

示例 3:

蟒蛇3

# Import libraries
import matplotlib
import numpy
  
# Create figure() objects
# This acts as a container
# for the different plots
fig = matplotlib.pyplot.figure()
  
# Generate line graph
x = numpy.arange(0, 1.414*2, 0.05)
y1 = numpy.sin(x)
y2 = numpy.cos(x)
  
# Creating two axes
# add_axes([xmin,ymin,dx,dy])
axes1 = fig.add_axes([0, 0, 1, 1])
axes1.plot(x, y1)
axes2 = fig.add_axes([0, 1, 1, 1])
axes2.plot(x, y2)
  
# Show plot
plt.show()

输出: