This page demonstrates Python tips and tricks that I use in my everyday programming as an atmospheric science graduate student.
-Brian Blaylock

Friday, May 13, 2016

Python Matplotlib available colors

Python's matplotlib defines a bunch of colors you can use in you plots. The way colors look on a computer screen looks different than how it looks when you print it, so you can print out the following page as a reference to what colors are available and what they really look like when printed.

Created using a script from http://stackoverflow.com/questions/22408237/named-colors-in-matplotlib

# plot python colors for printing

import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.colors as colors
import math
import matplotlib
for name, hex in matplotlib.colors.cnames.iteritems():
    print(name, hex)
    
width= 8
height= 11

fig = plt.figure(figsize=(width,height))
ax = fig.add_subplot(111)

ratio = 1.0 / 3.0
count = math.ceil(math.sqrt(len(colors.cnames)))
print count
x_count = count * ratio
y_count = count / ratio
x = 0
y = 0
w = 1 / x_count
h = 1 / y_count

for c in sorted(colors.cnames):
    pos = (x / x_count, y / y_count)
    ax.add_patch(patches.Rectangle(pos, w, h, color=c))
    ax.annotate(c, xy=pos,fontsize=10)
    if y >= y_count-1:
        x += 1
        y = 0
    else:
        y += 1
        

plt.yticks([])
plt.xticks([])
plt.savefig('allcolors',bbox_inches='tight',dpi=300)
#plt.show()

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.