When drawing multiple shapes with matplotlib I tried to find out how to not specify "red" or "blue" one by one.
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()
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 ...
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))
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
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()
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
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