[PYTHON] matplotlib Color list creation

Introduction

Nowadays, most of my hobbies and work are done in Python, and graphing is done in Python matplotlib. Colors will be used for the graph to improve the distinctiveness, but I wanted to keep the color list of matplotlib at hand, so I created an image of the color list.

Example

fig_col_mpl.png

How to make

Get a list of colors that can be handled by matplotlib by referring to the following. You can get color names and hexadecimal values in dictionary format.

Names and lists of colors that can be specified in matplotlib

All you have to do now is fill the specified rectangular area with fill.

However, in order to specify the color by referring to this when creating the graph, I want to display the color name and hexadecimal value as text. The problem is the color name and the display color of the hexadecimal text. You can specify them one by one, but I decided to use the HSV value because I could somehow specify the display color automatically.

In my case, hexadecimal is used to specify the display color, but in order to determine the text display color, the hexadecimal strip color is once converted to RGB, then converted to HSV, and the text is displayed based on that value. I tried to decide the color.

The function (def penc ()) that determines the text display color is as follows. colorsys.rgb_to_hsv, So I use it to convert from RGB to HSV.

def penc(hval):
    r=int(hval[1:3],16)
    g=int(hval[3:5],16)
    b=int(hval[6:8],16)
    hsv=colorsys.rgb_to_hsv(r/255, g/255, b/255)
    h=hsv[0]
    s=hsv[1]
    v=hsv[2]
    col='#000000'
    if 0.5<h: col='#ffffff'
    if v<0.75: col='#ffffff'
    return col

In the above example, the text display color is first specified as black (# 000000), but if the HSV H exceeds 0.5 and the V value is less than 0.75, the text display color is white (#ffffff). I try to do it. I thought about using the opposite color, but I was reluctant to make the text display color colorful, so I decided to divide the display color only by black and white.

Full program

import matplotlib
import colorsys
import matplotlib.pyplot as plt
from matplotlib import rcParams
rcParams['font.family'] = 'Ricty Diminished'


def penc(hval):
    #https://docs.python.org/ja/3/library/colorsys.html
    r=int(hval[1:3],16)
    g=int(hval[3:5],16)
    b=int(hval[6:8],16)
    hsv=colorsys.rgb_to_hsv(r/255, g/255, b/255)
    h=hsv[0]
    s=hsv[1]
    v=hsv[2]
    col='#000000'
    if 0.5<h: col='#ffffff'
    if v<0.75: col='#ffffff'
    return col
    

cdic=matplotlib.colors.cnames
lkey=[]
lval=[]
for key, val in zip(cdic.keys(), cdic.values()):
    lkey=lkey+[key]
    lval=lval+[val]
    
xmin=0
xmax=4
k=-1
fnameF='fig_col_mpl.png'
ymin=0
ymax=37
plt.figure(figsize=(6,12))
plt.xlim([xmin,xmax])
plt.ylim([ymax,ymin])
plt.axis('off')
fsize=8 # fontsize
k=-1
for i in range(int(ymin),int(ymax)):
    for j in range(0,4):
        k=k+1
        if k<len(lval):
            xs=float(j)
            xe=xs+1.0
            ys=float(i)
            ye=ys+1.0
            xx=[xs,xe,xe,xs]
            yy=[ys,ys,ye,ye]
            plt.fill(xx,yy,color=lval[k])
            text1=lkey[k]
            text2=lval[k]
            xg=0.5*(xs+xe)
            yg=0.5*(ys+ye)
            col=penc(lval[k])
            plt.text(xg,yg-0.25,text1,rotation=0,ha='center',va='center',fontsize=fsize,color=col)
            plt.text(xg,yg+0.25,text2,rotation=0,ha='center',va='center',fontsize=fsize,color=col)
plt.savefig(fnameF, dpi=300, bbox_inches="tight", pad_inches=0)

that's all

Recommended Posts

matplotlib Color list creation
palettable color map list
matplotlib gallery and color map
Gradient color selection with matplotlib
Continuously color with matplotlib scatter plot
Specify the color in the matplotlib 2D map
Flexible animation creation using animation.FuncAnimation of matplotlib
Set the xticklabels color individually with matplotlib