现在,我想在单独的画布上为每个功能创建箱线图。分离条件为第一列。我有类似的直方图图(下面的代码),但是我不能为盒图制作工作版本。
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)})
fig, ax = plt.subplots(figsize=(10, 10))
df.boxplot(['col1', 'col2', 'col3'], 'is_true_seed', ax)
第一个参数告诉熊猫要绘制哪些列,第二个参数告诉要分组的列(您称为分离条件),第三个参数告诉要绘制的轴。
列出除要分组的列之外的所有列可能会很乏味,但是可以通过省略第一个参数来避免这种情况。然后,您必须明确命名其他两个名称:
df.boxplot(by='is_true_seed', ax=ax)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句