Python 如何在多个子地块中绘图

Python 如何在多个子地块中绘图,python,matplotlib,subplot,Python,Matplotlib,Subplot,我对这段代码的工作原理有点困惑: fig, axes = plt.subplots(nrows=2, ncols=2) plt.show() 在这种情况下,fig轴是如何工作的?它有什么作用 为什么这项工作不能做同样的事情: fig = plt.figure() axes = fig.subplots(nrows=2, ncols=2) 阅读文档: subplot.subplot返回一个元组fig,ax,使用符号将其解包为两个变量 fig, axes = plt.subplots(nrows

我对这段代码的工作原理有点困惑:

fig, axes = plt.subplots(nrows=2, ncols=2)
plt.show()
在这种情况下,fig轴是如何工作的?它有什么作用

为什么这项工作不能做同样的事情:

fig = plt.figure()
axes = fig.subplots(nrows=2, ncols=2)
阅读文档:

subplot.subplot返回一个元组fig,ax,使用符号将其解包为两个变量

fig, axes = plt.subplots(nrows=2, ncols=2)
守则:

fig = plt.figure()
axes = fig.subplots(nrows=2, ncols=2)

不起作用,因为子图是pyplot中的一个函数,而不是对象图形的成员。

有几种方法可以做到这一点。子图方法创建图形以及子图,然后子图存储在ax数组中。例如:

import matplotlib.pyplot as plt

x = range(10)
y = range(10)

fig, ax = plt.subplots(nrows=2, ncols=2)

for row in ax:
    for col in row:
        col.plot(x, y)

plt.show()
但是,类似的方法也会起作用,但它不是很干净,因为您正在创建一个带有子图的图形,然后在其上添加:

fig = plt.figure()

plt.subplot(2, 2, 1)
plt.plot(x, y)

plt.subplot(2, 2, 2)
plt.plot(x, y)

plt.subplot(2, 2, 3)
plt.plot(x, y)

plt.subplot(2, 2, 4)
plt.plot(x, y)

plt.show()

您可能会感兴趣的是,从matplotlib版本2.1开始,问题中的第二个代码也可以正常工作

从:

Figure类现在有子图块方法 Figure类现在有一个SubPlot方法,其行为与pyplot.SubPlot相同,但在现有的Figure上

例如:

import matplotlib.pyplot as plt

fig = plt.figure()
axes = fig.subplots(nrows=2, ncols=2)

plt.show()
也可以在子批调用中解包轴

并设置是否要在子地块之间共享x轴和y轴

像这样:

import matplotlib.pyplot as plt
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2, sharex=True, sharey=True)
ax1.plot(range(10), 'r')
ax2.plot(range(10), 'b')
ax3.plot(range(10), 'g')
ax4.plot(range(10), 'k')
plt.show()

如果确实要使用循环,请执行以下操作:

def plot(data):
    fig = plt.figure(figsize=(100, 100))
    for idx, k in enumerate(data.keys(), 1):
        x, y = data[k].keys(), data[k].values
        plt.subplot(63, 10, idx)
        plt.bar(x, y)  
    plt.show()
生成图形及其子图 图,轴=plt.subplotsnrows,ncol 按顺序迭代所有子批次:

对于轴中的ax.flatte: ax.plotx,y 通过索引访问特定子批次:

对于rangenrows中的行: 对于RangeCols中的col: 轴[行,列]。绘图X[行],y[列]
其他答案都很好,这个答案是一个可能有用的组合

import numpy as np
import matplotlib.pyplot as plt

# Optional: define x for all the sub-plots
x = np.linspace(0,2*np.pi,100)

# (1) Prepare the figure infrastructure 
fig, ax_array = plt.subplots(nrows=2, ncols=2)

# flatten the array of axes, which makes them easier to iterate through and assign
ax_array = ax_array.flatten()

# (2) Plot loop
for i, ax in enumerate(ax_array):
  ax.plot(x , np.sin(x + np.pi/2*i))
  #ax.set_title(f'plot {i}')

# Optional: main title
plt.suptitle('Plots')
总结 准备图形基础结构

获取ax_数组,一个子批数组 展平阵列,以便在一个“for循环”中使用它 绘图循环

在展平的ax_阵列上循环以更新子批次 可选:使用枚举跟踪子批次号
我的绘图不是plotx,而是来自用户定义的函数,该函数使用networkx创建图形。如何使用它?是否可以在没有for循环的情况下生成多个子批次?例如,在单独的绘图上绘制多列数据的矢量化解决方案?这对于R中的ggplot非常简单,但对于Python来说似乎不可能。您可以通过axn=ax.flatte将两个for循环减少为一个,然后在axn中对轴:axes.plotx,y@wander95谢谢这个答案不适用于col=1或row=1没有你我知道ax是什么,但不是fig。它们是什么?ax实际上是一个numpy数组。fig是matplotlib.figure.figure类,通过该类,您可以对打印的图形执行大量操作。例如,您可以将colorbar添加到特定的子图,您可以更改所有子图后面的背景色。您可以修改这些子地块的布局或向其添加新的小ax。您最好希望所有子地块都有一个主标题,可以通过fig.Suptitletle方法获得。最后,一旦您对绘图满意,就可以使用fig.savefig方法保存它@利沃
import numpy as np
import matplotlib.pyplot as plt

# Optional: define x for all the sub-plots
x = np.linspace(0,2*np.pi,100)

# (1) Prepare the figure infrastructure 
fig, ax_array = plt.subplots(nrows=2, ncols=2)

# flatten the array of axes, which makes them easier to iterate through and assign
ax_array = ax_array.flatten()

# (2) Plot loop
for i, ax in enumerate(ax_array):
  ax.plot(x , np.sin(x + np.pi/2*i))
  #ax.set_title(f'plot {i}')

# Optional: main title
plt.suptitle('Plots')