Python Matplotlib 箱线图颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41997493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python Matplotlib Boxplot Color
提问by user58925
I am trying to make two sets of box plots using Matplotlib. I want each set of box plot filled (and points and whiskers) in a different color. So basically there will be two colors on the plot
我正在尝试使用 Matplotlib 制作两组箱线图。我希望用不同的颜色填充每组箱线图(以及点和胡须)。所以基本上剧情上会有两种颜色
My code is below, would be great if you can help make these plots in color. d0
and d1
are each list of lists of data. I want the set of box plots made with data in d0
in one color, and the set of box plots with data in d1
in another color.
我的代码如下,如果您能帮助制作这些彩色图,那就太好了。d0
和d1
是每个数据列表的列表。我想要一组用d0
一种颜色的数据制作的箱线图,以及一组用d1
另一种颜色的数据制作的箱线图。
plt.boxplot(d0, widths = 0.1)
plt.boxplot(d1, widths = 0.1)
回答by ImportanceOfBeingErnest
To colorize the boxplot, you need to first use the patch_artist=True
keyword to tell it that the boxes are patches and not just paths. Then you have two main options here:
要为箱线图着色,您需要首先使用patch_artist=True
关键字来告诉它这些箱体是补丁而不仅仅是路径。那么你在这里有两个主要选择:
- set the color via
...props
keyword argument, e.g.boxprops=dict(facecolor="red")
. For all keyword arguments, refer to the documentation - Use the
plt.setp(item, properties)
functionality to set the properties of the boxes, whiskers, fliers, medians, caps. - obtain the individual items of the boxes from the returned dictionary and use
item.set_<property>(...)
on them individually. This option is detailed in an answer to the following question: python matplotlib filled boxplots, where it allows to change the color of the individual boxes separately.
- 通过
...props
关键字参数设置颜色,例如boxprops=dict(facecolor="red")
。对于所有关键字参数,请参阅文档 - 使用该
plt.setp(item, properties)
功能设置框、须、传单、中线、帽的属性。 - 从返回的字典中获取盒子的各个项目
item.set_<property>(...)
并单独使用它们。此选项在对以下问题的回答中有详细说明:python matplotlib 填充 boxplots,它允许单独更改单个框的颜色。
The complete example, showing options 1 and 2:
完整示例,显示选项 1 和 2:
import matplotlib.pyplot as plt
import numpy as np
data = np.random.normal(0.1, size=(100,6))
data[76:79,:] = np.ones((3,6))+0.2
plt.figure(figsize=(4,3))
# option 1, specify props dictionaries
c = "red"
plt.boxplot(data[:,:3], positions=[1,2,3], notch=True, patch_artist=True,
boxprops=dict(facecolor=c, color=c),
capprops=dict(color=c),
whiskerprops=dict(color=c),
flierprops=dict(color=c, markeredgecolor=c),
medianprops=dict(color=c),
)
# option 2, set all colors individually
c2 = "purple"
box1 = plt.boxplot(data[:,::-2]+1, positions=[1.5,2.5,3.5], notch=True, patch_artist=True)
for item in ['boxes', 'whiskers', 'fliers', 'medians', 'caps']:
plt.setp(box1[item], color=c2)
plt.setp(box1["boxes"], facecolor=c2)
plt.setp(box1["fliers"], markeredgecolor=c2)
plt.xlim(0.5,4)
plt.xticks([1,2,3], [1,2,3])
plt.show()
回答by Martin Evans
You can change the color of a box plot using setp
on the returned value from boxplot()
as follows:
您可以使用setp
以下返回值更改箱线图的颜色boxplot()
:
import matplotlib.pyplot as plt
def draw_plot(data, edge_color, fill_color):
bp = ax.boxplot(data, patch_artist=True)
for element in ['boxes', 'whiskers', 'fliers', 'means', 'medians', 'caps']:
plt.setp(bp[element], color=edge_color)
for patch in bp['boxes']:
patch.set(facecolor=fill_color)
example_data1 = [[1,2,0.8], [0.5,2,2], [3,2,1]]
example_data2 = [[5,3, 4], [6,4,3,8], [6,4,9]]
fig, ax = plt.subplots()
draw_plot(example_data1, 'red', 'tan')
draw_plot(example_data2, 'blue', 'cyan')
ax.set_ylim(0, 10)
plt.show()
回答by Roman Fursenko
This question seems to be similar to that one (Face pattern for boxes in boxplots) I hope this code solves your problem
这个问题似乎与那个问题相似(箱线图中的框的面部图案)我希望这段代码可以解决您的问题
import matplotlib.pyplot as plt
# fake data
d0 = [[4.5, 5, 6, 4],[4.5, 5, 6, 4]]
d1 = [[1, 2, 3, 3.3],[1, 2, 3, 3.3]]
# basic plot
bp0 = plt.boxplot(d0, patch_artist=True)
bp1 = plt.boxplot(d1, patch_artist=True)
for box in bp0['boxes']:
# change outline color
box.set(color='red', linewidth=2)
# change fill color
box.set(facecolor = 'green' )
# change hatch
box.set(hatch = '/')
for box in bp1['boxes']:
box.set(color='blue', linewidth=5)
box.set(facecolor = 'red' )
plt.show()
回答by Amjad
Change the color of a boxplot
更改箱线图的颜色
import numpy as np
import matplotlib.pyplot as plt
#generate some random data
data = np.random.randn(200)
d= [data, data]
#plot
box = plt.boxplot(d, showfliers=False)
# change the color of its elements
for _, line_list in box.items():
for line in line_list:
line.set_color('r')
plt.show()