2 回答

TA貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超14個(gè)贊
您需要將地塊分配給 ax ,它也將是 set_title 等:
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
data = pd.read_csv("datasets_228_482_diabetes.csv")
fig,ax = plt.subplots(3,3,figsize=(9,9))
ax = ax.flatten()
for i,column in enumerate(data.columns):
a1 = data[(data['Outcome'] == 0)][column]
a2 = data[(data['Outcome'] == 1)][column]
ax[i].hist(a1, color='blue', alpha=0.6, label='Have Diabetes = NO')
ax[i].hist(a2, color='yellow', alpha=0.6, label='Have Diabetes = YES')
ax[i].set_title('Histogram for '+column)
ax[i].set_xlabel(f'{column}')
ax[i].set_ylabel('number of people')
ax[i].legend(loc='upper right',frameon=True,markerscale=7,fontsize=7)
fig.tight_layout()
正如您所看到的,最后一列的結(jié)果非常無用,所以如果您不繪制它,您也可以考慮使用 seaborn:
g = sns.FacetGrid(data=data.melt(id_vars="Outcome"),
col="variable",hue="Outcome",sharex=False,sharey=False,
col_wrap=4,palette=['blue','yellow'])
g = g.map(plt.hist,"value",alpha=0.7)

TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超6個(gè)贊
我認(rèn)為你應(yīng)該使用axes而不是pyplot:
from matplotlib import pyplot as plt
fig, axes = plt.subplots(3,3, figsize=(9,9))
for d, ax in zip(data_list, axes.ravel()):
ax.hist(d) # or something similar
添加回答
舉報(bào)