Matplotlib.colors.to_hex() in Python Last Updated : 21 Apr, 2020 Comments Improve Suggest changes Like Article Like Report Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. matplotlib.colors.to_hex() The matplotlib.colors.to_hex() function is used to convert numbers between 0 to 1 into hex color code. It uses the #rrggbb format if keep_alpha is set to False(its also the default) else it uses #rrggbbaa. Syntax: matplotlib.colors.to_hex(c, keep_alpha=False) Parameters: c: This represents an array of color sequence between 0 to 1. keep_alpha: If set True uses #rrggbbaa format else uses #rrggbb format and it only accepts boolean values. Example 1: Python3 1== import matplotlib.pyplot as plt from matplotlib import colors import numpy as np # dummy data to build the grid data = np.random.rand(10, 10) * 20 # converting into hex color code hex_color=matplotlib.colors.to_hex([ 0.47, 0.0, 1.0 ]) # create discrete colormap cmap = colors.ListedColormap([hex_color, 'green']) bounds = [0,10,20] norm = colors.BoundaryNorm(bounds, cmap.N) fig, ax = plt.subplots() ax.imshow(data, cmap=cmap, norm=norm) # draw gridlines ax.grid(which='major', axis='both', linestyle='-', color='k', linewidth=2) ax.set_xticks(np.arange(-.5, 10, 1)); ax.set_yticks(np.arange(-.5, 10, 1)); plt.show() Output: Example 2: Python3 1== import matplotlib.pyplot as plt from matplotlib import colors import numpy as np # dummy data to build the grid data = np.random.rand(10, 10) * 20 # converting into hex color # code with alpha set to True hex_color = matplotlib.colors.to_hex([ 0.47, 0.0, 1.0, 0.5 ], keep_alpha = True) # create discrete colormap cmap = colors.ListedColormap([hex_color, 'red']) bounds = [0, 10, 20] norm = colors.BoundaryNorm(bounds, cmap.N) fig, ax = plt.subplots() ax.imshow(data, cmap = cmap, norm = norm) # draw gridlines ax.grid(which ='major', axis ='both', linestyle ='-', color ='k', linewidth = 2) ax.set_xticks(np.arange(-.5, 10, 1)); ax.set_yticks(np.arange(-.5, 10, 1)); plt.show() Output: Comment More infoAdvertise with us Next Article Matplotlib.colors.to_hex() in Python R RajuKumar19 Follow Improve Article Tags : Python Write From Home Python-Library Python-matplotlib Practice Tags : python Similar Reads Matplotlib.colors.rgb_to_hsv() in Python Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. matplotlib.colors.rgb_to_hsv() The matplotlib.colors.rgb_to_hsv() function belongs to th 2 min read Matplotlib.colors.hsv_to_rgb() in Python Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. matplotlib.colors.hsv_to_rgb() The matplotlib.colors.hsv_to_rgb() function is used to co 2 min read Matplotlib.colors.to_rgb() in Python Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. matplotlib.colors.to_rgb() The matplotlib.colors.to_rgb() function is used convert c (i 3 min read Matplotlib.colors.to_rgba() in Python Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. matplotlib.colors.to_rgba() The matplotlib.colors.to_rgba() function is used convert c( 3 min read Matplotlib pyplot.colors() In Python, we can plot graphs for visualization using the Matplotlib library. For integrating plots into applications, Matplotlib provides an API. Matplotlib has a module named pyplot which provides a MATLAB-like interface. Matplotlib Add ColorThis function is used to specify the color. It is a do-n 2 min read Like