To avoid spending time coloring shapes drawn with python

Coloring shapes with matplotlib

When drawing multiple shapes with matplotlib I tried to find out how to not specify "red" or "blue" one by one.

Introduction Drawing a shape using matplotlib fill

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


#Graphic data creation
#Base rectangle
x=np.array([-0.5,0.5,0.5,-0.5])
y=np.array([0,0,1,1])

#Prepare N squares
N=3
#Number of columns
cn=10
xl=list()
yl=list()
for i in range(N):
  xl.append(x+1.2*(i%cn))
  yl.append(y-1.2*int(i/cn))

#Drawing
fig = plt.figure()
ax = fig.add_subplot(111)
plt.axes().set_aspect('equal')

poly = plt.fill(xl[0],yl[0],fc="red")#here"red"What to do if you don't want to write
poly = plt.fill(xl[1],yl[1],fc="green")
poly = plt.fill(xl[2],yl[2],fc="blue")

plt.show()

cap1.PNG

As mentioned above, it's fine if there are only 3 types of figures, but I can't think about how many colors to use according to the number of figures, or specify "red", "green", "blue". There must be a good way just by not knowing ...

If you use 10 or less shapes, it is beautiful to use tab10

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

#Base rectangle
x=np.array([-0.5,0.5,0.5,-0.5])
y=np.array([0,0,1,1])

#Prepare N squares
N=10
#Number of columns
cn=10
xl=list()
yl=list()
for i in range(N):
  xl.append(x+1.2*(i%cn))
  yl.append(y-1.2*int(i/cn))

cmap = plt.get_cmap("tab10") #Specify the type of color map here
print(cmap)
#Drawing
fig = plt.figure()
ax = fig.add_subplot(111)
plt.axes().set_aspect('equal')

for xx,yy,cc in zip(xl,yl,range(N)):
  poly = plt.fill(xx,yy,fc=cmap(cc))


cap2.PNG

A fixed color map is prepared in pyplot of matplotlib, You can call it with the get_cmap function. In addition to ** tab10 **, ** Set1 ** seems to be easy to use as a color map type.

Details are described in detail in the following articles. Several ways to specify the Nth color of the color cycle with matplotlib

See the reference for what kind of maps are available. color example code: colormaps_reference.py

What to do if you want to put out even 10 or more beautifully

Try to create your own color map

I may not need more than 10 but I thought about what to do.

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import colorsys

#Base rectangle
x=np.array([-0.5,0.5,0.5,-0.5])
y=np.array([0,0,1,1])

#Prepare N squares
N=20
#Number of columns
cn=5
xl=list()
yl=list()
for i in range(N):
  xl.append(x+1.2*(i%cn))
  yl.append(y-1.2*int(i/cn))

#Color map creation==========================
hexs=list()
for i in range(N):
  rgb=colorsys.hsv_to_rgb(i/N, 1, 1)#(h,s,v)To(r,g,b)Conversion to
  hexs.append(matplotlib.colors.rgb2hex(rgb))#(r,g,b)To a hexadecimal color code

#Drawing
fig = plt.figure()
ax = fig.add_subplot(111)
plt.axes().set_aspect('equal')

for xx,yy,cc in zip(xl,yl,hexs):
  poly = plt.fill(xx,yy,fc=cc)

plt.show()

cap3.PNG

Simple equal division of hue

RGB is familiar with the color, which is expressed by how much the three components of red, green, and blue are mixed, but there is an expression called HSV, which is expressed by hue and brightness again. By dividing the hue into equal parts, try to create a color map of arbitrary size with different colors.

See below for an easy-to-understand explanation of HSV HSV Color Space

Summary

Basically, there seems to be no problem if you enter using tab10. The self-made color map may look beautiful at first glance, I don't think you can tell which one has the lower number when you actually use green.

I want to edit if there is a better way.

(When I was investigating how to create a color scheme that was easy to see, there seems to be a universal design of *** colors.)

Recommended Posts

To avoid spending time coloring shapes drawn with python
How to measure execution time with Python Part 1
How to measure execution time with Python Part 2
How to measure mp3 file playback time with python
Connect to BigQuery with Python
Connect to Wikipedia with Python
Post to slack with Python 3
Execution time measurement with Python With
Switch python to 2.7 with alternatives
Write to csv with Python
Time synchronization (Windows) with Python
I made a package to filter time series with python
Python: How to use async with
Link to get started with python
[Python] Write to csv file with Python
Create folders from '01' to '12' with python
Try to operate Facebook with Python
Output to csv file with Python
Convert list to DataFrame with python
MP3 to WAV conversion with Python
To do tail recursion with Python2
How to get started with Python
Python (from first time to execution)
What to do with PYTHON release?
Unable to install Python with pyenv
How to use FTP with Python
How to calculate date with python
Easily post to twitter with Python 3
I want to debug with Python
How to write offline real time Solve F01 problems with Python
How to calculate "xx time" in one shot with Python timedelta
Challenge to create time axis list report with Toggl API and Python
How to write offline real time I tried to solve E11 with python
How to get the date and time difference in seconds with python
How to draw a vertical line on a heatmap drawn with Python seaborn
How to write offline real time I tried to solve E12 with python
Try to reproduce color film with Python
Try logging in to qiita with Python
Change Python 64bit environment to 32bit environment with Anaconda
English speech recognition with python [speech to text]
HTML email with image to send with python
Memo to ask for KPI with python
Python to remember only with hello, worlds
Output color characters to pretty with python
Introduction to Python Image Inflating Image inflating with ImageDataGenerator
Output Python log to console with GAE
Private Python handbook (updated from time to time)
Convert Excel data to JSON with python
Convert Hiragana to Romaji with Python (Beta)
Fractal to make and play with Python
I wanted to solve ABC160 with Python
Decorator to avoid UnicodeEncodeError in Python 3 print ()
Connect to MySQL with Python within Docker
Single pixel camera to experience with Python
[Python] Introduction to CNN with Pytorch MNIST
Convert FX 1-minute data to 5-minute data with Python
I want to analyze logs with Python
How to do portmanteau test with python
I want to play with aws with python
How to display python Japanese with lolipop
Trying to handle SQLite3 with Python [Note]