在 matplotlib 中的一个图像中加入子图
Join subplots in one single image in matplotlib
我有这个接收数据帧、字符串和参数列表的函数:
def descriptive_analysis(df, net, parameters):
'''
df: pandas dataframe
net: str with network name
parameters: list of 7 (str) features
'''
for parameter in parameters:
fig = plt.figure(figsize=(20,5))
# subplot 1
plt.subplot(1, 3, 1)
# histogram
sns.histplot(df[parameter])
# subplot 2
plt.subplot(1, 3, 2)
# boxplot
df[parameter].plot(kind = 'box')
# subplot 3
ax3 = plt.subplot(1,3,3)
# qqplot Gráfico de probabilidade da Normal
sm.qqplot(df[parameter], line='s', ax=ax3)
# add legend
fig.legend(labels=[f'network:{net}, metric:{parameter}'])
上面为每个参数、每个网络生成了多个三元组子图,如下所示:
所以:
等等:
如何为每个网络绘制一个包含所有三元组子图(总共 7 个)的单张图像?
- 在循环外创建一个
subplots
的 7x3 ax_grid
。
- 用
ax_grid
压缩 parameters
以用 ax_row
. 迭代每个 parameter
- 将 hist/box/qq 图放置在其各自的
ax=ax_row[...]
上。
def descriptive_analysis(df, net, parameters):
'''
df: pandas dataframe
net: str with network name
parameters: list of 7 (str) features
'''
fig, ax_grid = plt.subplots(len(parameters), 3, constrained_layout=True)
for parameter, ax_row in zip(parameters, ax_grid):
sns.histplot(df[parameter], ax=ax_row[0])
df[parameter].plot(kind='box', ax=ax_row[1])
sm.qqplot(df[parameter], line='s', ax=ax_row[2])
fig.savefig('results.png', dpi=300)
我有这个接收数据帧、字符串和参数列表的函数:
def descriptive_analysis(df, net, parameters):
'''
df: pandas dataframe
net: str with network name
parameters: list of 7 (str) features
'''
for parameter in parameters:
fig = plt.figure(figsize=(20,5))
# subplot 1
plt.subplot(1, 3, 1)
# histogram
sns.histplot(df[parameter])
# subplot 2
plt.subplot(1, 3, 2)
# boxplot
df[parameter].plot(kind = 'box')
# subplot 3
ax3 = plt.subplot(1,3,3)
# qqplot Gráfico de probabilidade da Normal
sm.qqplot(df[parameter], line='s', ax=ax3)
# add legend
fig.legend(labels=[f'network:{net}, metric:{parameter}'])
上面为每个参数、每个网络生成了多个三元组子图,如下所示:
所以:
等等:
如何为每个网络绘制一个包含所有三元组子图(总共 7 个)的单张图像?
- 在循环外创建一个
subplots
的 7x3ax_grid
。 - 用
ax_grid
压缩parameters
以用ax_row
. 迭代每个 - 将 hist/box/qq 图放置在其各自的
ax=ax_row[...]
上。
parameter
def descriptive_analysis(df, net, parameters):
'''
df: pandas dataframe
net: str with network name
parameters: list of 7 (str) features
'''
fig, ax_grid = plt.subplots(len(parameters), 3, constrained_layout=True)
for parameter, ax_row in zip(parameters, ax_grid):
sns.histplot(df[parameter], ax=ax_row[0])
df[parameter].plot(kind='box', ax=ax_row[1])
sm.qqplot(df[parameter], line='s', ax=ax_row[2])
fig.savefig('results.png', dpi=300)