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

Appearance settings

Commit 9ff5564

Browse filesBrowse files
authored
Solved issue of color borders #15614
1 parent 6f303b9 commit 9ff5564
Copy full SHA for 9ff5564

File tree

Expand file treeCollapse file tree

1 file changed

+108
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+108
-0
lines changed

‎examples/color/named_colors.py

Copy file name to clipboard
+108Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
"""
2+
====================
3+
List of named colors
4+
====================
5+
6+
This plots a list of the named colors supported in matplotlib. Note that
7+
:ref:`xkcd colors <xkcd-colors>` are supported as well, but are not listed here
8+
for brevity.
9+
10+
For more information on colors in matplotlib see
11+
12+
* the :doc:`/tutorials/colors/colors` tutorial;
13+
* the `matplotlib.colors` API;
14+
* the :doc:`/gallery/color/color_demo`.
15+
"""
16+
17+
from matplotlib.patches import Rectangle
18+
import matplotlib.pyplot as plt
19+
import matplotlib.colors as mcolors
20+
21+
22+
def plot_colortable(colors, title, sort_colors=True, emptycols=0):
23+
24+
cell_width = 212
25+
cell_height = 22
26+
swatch_width = 48
27+
margin = 12
28+
topmargin = 40
29+
30+
# Sort colors by hue, saturation, value and name.
31+
if sort_colors is True:
32+
by_hsv = sorted((tuple(mcolors.rgb_to_hsv(mcolors.to_rgb(color))),
33+
name)
34+
for name, color in colors.items())
35+
names = [name for hsv, name in by_hsv]
36+
else:
37+
names = list(colors)
38+
39+
n = len(names)
40+
ncols = 4 - emptycols
41+
nrows = n // ncols + int(n % ncols > 0)
42+
43+
width = cell_width * 4 + 2 * margin
44+
height = cell_height * nrows + margin + topmargin
45+
dpi = 72
46+
47+
fig, ax = plt.subplots(figsize=(width / dpi, height / dpi), dpi=dpi)
48+
fig.subplots_adjust(margin/width, margin/height,
49+
(width-margin)/width, (height-topmargin)/height)
50+
ax.set_xlim(0, cell_width * 4)
51+
ax.set_ylim(cell_height * (nrows-0.5), -cell_height/2.)
52+
ax.yaxis.set_visible(False)
53+
ax.xaxis.set_visible(False)
54+
ax.set_axis_off()
55+
ax.set_title(title, fontsize=24, loc="left", pad=10)
56+
57+
for i, name in enumerate(names):
58+
row = i % nrows
59+
col = i // nrows
60+
y = row * cell_height
61+
62+
swatch_start_x = cell_width * col
63+
text_pos_x = cell_width * col + swatch_width + 7
64+
65+
ax.text(text_pos_x, y, name, fontsize=14,
66+
horizontalalignment='left',
67+
verticalalignment='center')
68+
69+
ax.add_patch(
70+
Rectangle(xy=(swatch_start_x, y-9), width=swatch_width,
71+
height=18, facecolor=colors[name], edgecolor='0.7')
72+
)
73+
74+
return fig
75+
76+
plot_colortable(mcolors.BASE_COLORS, "Base Colors",
77+
sort_colors=False, emptycols=1)
78+
plot_colortable(mcolors.TABLEAU_COLORS, "Tableau Palette",
79+
sort_colors=False, emptycols=2)
80+
81+
#sphinx_gallery_thumbnail_number = 3
82+
plot_colortable(mcolors.CSS4_COLORS, "CSS Colors")
83+
84+
# Optionally plot the XKCD colors (Caution: will produce large figure)
85+
#xkcd_fig = plot_colortable(mcolors.XKCD_COLORS, "XKCD Colors")
86+
#xkcd_fig.savefig("XKCD_Colors.png")
87+
88+
plt.show()
89+
90+
91+
#############################################################################
92+
#
93+
# ------------
94+
#
95+
# References
96+
# """"""""""
97+
#
98+
# The use of the following functions, methods, classes and modules is shown
99+
# in this example:
100+
101+
import matplotlib
102+
matplotlib.colors
103+
matplotlib.colors.rgb_to_hsv
104+
matplotlib.colors.to_rgba
105+
matplotlib.figure.Figure.get_size_inches
106+
matplotlib.figure.Figure.subplots_adjust
107+
matplotlib.axes.Axes.text
108+
matplotlib.patches.Rectangle

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.