[PYTHON] Let's apply the brand image color to the matplotlib colormap!

Introduction

What colors do you use when drawing graphs with matplotlib? Matplotlib comes with a beautiful colormap by default, I want to use different color variations for presentations, materials and graphs Have you ever wanted to unify the color image?

In this article, the brand color image to the matplotlib colormap I will challenge adaptation!

Class to use

Matplotlib has two types of classes for creating your own colormap.

  1. matplotlib.colors.ListedColormap

Create by setting the color code that composes the colormap with list or array. The constituent colors of the created colormap change discretely.

[Usage example]

import matplotlib as mpl
color_list = ['red', 'green', 'blue']
cmap = mpl.colors.ListedColormap(color_list)

[Created color map] image.png

  1. matplotlib.colors.LinearSegmentedColormap

Create by setting the RGB information that makes up the colormap in the specified data format. The constituent colors of the created colormap change continuously.

[Usage example]

import matplotlib as mpl

#########################################################################
# 
#[Constituent color settings]
#
#The data of the constituent colors is,
# [
#  (x_0, y_0, z_0)
#  ...
#  (x_i, y_i, z_i)
# ]
#The RGB value is set to a value between 0 and 1.
#The constituent colors of colormap are z_0 -> y_1, z_1 -> y_2, ...It changes continuously in the order of.
#Red with the following settings-> green, green ->It means a change of blue.
#
#########################################################################

segment_data = {    
    'red':
        [
            (0.0,   0/255, 255/255),
            (0.5,   0/255,   0/255),
            (1.0,   0/255,   0/255),
        ],
    'green':
        [
            (0.0,   0/255,   0/255),
            (0.5, 128/255, 128/255),
            (1.0,   0/255,   0/255),
        ],
    'blue':
        [
            (0.0,   0/255,   0/255),
            (0.5,   0/255,   0/255),
            (1.0, 255/255,   0/255),
        ],
}
cmap = mpl.colors.LinearSegmentedColormap('colormap_name', segment_data)

With the above implementation, setting the constituent colors is not intuitive and a little difficult, but Using matplotlib.colors.LinearSegmentedColormap.from_list It can be created with a list of color codes.

#When creating with list
import matplotlib as mpl
color_list = ['red', 'green', 'blue']
cmap = mpl.colors.LinearSegmentedColormap.from_list('colormap_name', color_list)

[Created color map] image.png

Implementation

When actually creating your own colormap, use the colormap created with matplotlib.cm.register_cmap. It is convenient to register.

import matplotlib.pyplot as plt
import matplotlib as mpl

def set_custom_colormap(name: str, color_list: list):
    
    """
    color_Convert list to colormap and register it in matplotlib with name.
Two patterns of discrete type and continuous type and four patterns of colormap are registered in reverse order.
    """

    cmap_dis = mpl.colors.ListedColormap(color_list)
    cmap_seq = mpl.colors.LinearSegmentedColormap.from_list(name, color_list)
    
    plt.register_cmap(name + '_dis', cmap_dis)
    plt.register_cmap(name + '_dis_r', cmap_dis.reversed())
    plt.register_cmap(name + '_seq', cmap_seq)
    plt.register_cmap(name + '_seq_r', cmap_seq.reversed())

    return

After that, you can check the color code of the color you want to set.

SENSY colormap

COLORMAP_SOURCE_DICT = {
    
    #Single color gradient
    'sensy_single_1': ['#FFFFFF', '#F4458C'],
    'sensy_single_2': ['#FFFFFF', '#FF6B9A'],
    'sensy_single_3': ['#FFFFFF', '#FA8EB5'],
    'sensy_single_4': ['#FFFFFF', '#A7C0FD'],
    'sensy_single_5': ['#FFFFFF', '#5073ED'],
    'sensy_single_6': ['#FFFFFF', '#4E6FF0'],
    'sensy_single_7': ['#FFFFFF', '#4B4C80'],
    'sensy_single_8': ['#FFFFFF', '#2B2C4B'],
    
    #Multicolor gradient
    'sensy': ['#FF6B9A', '#4E6FF0'],
    'sensy_diverge': ['#FF6B9A', '#FFFFFF', '#4E6FF0'],
    'sensy_accent': ['#5073ED', '#FA8EB5', '#A7C0FD', '#2B2C4B', '#F4458C', '#4B4C80'],

}

#colormap creation&Registration
for name, color_list in COLORMAP_SOURCE_DICT.items():
    set_custom_colormap(name, color_list)


#Initial setting###################

#Initial colormap
DEFAULT_COLORMAP = 'sensy_accent_dis'

#When using matplotlib
mpl.rcParams['image.cmap'] = DEFAULT_COLORMAP

#When using seaborn
import seaborn as sns
sns.set()
sns.set_palette(DEFAULT_COLORMAP)

      

[Color map image] image.png image.png

[Adaptation image]

Graph type image
bar graph image.png
Scatter plot image.png
contour image.png
Heat map image.png

in conclusion

If I knew the color code I wanted to use, I could easily create my own colormap. If you feel unsatisfied or inconsistent with the color variations of the graphs you usually use, It may be fun to make a beautiful colormap by yourself.

reference

Recommended Posts

Let's apply the brand image color to the matplotlib colormap!
Mouse over Matplotlib to display the corresponding image
Dot according to the image
The road to download Matplotlib
Let's cut the face from the image
I want to change the color by clicking the scatter point in matplotlib
Let's activate the camera and apply a mosaic to the human face (OpenCV)
Specify the color in the matplotlib 2D map
Apply the error propagation formula to the standard error
Set the xticklabels color individually with matplotlib
Match the colorbar to the figure with matplotlib
How to assign multiple values to the Matplotlib colorbar
How to calculate the volatility of a brand
Try adding fisheye lens distortion to the image
Introduction to Python Let's prepare the development environment
Crop the image to rounded corners with pythonista
I tried to correct the keystone of the image
Wagtail Recommendation (4) Let's pass the context to the template
Apply Pandas Extraction Basic Grammar / Response to matplotlib
ROS Lecture 119 Correct the color of the camera image