如何为 Pandas 数据框中的每一列创建箱线图?
How do I create a Box plot for each column in a Pandas Dataframe?
我的数据框(pandas 的结构)如上所示
现在我想在单独的 canvas 上为每个特征制作箱线图。分离条件是第一列。我有类似的直方图图(下面的代码),但我无法为箱线图制作工作版本。
hist_params = {'normed': True, 'bins': 60, 'alpha': 0.4}
# create the figure
fig = plt.figure(figsize=(16, 25))
for n, feature in enumerate(features):
# add sub plot on our figure
ax = fig.add_subplot(features.shape[1] // 5 + 1, 6, n + 1)
# define range for histograms by cutting 1% of data from both ends
min_value, max_value = numpy.percentile(data[feature], [1, 99])
ax.hist(data.ix[data.is_true_seed.values == 0, feature].values, range=(min_value, max_value),
label='ghost', **hist_params)
ax.hist(data.ix[data.is_true_seed.values == 1, feature].values, range=(min_value, max_value),
label='true', **hist_params)
ax.legend(loc='best')
ax.set_title(feature)
以上代码产生如下输出(仅附上其中的一部分):
DataFrame.boxplot()
很好地自动化了这个:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({'is_true_seed': np.random.choice([True, False], 10),
'col1': np.random.normal(size=10),
'col2': np.random.normal(size=10),
'col3': np.random.normal(size=10)})
is_true_seed col1 col2 col3
0 False -0.990041 -0.561413 -0.512582
1 False 0.825099 0.827453 -0.366211
2 True 0.083442 -1.199540 0.345792
3 True 0.065715 1.560029 -0.324501
4 True -1.699770 -0.270820 -1.380125
ax = df.boxplot(['col1', 'col2', 'col3'], 'is_true_seed', figsize=(10, 10))
第一个参数告诉 pandas 要绘制哪些列,第二个参数告诉要按哪一列分组(您称之为分离条件),第三个参数要在哪个轴上绘制。
列出除要作为分组依据的列之外的所有列可能会很乏味,但您可以通过省略第一个参数来避免这种情况。然后您必须明确命名其他两个:
ax = df.boxplot(by='is_true_seed', figsize=(10, 10))
如果您想为每列创建一个单独的图,那么您可以遍历每一列并使用 plt.figure()
为每个图创建一个新图形。
import matplotlib.pyplot as plt
for column in df:
plt.figure()
df.boxplot([column])
如果您只想将所有列放入同一个箱线图中,那么您可以使用 df.plot(kind='box')
我的数据框(pandas 的结构)如上所示
现在我想在单独的 canvas 上为每个特征制作箱线图。分离条件是第一列。我有类似的直方图图(下面的代码),但我无法为箱线图制作工作版本。
hist_params = {'normed': True, 'bins': 60, 'alpha': 0.4}
# create the figure
fig = plt.figure(figsize=(16, 25))
for n, feature in enumerate(features):
# add sub plot on our figure
ax = fig.add_subplot(features.shape[1] // 5 + 1, 6, n + 1)
# define range for histograms by cutting 1% of data from both ends
min_value, max_value = numpy.percentile(data[feature], [1, 99])
ax.hist(data.ix[data.is_true_seed.values == 0, feature].values, range=(min_value, max_value),
label='ghost', **hist_params)
ax.hist(data.ix[data.is_true_seed.values == 1, feature].values, range=(min_value, max_value),
label='true', **hist_params)
ax.legend(loc='best')
ax.set_title(feature)
以上代码产生如下输出(仅附上其中的一部分):
DataFrame.boxplot()
很好地自动化了这个:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({'is_true_seed': np.random.choice([True, False], 10),
'col1': np.random.normal(size=10),
'col2': np.random.normal(size=10),
'col3': np.random.normal(size=10)})
is_true_seed col1 col2 col3
0 False -0.990041 -0.561413 -0.512582
1 False 0.825099 0.827453 -0.366211
2 True 0.083442 -1.199540 0.345792
3 True 0.065715 1.560029 -0.324501
4 True -1.699770 -0.270820 -1.380125
ax = df.boxplot(['col1', 'col2', 'col3'], 'is_true_seed', figsize=(10, 10))
第一个参数告诉 pandas 要绘制哪些列,第二个参数告诉要按哪一列分组(您称之为分离条件),第三个参数要在哪个轴上绘制。
列出除要作为分组依据的列之外的所有列可能会很乏味,但您可以通过省略第一个参数来避免这种情况。然后您必须明确命名其他两个:
ax = df.boxplot(by='is_true_seed', figsize=(10, 10))
如果您想为每列创建一个单独的图,那么您可以遍历每一列并使用 plt.figure()
为每个图创建一个新图形。
import matplotlib.pyplot as plt
for column in df:
plt.figure()
df.boxplot([column])
如果您只想将所有列放入同一个箱线图中,那么您可以使用 df.plot(kind='box')