使用 matplotlib 在一个图中绘制多个直方图
Plotting more than one histogram in a figure with matplotlib
我需要在助手 class 中编写函数代码,它将在一个图中绘制多个直方图。使用下面的代码,我得到 ValueError:要解压的值太多 。在 for 命令行中:变量多于值,反之亦然。我做错了什么?
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
这是
'unpack' 没有三个变量,只有一个变量,因此出现错误(详情见下文)。您可能不需要 for
循环,因为 df.hist()
有一个 layout
参数,允许您使用 (n_row, n_col)
参数定义相同的参数。
df = pd.DataFrame(data=np.random.random(size=(50, 6)), columns=[i for i in string.ascii_lowercase[:6]])
df.hist(layout=(3,2))
plt.show()
当您在 for
循环中迭代 df
时:
for n_rows, n_cols, plot_number in df:
df
returns 每次迭代只有一个值,即 column
个名称。
参见示例:
df = pd.DataFrame(data=np.random.random(size=(5, 5)), columns=[i for i in string.ascii_lowercase[:5]])
print([i for i in df])
['a', 'b', 'c', 'd', 'e']
ValueError
被引发是因为 n_rows, n_cols, plot_number
建议在每次迭代期间解压三个值。使用 for ... in df.items()
,例如,您将获得两个值 - 列名和数据:
for i, col in df.items():
print('\n', i)
print(col)
a
0 0.640400
1 0.683003
2 0.807806
3 0.767698
4 0.648523
Name: a, dtype: float64
b
0 0.774166
1 0.052386
2 0.235688
3 0.018334
4 0.492798
Name: b, dtype: float64
c
0 0.390146
1 0.383680
2 0.588734
3 0.911859
4 0.901137
Name: c, dtype: float64
d
0 0.455289
1 0.626278
2 0.977627
3 0.311236
4 0.570580
Name: d, dtype: float64
e
0 0.782046
1 0.041161
2 0.226500
3 0.331402
4 0.942302
Name: e, dtype: float64
我需要在助手 class 中编写函数代码,它将在一个图中绘制多个直方图。使用下面的代码,我得到 ValueError:要解压的值太多 。在 for 命令行中:变量多于值,反之亦然。我做错了什么?
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
这是
'unpack' 没有三个变量,只有一个变量,因此出现错误(详情见下文)。您可能不需要 for
循环,因为 df.hist()
有一个 layout
参数,允许您使用 (n_row, n_col)
参数定义相同的参数。
df = pd.DataFrame(data=np.random.random(size=(50, 6)), columns=[i for i in string.ascii_lowercase[:6]])
df.hist(layout=(3,2))
plt.show()
当您在 for
循环中迭代 df
时:
for n_rows, n_cols, plot_number in df:
df
returns 每次迭代只有一个值,即 column
个名称。
参见示例:
df = pd.DataFrame(data=np.random.random(size=(5, 5)), columns=[i for i in string.ascii_lowercase[:5]])
print([i for i in df])
['a', 'b', 'c', 'd', 'e']
ValueError
被引发是因为 n_rows, n_cols, plot_number
建议在每次迭代期间解压三个值。使用 for ... in df.items()
,例如,您将获得两个值 - 列名和数据:
for i, col in df.items():
print('\n', i)
print(col)
a
0 0.640400
1 0.683003
2 0.807806
3 0.767698
4 0.648523
Name: a, dtype: float64
b
0 0.774166
1 0.052386
2 0.235688
3 0.018334
4 0.492798
Name: b, dtype: float64
c
0 0.390146
1 0.383680
2 0.588734
3 0.911859
4 0.901137
Name: c, dtype: float64
d
0 0.455289
1 0.626278
2 0.977627
3 0.311236
4 0.570580
Name: d, dtype: float64
e
0 0.782046
1 0.041161
2 0.226500
3 0.331402
4 0.942302
Name: e, dtype: float64