Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Named colors example #2801

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 14, 2014
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
docstring and pep8
  • Loading branch information
fariza committed Feb 12, 2014
commit 29d8e1eff2a740c5ea03f4d35ba42ed9fb213b3d
48 changes: 27 additions & 21 deletions examples/color/named_colors.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,50 @@
"""
Visualization of named colors.

Simple plot example with the named colors and its visual representation.
"""

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cbook
from matplotlib import colors

from matplotlib import colors

colors_ = colors.cnames.items()
#add the single letter colors

# Add the single letter colors.
for name, rgb in colors.ColorConverter.colors.items():
hex_ = colors.rgb2hex(rgb)
colors_.append((name, hex_))
#hex color values

# Transform to hex color values.
hex_ = [color[1] for color in colors_]
#rgb equivalent
# Get the rgb equivalent.
rgb = [colors.hex2color(color) for color in hex_]
#hsv equivalent
# Get the hsv equivalent.
hsv = [colors.rgb_to_hsv(color) for color in rgb]
#split the hsv to sort

# Split the hsv values to sort.
hue = [color[0] for color in hsv]
sat = [color[1] for color in hsv]
val = [color[2] for color in hsv]
#sort by hue, saturation and value

# Sort by hue, saturation and value.
ind = np.lexsort((val, sat, hue))
sorted_colors = [colors_[i] for i in ind]

n = len(sorted_colors)
ncols = 3
nrows = int(np.ceil(1. * n / ncols))

fig = plt.figure(figsize=(ncols*5, nrows*4))
for i, (name, color) in enumerate(sorted_colors):
ax = fig.add_subplot(nrows, ncols, i + 1,
xticks=[], yticks=[])
ax = fig.add_subplot(nrows, ncols, i + 1)
ax.text(0.55, 0.5, name, fontsize=12,
horizontalalignment='left',
verticalalignment='center')

ax.hlines(0.5, 0, 0.5, color='black', linewidth=9)
horizontalalignment='left',
verticalalignment='center')

# Add extra black line a little bit thicker to make
# clear colors more visible.
ax.hlines(0.5, 0, 0.5, color='black', linewidth=10)
ax.hlines(0.5, 0, 0.5, color=color, linewidth=8)
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
Expand Down