Skip to content

Instantly share code, notes, and snippets.

@cjayb
Created March 23, 2015 05:24
Show Gist options
  • Save cjayb/3c711de1aa6edc3a4db0 to your computer and use it in GitHub Desktop.
Save cjayb/3c711de1aa6edc3a4db0 to your computer and use it in GitHub Desktop.
Named colors in matplotlib
# copy from http://stackoverflow.com/questions/22408237/named-colors-in-matplotlib
# credits to BoshWash
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.colors as colors
import math
fig = plt.figure()
ax = fig.add_subplot(111)
ratio = 1.0 / 3.0
count = math.ceil(math.sqrt(len(colors.cnames)))
x_count = count * ratio
y_count = count / ratio
x = 0
y = 0
w = 1 / x_count
h = 1 / y_count
for c in colors.cnames:
pos = (x / x_count, y / y_count)
ax.add_patch(patches.Rectangle(pos, w, h, color=c))
ax.annotate(c, xy=pos)
if y >= y_count-1:
x += 1
y = 0
else:
y += 1
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment