Created
March 23, 2015 05:24
-
-
Save cjayb/3c711de1aa6edc3a4db0 to your computer and use it in GitHub Desktop.
Named colors in matplotlib
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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