Python matplotlib.colors 模块,hex2color() 实例源码
我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用matplotlib.colors.hex2color()。
def linear_gradient(start_hex, finish_hex="#FFFFFF", n=10):
"""Returns a gradient list of (n) colors between
two hex colors. start_hex and finish_hex
should be the full six-digit color string,
including the number sign ("#FFFFFF")
Code from http://bsou.io/posts/color-gradients-with-python
"""
# Starting and ending colors in RGB form
s = hex2color(start_hex)
f = hex2color(finish_hex)
# Initialize a list of the output colors with the starting color
RGB_list = [rgb2hex(s)]
# Calcuate a color at each evenly spaced value of t from 1 to n
for t in range(1, n):
# Interpolate RGB vector for color at the current value of t
curr_vector = [s[j] + (t / (n - 1)) * (f[j] - s[j])
for j in range(3)]
# Add it to our list of output colors
RGB_list.append(rgb2hex(curr_vector))
return RGB_list
def diffcolorhex(rownum):
"""
uses predefined lookup table to return distinct colors
"""
readFile = open('hex_colors.txt', 'r')
sepFile = readFile.read().split('\n')
readFile.close()
x = []
for plotPair in sepFile:
k = list(colors.hex2color(plotPair))
# print(list(k))
x.append(k)
if rownum > len(x):
rownumf = len(x) - 1
print('rownum: %s exceeds length of colors: %s' % (rownum, len(x)))
else:
rownumf = rownum
return (x[rownumf])
def set_styling():
sb.set_style("white")
red = colors.hex2color("#bb3f3f")
blue = colors.hex2color("#5a86ad")
deep_colors = sb.color_palette("deep")
green = deep_colors[1]
custom_palette = [red, blue, green]
custom_palette.extend(deep_colors[3:])
sb.set_palette(custom_palette)
mpl.rcParams.update({"figure.figsize": np.array([6, 6]),
"legend.fontsize": 12,
"font.size": 16,
"axes.labelsize": 16,
"axes.labelweight": "bold",
"xtick.labelsize": 16,
"ytick.labelsize": 16})
def match_colour(ccolor):
# converting hex to rgb
requested_color = colors.hex2color(ccolor.upper())
lab = rgbtolab(requested_color)
return lab
def closest_colour(requested_colour, dfs):
min_colours = {}
requested_color = colors.hex2color(requested_colour)
requested_color = rgbtolab(requested_color)
for key, color_shade, code, color_base in zip(dfs['hex_to_rgb'] \
, dfs['Color_shade'], dfs['Hex_code'], dfs['Color_category']):
r_c, g_c, b_c = map(float, key)
rd = abs(r_c - float(requested_color[0]))
gd = abs(g_c - float(requested_color[1]))
bd = abs(b_c - float(requested_color[2]))
min_colours[sqrt(rd + gd + bd)] = [color_shade, color_base]
return min_colours[min(min_colours.keys())]
def get_line_color(ix, modifier=None):
colour = _lines_colour_cycle[ix]
if modifier=='dark':
return tuple(c/2 for c in colors.hex2color(colour))
elif modifier=='light':
return tuple(1-(1-c)/2 for c in colors.hex2color(colour))
elif modifier is not None:
raise NotImplementedError(modifier)
return colors.hex2color(colour)