2015-05-06 3 views
4

Если я создаю цвета, например, путем: импорта NumPy в нп из Matplotlib импорта pyplot как PLTиспользование Matplotlib карта цветов для цветового цикла

n = 6 
color = plt.cm.coolwarm(np.linspace(0.1,0.9,n)) 
color 

цвет является NumPy массив:

array([[ 0.34832334, 0.46571115, 0.88834616, 1. ], [ 0.56518158, 0.69943844, 0.99663507, 1. ], [ 0.77737753, 0.84092121, 0.9461493 , 1. ], [ 0.93577377, 0.8122367 , 0.74715647, 1. ], [ 0.96049006, 0.61627642, 0.4954666 , 1. ], [ 0.83936494, 0.32185622, 0.26492398, 1. ]])

Однако, если я подключаю значения RGB (без альфа-значения 1) в качестве кортежей в файле .mplstyle (map(tuple,color[:,0:-1])), то я получаю ошибку, подобную этой:

in file "/home/moritz/.config/matplotlib/stylelib/ggplot.mplstyle" Key axes.color_cycle: [(0.34832334141176474 does not look like a color arg (val, error_details, msg))

Любые идеи почему?

+0

цвет агд должен начинаться с '(' не с '[(', да? – cphlewis

+0

все еще не работает. Я пробовал ((...), (. ..)); (...), (...); (...) (...) – Moritz

ответ

3

Детали находятся в самом matplotlibrc, на самом деле: ему нужна строка rep (шестнадцатеричная или буква или слово, а не кортеж).

import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib as mpl 

fig, ax1 = plt.subplots(1,1) 

ys = np.random.random((5, 6)) 
ax1.plot(range(5), ys) 
ax1.set_title('Default color cycle') 
plt.show() 

# From the sample matplotlibrc: 
#axes.color_cycle : b, g, r, c, m, y, k # color cycle for plot lines 
              # as list of string colorspecs: 
              # single letter, long name, or 
              # web-style hex 

# setting color cycle after calling plt.subplots doesn't "take" 
# try some hex values as **string** colorspecs 
mpl.rcParams['axes.color_cycle'] = ['#129845','#271254', '#FA4411', '#098765', '#000009'] 

fig, ax2 = plt.subplots(1,1) 
ax2.plot(range(5), ys) 
ax2.set_title('New color cycle') 


n = 6 
color = plt.cm.coolwarm(np.linspace(0.1,0.9,n)) # This returns RGBA; convert: 
hexcolor = map(lambda rgb:'#%02x%02x%02x' % (rgb[0]*255,rgb[1]*255,rgb[2]*255), 
       tuple(color[:,0:-1])) 

mpl.rcParams['axes.color_cycle'] = hexcolor 

fig, ax3 = plt.subplots(1,1) 
ax3.plot(range(5), ys) 
ax3.set_title('Color cycle from colormap') 

plt.show() 

enter image description here enter image description here enter image description here

Смежные вопросы