在之前的配对图中,我迭代数据集,并且在同一循环中时,
Matplotlib
为我处理颜色。 ax.bxp()
(文档) 与其他绘图函数的工作方式不同,获取字典列表,因此不需要迭代。
ax.bxp(list_of_dicts # from my DataFrame below
,vert=False
,showfliers=False)
当我迭代箱线图 DataFrame 行并使用
ax.bxp()
时,箱线图会绘制在另一个之上,使其毫无用处。
接下来我应该尝试什么?
用于创建
ax.bxp()
图的示例数据:
q1 med mean q3 whishi whislo my_data_col label
0 73.992 74.000 73.99936 74.006 73.982 74.030 obs1 obs1
1 73.995 74.001 73.99996 74.007 73.967 74.024 obs2 obs2
2 73.993 73.998 74.00100 74.009 73.986 74.021 obs3 obs3
3 73.999 74.005 74.00332 74.007 73.985 74.020 obs4 obs4
4 73.996 74.004 74.00224 74.009 73.984 74.014 obs5 obs5
您可以显式设置颜色,以便可以在直方图和箱线图中重复使用它们。
下面是如何使用批处理
ax.bxp()
并设置颜色的示例。
如果您设法在 for 循环中完成此操作,但只是卡住了框重叠的情况,那么请查看
positions
的 ax.bxp()
arg。
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cbook as cbook
# fake data
np.random.seed(19680801)
data = np.random.lognormal(size=(37, 4), mean=1.5, sigma=1.75)
labels = list("ABCD")
# compute the boxplot stats
stats = cbook.boxplot_stats(data, labels=labels, bootstrap=10000)
for n in range(len(stats)):
stats[n]["med"] = np.median(data)
stats[n]["mean"] *= 2
# draw boxplots
fig, ax = plt.subplots()
bplots = ax.bxp(stats, patch_artist=True)
ax.set_yscale("log")
ax.set_yticklabels([])
# change colors
colors = ["blue", "red", "green", "orange"]
for patch, clr in zip(bplots["boxes"], colors):
patch.set_facecolor(clr)
plt.show()