我需要在輔助類中爲函數編寫代碼,該類將在圖中繪製多個直方圖。用我下面的代碼,我得到ValueError:太多的值來解壓。在for命令行中:有多個變量而不是值,反之亦然。我究竟做錯了什麼?用matplotlib在圖中繪製多個直方圖
def draw_histograms(df, variables, n_rows, n_cols):
fig = plt.figure()
for n_rows, n_cols, plot_number in df:
fig.add_subplot(n_rows, n_cols, plot_number)
plt.show()
""" variables includes a list of variables you need to draw histograms for.
n_rows and n_cols specifies the number of subplots you need to have in a figure.
If n_rows =3 and n_cols =2, there will 3*2 = 6 subplots placed in a grid of 3 rows and 2 columns.
subplot(321) is identical to subplot(3,2,1), which refers to the 1st subplot in a grid of 3 rows and 2 columns"""
util.draw_histograms(df, variables = ['DerogCnt', 'CollectCnt', 'InqCnt06', 'InqTimeLast', 'InqFinanceCnt24', 'TLTimeFirst', 'TLTimeLast', 'TLCnt03', 'TLCnt12'], 3,3)
這是DF模樣。 變量不包含全部內容,因爲不相關的內容已被刪除。
TARGET ID DerogCnt CollectCnt BanruptcyInd InqCnt06 InqTimeLast \
0 0 66 1 1 0 7 1
1 0 116 1 1 0 2 1
2 0 124 0 0 0 1 1
3 0 128 0 0 0 6 3
4 0 143 0 0 0 1 0
InqFinanceCnt24 TLTimeFirst TLTimeLast ... TL50UtilCnt \
0 4 125 3 ... 4
1 0 252 18 ... 2
2 4 254 12 ... 3
3 6 154 3 ... 5
4 1 311 17 ... 3
TLBalHCPct TLSatPct TLDel3060Cnt24 TLDel90Cnt24 TLDel60CntAll \
0 0.85 0.67 0 0 1
1 0.48 0.30 0 1 4
2 0.84 0.67 0 1 1
3 0.73 0.76 0 1 1
4 0.88 0.63 0 0 1
TLOpenPct TLBadDerogCnt TLDel60Cnt24 TLOpen24Pct
0 0.58 0 0 0.71
1 0.40 2 1 0.50
2 0.50 1 1 0.33
3 0.53 1 1 1.22
4 0.63 0 0 0.20
這裏的
你不'顯示使用什麼在DF,所以我們不能告訴。順便說一句,你不要在'draw_histograms'函數中使用'variables'。 – roadrunner66