我有這個函式,它接收一個資料幀、一個字串和一個引數串列:
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 個)連接在一起的單個影像?
uj5u.com熱心網友回復:
- 在回圈外創建一個 7x3
ax_grid的subplots。 - 壓縮
parameterswithax_grid以parameter使用ax_row. - 將 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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/440747.html
標籤:Python 熊猫 matplotlib
