美文网首页
Python boxplot 设定每个boxplot的颜色

Python boxplot 设定每个boxplot的颜色

作者: 王叽叽的小心情 | 来源:发表于2020-10-22 14:42 被阅读0次

目标:给每个boxplot设定不同的颜色和属性

官方文档:https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.boxplot.html#matplotlib.axes.Axes.boxplot

切入点:可以看出是给在matplotlib.axes.Axes.boxplot中的boxprops进行参数设置,但是前提条件是要将patch_artist=True,将返回的结果按照版块进行绘制,说明文档:If False, produces boxes with the Line2D artist. Otherwise, boxes and drawn with Patch artists.

想法:本来是想在boxprops中的facecolor中传入一个color list,但没成功,貌似在该参数下只能设置相同的颜色(需要求证),所以只能遍历绘图得到的patch,挨个进行设置,主要代码如下:

# 分组箱线图,进行颜色设置
bp = axs.boxplot(y_group, showfliers=False, widths=[i *0.25 for i in pos], 
     labels=None, positions=pos, patch_artist=True)
[bp['boxes'][i].set(facecolor=color_list[i], alpha=0.7) for i in range(N)]
# 其中y_group是一个[[1, 2, 3], [1,2,3,4,5], [8,9]]格式的,即三个箱线图的各自的分组数据,pos是自定义的箱线图的位置,color_list是每个颜色,N是箱线图的数目。
# 当然也可以按照for循环进行挨个设置,也可以列表表达式

其他一些参考资料
箱线图样式设置:https://stackoverflow.com/questions/28740234/face-pattern-for-boxes-in-boxplots
箱线图透明色设置: https://stackoverflow.com/questions/61501795/set-boxplot-boxes-facecolor-transparency-in-python-matplotlib
箱线图颜色填充:https://stackoverflow.com/questions/20289091/python-matplotlib-filled-boxplots

附上一个箱线图的解释


箱线图分位数解释

相关文章

  • Python boxplot 设定每个boxplot的颜色

    目标:给每个boxplot设定不同的颜色和属性 官方文档:https://matplotlib.org/api/_...

  • Python可视化17seborn-箱图boxplot

    本文系统详解利用python中seaborn.boxplot绘制箱图boxplot。seaborn.boxplot...

  • R第十二天

    数据可视化 一、箱线图 boxplot(数据集的变量)——col调颜色 变量~变量 boxplot.stats()...

  • Some useful python code

    创建绘制并保存Python boxplot子图 关闭屏幕显示

  • 2020-06-29

    R语言boxplot绘图函数 boxplot 用于绘制箱线图,我们都知道boxplot 用于展示一组数据的总体分布...

  • ggplot2学习

    ggplot2是个神奇的包,今天刚好帮师兄画一个boxplot图,要求各个boxplot图需要不同的颜色,我们一起...

  • Boxplot

    1.最简单boxplot 2.自定义颜色的boxplots

  • boxplot

    https://biit.cs.ut.ee/clustvis/ https://discuss.analytics...

  • boxplot

  • boxplot

    library(ggplot2) library(magrittr) library(ggpubr) a=read...

网友评论

      本文标题:Python boxplot 设定每个boxplot的颜色

      本文链接:https://www.haomeiwen.com/subject/fjgumktx.html