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

Commit0d402af

Browse files
QuLogicmeeseeksmachine
authored andcommitted
Backport PR#24880: Minor cleanups to named colors example.
1 parenta886617 commit0d402af

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

‎examples/color/named_colors.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717
on some common color categories.
1818
"""
1919

20+
importmath
21+
2022
frommatplotlib.patchesimportRectangle
2123
importmatplotlib.pyplotasplt
2224
importmatplotlib.colorsasmcolors
2325

2426

25-
defplot_colortable(colors,sort_colors=True,emptycols=0):
27+
defplot_colortable(colors,*,ncols=4,sort_colors=True):
2628

2729
cell_width=212
2830
cell_height=22
@@ -31,16 +33,13 @@ def plot_colortable(colors, sort_colors=True, emptycols=0):
3133

3234
# Sort colors by hue, saturation, value and name.
3335
ifsort_colorsisTrue:
34-
by_hsv=sorted((tuple(mcolors.rgb_to_hsv(mcolors.to_rgb(color))),
35-
name)
36-
forname,colorincolors.items())
37-
names= [nameforhsv,nameinby_hsv]
36+
names=sorted(
37+
colors,key=lambdac:tuple(mcolors.rgb_to_hsv(mcolors.to_rgb(c))))
3838
else:
3939
names=list(colors)
4040

4141
n=len(names)
42-
ncols=4-emptycols
43-
nrows=n//ncols+int(n%ncols>0)
42+
nrows=math.ceil(n/ncols)
4443

4544
width=cell_width*4+2*margin
4645
height=cell_height*nrows+2*margin
@@ -79,14 +78,14 @@ def plot_colortable(colors, sort_colors=True, emptycols=0):
7978
# Base colors
8079
# -----------
8180

82-
plot_colortable(mcolors.BASE_COLORS,sort_colors=False,emptycols=1)
81+
plot_colortable(mcolors.BASE_COLORS,ncols=3,sort_colors=False)
8382

8483
#############################################################################
8584
# ---------------
8685
# Tableau Palette
8786
# ---------------
8887

89-
plot_colortable(mcolors.TABLEAU_COLORS,sort_colors=False,emptycols=2)
88+
plot_colortable(mcolors.TABLEAU_COLORS,ncols=2,sort_colors=False)
9089

9190
#############################################################################
9291
# ----------
@@ -104,7 +103,7 @@ def plot_colortable(colors, sort_colors=True, emptycols=0):
104103
# XKCD colors are supported, but they produce a large figure, so we skip them
105104
# for now. You can use the following code if desired::
106105
#
107-
# xkcd_fig = plot_colortable(mcolors.XKCD_COLORS, "XKCD Colors")
106+
# xkcd_fig = plot_colortable(mcolors.XKCD_COLORS)
108107
# xkcd_fig.savefig("XKCD_Colors.png")
109108
#
110109
# .. admonition:: References

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp