Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1defb57

Browse files
timhoffmrcomer
andauthored
Simplify example: Box plots with custom fill colors (#27781)
Simplify example: Box plots with custom fill colorsThe [original example](https://matplotlib.org/3.8.0/gallery/statistics/boxplot_color.html) was more complex than need be:- The two plots only differed in the `notch=True` parameter, which is irrelevant for custom fill colors -> reduce to one- rework to a more realistic-like plot---------Co-authored-by: Ruth Comer <10599679+rcomer@users.noreply.github.com>
1 parentf048468 commit1defb57

File tree

1 file changed

+20
-38
lines changed

1 file changed

+20
-38
lines changed

‎galleries/examples/statistics/boxplot_color.py

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,52 +3,34 @@
33
Box plots with custom fill colors
44
=================================
55
6-
This plot illustrates how to create two types of box plots
7-
(rectangular and notched), and how to fill them with custom
8-
colors by accessing the properties of the artists of the
9-
box plots. Additionally, the ``labels`` parameter is used to
10-
provide x-tick labels for each sample.
11-
12-
A good general reference on boxplots and their history can be found
13-
here: http://vita.had.co.nz/papers/boxplots.pdf
6+
To color each box of a box plot individually:
7+
8+
1) use the keyword argument ``patch_artist=True`` to create filled boxes.
9+
2) loop through the created boxes and adapt their color.
1410
"""
1511

1612
importmatplotlib.pyplotasplt
1713
importnumpyasnp
1814

19-
# Random test data
2015
np.random.seed(19680801)
21-
all_data= [np.random.normal(0,std,size=100)forstdinrange(1,4)]
22-
labels= ['x1','x2','x3']
23-
24-
fig, (ax1,ax2)=plt.subplots(nrows=1,ncols=2,figsize=(9,4))
25-
26-
# rectangular box plot
27-
bplot1=ax1.boxplot(all_data,
28-
vert=True,# vertical box alignment
29-
patch_artist=True,# fill with color
30-
labels=labels)# will be used to label x-ticks
31-
ax1.set_title('Rectangular box plot')
32-
33-
# notch shape box plot
34-
bplot2=ax2.boxplot(all_data,
35-
notch=True,# notch shape
36-
vert=True,# vertical box alignment
37-
patch_artist=True,# fill with color
38-
labels=labels)# will be used to label x-ticks
39-
ax2.set_title('Notched box plot')
16+
fruit_weights= [
17+
np.random.normal(130,10,size=100),
18+
np.random.normal(125,20,size=100),
19+
np.random.normal(120,30,size=100),
20+
]
21+
labels= ['peaches','oranges','tomatoes']
22+
colors= ['peachpuff','orange','tomato']
23+
24+
fig,ax=plt.subplots()
25+
ax.set_ylabel('fruit weight (g)')
26+
27+
bplot=ax.boxplot(fruit_weights,
28+
patch_artist=True,# fill with color
29+
labels=labels)# will be used to label x-ticks
4030

4131
# fill with colors
42-
colors= ['pink','lightblue','lightgreen']
43-
forbplotin (bplot1,bplot2):
44-
forpatch,colorinzip(bplot['boxes'],colors):
45-
patch.set_facecolor(color)
46-
47-
# adding horizontal grid lines
48-
foraxin [ax1,ax2]:
49-
ax.yaxis.grid(True)
50-
ax.set_xlabel('Three separate samples')
51-
ax.set_ylabel('Observed values')
32+
forpatch,colorinzip(bplot['boxes'],colors):
33+
patch.set_facecolor(color)
5234

5335
plt.show()
5436

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp