Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
118 views
in Technique[技术] by (71.8m points)

python - LinearSegmentedColormap to list

I have LinearSegmentedColormap:

<matplotlib.colors.LinearSegmentedColormap at 0x7f613c621c70>

and I'm interested in extracting the color names, preferably in a list.

Is that possible?

question from:https://stackoverflow.com/questions/65927253/linearsegmentedcolormap-to-list

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Most colors don't have a specific name, but you can use the hexadecimal format as a string representation.

Here is an example extracting the colors into a list:

import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import numpy as np

cdict = {'red': [(0.0, 0.0, 0.0),
                 (0.5, 1.0, 1.0),
                 (1.0, 1.0, 1.0)],
         'green': [(0.0, 0.0, 0.0),
                   (0.25, 0.0, 0.0),
                   (0.75, 1.0, 1.0),
                   (1.0, 1.0, 1.0)],
         'blue': [(0.0, 0.0, 0.0),
                  (0.5, 0.0, 0.0),
                  (1.0, 1.0, 1.0)]}
mycmap = mcolors.LinearSegmentedColormap('my_colormap', cdict, 256)
color_list = [mcolors.rgb2hex(mycmap(i)) for i in range(mycmap.N)]

fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(16, 4))
scat = ax1.scatter(*np.random.rand(2, 500), c=np.random.rand(500), cmap=mycmap)
ax1.set_title('using the colormap')
plt.colorbar(scat, ax=ax1)
for i, color in enumerate(color_list):
    ax2.plot([0, 1], [i / 256, i / 256], color=color)
ax2.set_title('using the list of colors')
plt.show()

comparison plot

In this case the list of colors looks like:

['#000000', '#020000', '#040000', '#060000', '#080000', '#0a0000', '#0c0000', '#0e0000', '#100000', '#120000',
 '#140000', '#160000', '#180000', '#1a0000', '#1c0000', '#1e0000', '#200000', '#220000', '#240000', '#260000',
 '#280000', '#2a0000', '#2c0000', '#2e0000', '#300000', '#320000', '#340000', '#360000', '#380000', '#3a0000',
 '#3c0000', '#3e0000', '#400000', '#420000', '#440000', '#460000', '#480000', '#4a0000', '#4c0000', '#4e0000',
 '#500000', '#520000', '#540000', '#560000', '#580000', '#5a0000', '#5c0000', '#5e0000', '#600000', '#620000',
 '#640000', '#660000', '#680000', '#6a0000', '#6c0000', '#6e0000', '#700000', '#720000', '#740000', '#760000',
 '#780000', '#7a0000', '#7c0000', '#7e0000', '#800000', '#820200', '#840400', '#860600', '#880800', '#8a0a00',
 '#8c0c00', '#8e0e00', '#901000', '#921200', '#941400', '#961600', '#981800', '#9a1a00', '#9c1c00', '#9e1e00',
 '#a02000', '#a22200', '#a42400', '#a62600', '#a82800', '#aa2a00', '#ac2c00', '#ae2e00', '#b03000', '#b23200',
 '#b43400', '#b63600', '#b83800', '#ba3a00', '#bc3c00', '#be3e00', '#c04000', '#c24200', '#c44400', '#c64600',
 '#c84800', '#ca4a00', '#cc4c00', '#ce4e00', '#d05000', '#d25200', '#d45400', '#d65600', '#d85800', '#da5a00',
 '#dc5c00', '#de5e00', '#e06000', '#e26200', '#e46400', '#e66600', '#e86800', '#ea6a00', '#ec6c00', '#ee6e00',
 '#f07000', '#f27200', '#f47400', '#f67600', '#f87800', '#fa7a00', '#fc7c00', '#fe7e00', '#ff8001', '#ff8203',
 '#ff8405', '#ff8607', '#ff8809', '#ff8a0b', '#ff8c0d', '#ff8e0f', '#ff9011', '#ff9213', '#ff9415', '#ff9617',
 '#ff9819', '#ff9a1b', '#ff9c1d', '#ff9e1f', '#ffa021', '#ffa223', '#ffa425', '#ffa627', '#ffa829', '#ffaa2b',
 '#ffac2d', '#ffae2f', '#ffb031', '#ffb233', '#ffb435', '#ffb637', '#ffb839', '#ffba3b', '#ffbc3d', '#ffbe3f',
 '#ffc041', '#ffc243', '#ffc445', '#ffc647', '#ffc849', '#ffca4b', '#ffcc4d', '#ffce4f', '#ffd051', '#ffd253',
 '#ffd455', '#ffd657', '#ffd859', '#ffda5b', '#ffdc5d', '#ffde5f', '#ffe061', '#ffe263', '#ffe465', '#ffe667',
 '#ffe869', '#ffea6b', '#ffec6d', '#ffee6f', '#fff071', '#fff273', '#fff475', '#fff677', '#fff879', '#fffa7b',
 '#fffc7d', '#fffe7f', '#ffff81', '#ffff83', '#ffff85', '#ffff87', '#ffff89', '#ffff8b', '#ffff8d', '#ffff8f',
 '#ffff91', '#ffff93', '#ffff95', '#ffff97', '#ffff99', '#ffff9b', '#ffff9d', '#ffff9f', '#ffffa1', '#ffffa3',
 '#ffffa5', '#ffffa7', '#ffffa9', '#ffffab', '#ffffad', '#ffffaf', '#ffffb1', '#ffffb3', '#ffffb5', '#ffffb7',
 '#ffffb9', '#ffffbb', '#ffffbd', '#ffffbf', '#ffffc1', '#ffffc3', '#ffffc5', '#ffffc7', '#ffffc9', '#ffffcb',
 '#ffffcd', '#ffffcf', '#ffffd1', '#ffffd3', '#ffffd5', '#ffffd7', '#ffffd9', '#ffffdb', '#ffffdd', '#ffffdf',
 '#ffffe1', '#ffffe3', '#ffffe5', '#ffffe7', '#ffffe9', '#ffffeb', '#ffffed', '#ffffef', '#fffff1', '#fffff3',
 '#fffff5', '#fffff7', '#fffff9', '#fffffb', '#fffffd', '#ffffff']

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...