[英]Why don't colors change in matplotlib boxplot?
我用 matplotlib 做了一个箱线图:
import pandas as pd
import matplotlib.pyplot as plt
A = pd.DataFrame([54.183933149245775,98.14228839908178,97.56790596547185,81.28351460722497,116.36733517668105,93.64706288367272,107.68860349692736,109.65565349602194,88.58717530217115,54.87561132504807,137.89097514410435,116.90021701471281,121.41252555476005,102.68420408219474,107.32642696333856,
120.27307064490907,114.3674635060443,91.38936314166017,149.0476109186976,121.76625219213736,155.6027360469248,115.86331915425764,99.35036421024546,104.93804853361358,115.64286896238708,129.51583078514085,116.30239399660411,97.58582728510798,119.59975852978403,103.68594428632996], columns=['A'])
fig, ax = plt.subplots(1,1)
A.boxplot(grid=False, fontsize=12, notch=True,
flierprops = dict(markersize=10, markeredgecolor ='red', markerfacecolor='b'),
boxprops = dict(linewidth=2, color='red'))
fig.show()
传单道具将改变 colors 和标记大小。 然而,对于“boxprops”,线宽可以改变,但颜色永远不会改变(这里它保持蓝色)。 有人知道为什么吗? 此外,matplotlib 文档在哪里提供这些属性的所有选项?
你可以通过做两件事来做到这一点,
boxplot
的return_type
boxes key
的颜色,如下所示:在这里,我将框改成绿色
import pandas as pd
import matplotlib.pyplot as plt
A = pd.DataFrame([54.183933149245775,98.14228839908178,97.56790596547185,81.28351460722497,116.36733517668105,93.64706288367272,107.68860349692736,109.65565349602194,88.58717530217115,54.87561132504807,137.89097514410435,116.90021701471281,121.41252555476005,102.68420408219474,107.32642696333856,
120.27307064490907,114.3674635060443,91.38936314166017,149.0476109186976,121.76625219213736,155.6027360469248,115.86331915425764,99.35036421024546,104.93804853361358,115.64286896238708,129.51583078514085,116.30239399660411,97.58582728510798,119.59975852978403,103.68594428632996], columns=['A'])
fig, ax = plt.subplots(1,1)
bp = A.boxplot(grid=False, fontsize=12, notch=True,
flierprops = dict(markersize=10, markeredgecolor ='red', markerfacecolor='b'),
boxprops = dict(linewidth=2, color='red'),
return_type='dict') # add this argument
# set the color of the boxes to green
for item in bp['boxes']:
item.set_color('g')
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.