📜  Python中的 Matplotlib.pyplot.subplot2grid()

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

Python中的 Matplotlib.pyplot.subplot2grid()

Matplotlib 是Python中用于数组二维图的惊人可视化库。 Matplotlib 是一个基于 NumPy 数组构建的多平台数据可视化库,旨在与更广泛的 SciPy 堆栈配合使用。

Matplotlib.pyplot.subplot2grid()

Matplotlib.pyplot.subplot2grid()函数在网格内的指定位置创建轴对象时提供了额外的灵活性。它还有助于跨越多行或多列的轴对象。简而言之,此函数用于在同一个图形中创建多个图表。它是一个子图布局管理器。

示例 1:

import matplotlib.pyplot as plt
  
  
fig = plt.figure()
  
axes1 = plt.subplot2grid((4, 4), (0, 0),
                         colspan = 4)
  
axes2 = plt.subplot2grid((4, 4), (1, 0),
                         colspan = 3)
  
axes3 = plt.subplot2grid((4, 4), (1, 2), 
                         rowspan = 3)
  
axes4 = plt.subplot2grid((4, 4), (2, 0))
axes5 = plt.subplot2grid((4, 4), (2, 1))
  
fig.tight_layout()

输出 :

matplotlib.pyplot.subplot2grid()
示例 2:

import random
import matplotlib.pyplot as plt
from matplotlib import style
  
  
style.use('fivethirtyeight')
  
fig = plt.figure()
  
  
# helper function to plot the lines
def helper():
      
    xs = []
    ys = []
  
    for i in range(10):
        x = i
        y = random.randrange(10)
  
        xs.append(x)
        ys.append(y)
    return xs, ys
  
axes1 = plt.subplot2grid ((7, 1), (0, 0),
                          rowspan = 2, 
                          colspan = 1)
  
axes2 = plt.subplot2grid ((7, 1), (2, 0),
                          rowspan = 2,
                          colspan = 1)
  
axes3 = plt.subplot2grid ((7, 1), (4, 0), 
                          rowspan = 2, 
                          colspan = 1)
  
x, y = helper()
axes1.plot(x, y)
  
x, y = helper()
axes2.plot(x, y)
  
x, y = helper()
axes3.plot(x, y)

输出:
matplotlib.pyplot.subplot2grid()