Introducing the color map extension library palettable

Color map extension library palettable

Have you ever wondered, "I wish I had more color maps (or color cycles)" when drawing with matplotlib? Here, we will introduce a convenient extension library that solves such problems. That is ** palettetable **. There are ** 1599 ** color maps available. There are 79 standard color maps for matplotlib, so it's ** about 20 times ** !!.

The color map of matplotlib can be edited and created by yourself in various ways, so it can be said that you do not actually need to rely on the extension library. palettable is just an archive of the many colormaps created in this way.

So, this article is for ** "I want to use more colormaps, but it's a pain to research and create by myself. If you have an easy-to-use extension library, use it!" ** ..

The list of color maps for palettable is summarized in the article ** here ** (I have separated it because there are too many). The original HP (English) is here.

Installation

It can be easily installed with the pip command.

pip install palettable

How to use palettable (color cycle)

matplotlib follows the default color cycle when drawing plots. For example, like this, by default, 10 pieces go around and the 11th piece returns to the original color.

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 2*np.pi, 100) 
fig, ax = plt.subplots()

for i in range(11):
    y = np.sin(x)+i
    ax.plot(x, y)
plt.show()

fig1.png The color cycle used at this time was the default tab10 in matplotlib: tab10.png Now let's change this to a palettable color cycle. This time, I will use palettable.scientific.sequential.Tokyo_10. The color cycle for palettable is palette.scientific.sequential.Tokyo_10.mpl_colors You can get it (just add .mpl_colors to the end). Tokyo_10.png

To change the color cycle, use ʻax.set_prop_cycle () `:

import numpy as np
import matplotlib.pyplot as plt
import palettable #add to

x = np.linspace(0, 2*np.pi, 100) 
fig, ax = plt.subplots()
#Add the following line
ax.set_prop_cycle('color', palettable.scientific.sequential.Tokyo_10.mpl_colors)

for i in range(11):
    y = np.sin(x)+i
    ax.plot(x, y)
plt.show()

The result is like this. It has a specified color cycle, and it goes around with 10 pieces. fig2.png

Here, the palettable.scientific.sequential.Tokyo_10.mpl_colors passed to ʻax.set_prop_cycle ()is just a Pythonlist` object. Contents is like this. It's a standardized 10-by-3 RGB array:

>>> palettable.scientific.sequential.Tokyo_10.mpl_colors                                          
[(0.10196078431372549, 0.054901960784313725, 0.20392156862745098),
 (0.27058823529411763, 0.12549019607843137, 0.2980392156862745),
 (0.43137254901960786, 0.24313725490196078, 0.403921568627451),
 (0.5215686274509804, 0.3686274509803922, 0.47058823529411764),
 (0.5529411764705883, 0.4745098039215686, 0.5098039215686274),
 (0.5725490196078431, 0.5803921568627451, 0.5372549019607843),
 (0.592156862745098, 0.6823529411764706, 0.5686274509803921),
 (0.6549019607843137, 0.807843137254902, 0.615686274509804),
 (0.8352941176470589, 0.9490196078431372, 0.7372549019607844),
 (0.996078431372549, 0.996078431372549, 0.8470588235294118)]

In addition, the number of rounds is determined by the last number of palettable.scientific.sequential.Tokyo_10, and the number that can be specified depends on the color code. For example, in palettable.scientific.sequential.Tokyo_, you can specify from 3 to 20. For example, if you use palettable.scientific.sequential.Tokyo_11 to go around with 11 pieces, it will be as follows: Tokyo_11.png fig3.png I went around with just 11 pieces.

How to use palettable (color map)

It's finally a color map. Actually, this one is easier to use. First, let's make a two-dimensional plot with a simple sample code.

import numpy as np
import matplotlib.pyplot as plt

xx, yy = np.meshgrid(np.linspace(0, 2*np.pi, 100), np.linspace(0, 2*np.pi, 100))
zz = np.sin(xx+yy)

fig, ax = plt.subplots()
mp = ax.imshow(zz, cmap="Blues") #Specify the colormap on this line
fig.colorbar(mp)

fig4.png Now, let's change this color map to the same palettable.scientific.sequential.Tokyo_10 as before. The color map for palettable is palette.scientific.sequential.Tokyo_10.mpl_colormap You can get it (just add .mpl_colormap to the end).

import numpy as np
import matplotlib.pyplot as plt
import palettable #add to

xx, yy = np.meshgrid(np.linspace(0, 2*np.pi, 100), np.linspace(0, 2*np.pi, 100))
zz = np.sin(xx+yy)

fig, ax = plt.subplots()
#Change the cmap option on the next line
mp = ax.imshow(zz, cmap=palettable.scientific.sequential.Tokyo_10.mpl_colormap)
fig.colorbar(mp)

fig5.png The atmosphere looks quite different.

The content of palettable.scientific.sequential.Tokyo_10.mpl_colormap given to the cmap option is the LinearSegmentedColormap object of matplotlib.colors:

>>> palettable.scientific.sequential.Tokyo_10.mpl_colormap
<matplotlib.colors.LinearSegmentedColormap at 0x7fb05793f0d0>

Also, when using the palettable color map, it represents the color resolution in the same way as the last number (_10 part) that indicates the number of color cycles. Let's try comparing _3 and _20: fig6.png The appearance will change considerably. It is a good idea to change the color resolution according to the fluctuation range you want to express.

at the end

The palettable color map list is summarized in the article ** here **. Thank you for reading until the end.

Recommended Posts

Introducing the color map extension library palettable
palettable color map list
Introducing the TestData creation library Mimesis
[Python] Adjusted the color map standard
Specify the color in the matplotlib 2D map
Read the VTK file and display the color map with jupyter.