python-3.x - Change the colors of outline and median lines of boxplot in matplotlib
问题描述
I am trying to create boxplot from pandas dataframe using matplotlib.
I am able to get the boxplot and change the linewidth of outlines and median lines, but I would like to change all the colors to black. I tried several ways as described in Pandas boxplot: set color and properties for box, median, mean and Change the facecolor of boxplot in pandas.
However, none of them worked for my case.
Could someone solve these problems?
Here are my codes:
version: python 3.6.5
import pandas as pd
from pandas import DataFrame, Series
import matplotlib.pyplot as plt
df = pd.read_excel("training.xls")
fig = plt.figure()
ax = fig.add_subplot(111)
boxprops = dict(color="black",linewidth=1.5)
medianprops = dict(color="black",linewidth=1.5)
df.boxplot(column=["MEAN"],by="SrcID_Feat",ax=ax,
boxprops=boxprops,medianprops=medianprops)
plt.grid(False)
Result
解决方案
我发现除非我添加参数patch_artist=True
,否则 boxprops 字典都不会对箱线图产生任何影响。例如,当我生成一个将 facecolor 更改为黄色的箱线图时,我必须使用以下编码:
plt.boxplot(boxplot_data, positions=[1], patch_artist=True, boxprops=dict(facecolor='yellow'), showmeans=True)
推荐阅读
- r - 如何向 geom_col 添加额外的 x 和 y 轴?
- python - 使用win32com编译的python程序不断崩溃
- r - 使用 ifelse 和 dplyr 维护 POSIXct 时间格式,并为 true 和 false 返回 POSIXct
- r - R - 在目录中查找列中包含空白条目的文件
- python - 如何在scrapy中使用meta获取所有数据并解析?
- r - R Shiny如何组合数据集的转换
- sql-server - BizTalk SQL 批量插入和选择 - 最佳方式
- javascript - Svelte 十六进制查看器组件中的无限滚动错误
- flutter - 使用构建器将小部件添加到 Listview 不会保留小部件的状态
- c++ - 在这个简单的例子中,为什么 std::optional 的赋值运算符不能在编译时上下文中使用?