[PYTHON] I want to make matplotlib a dark theme

By default, matplotlib has a pure white background as shown in the image below, and if you look at it for a long time, it will hurt your eyes, so I want to change it to a different color. It is said that the preset style sheet can be used, so I will write down how to use it easily.

default_sin.png

import

python


import matplotlib.pyplot as plt

Style sheet list

python


plt.style.available

You can get a list of available stylesheets as an array. The contents of the array are as follows.

python


['bmh',
 'classic',
 'dark_background',
 'fast',
 'fivethirtyeight',
 'ggplot',
 'grayscale',
 'seaborn-bright',
 'seaborn-colorblind',
 'seaborn-dark-palette',
 'seaborn-dark',
 'seaborn-darkgrid',
 'seaborn-deep',
 'seaborn-muted',
 'seaborn-notebook',
 'seaborn-paper',
 'seaborn-pastel',
 'seaborn-poster',
 'seaborn-talk',
 'seaborn-ticks',
 'seaborn-white',
 'seaborn-whitegrid',
 'seaborn',
 'Solarize_Light2',
 'tableau-colorblind10',
 '_classic_test']

The black one is cooler, so the dark_background looks good.

Apply style

python


plt.style.use('dark_background')

After applying the style in this one line first, all you have to do is draw a graph or display an image as usual.

Try drawing a sine curve

In such a case, the sine curve and the market price are fixed, so let's draw it and check it.

python


import numpy as np

x = np.linspace(0, 2*np.pi, 200)
y = np.sin(x)

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y)

dark_sin.png

think that's good.

However, I feel that it is a little too black. I'm a little worried that the file name of this image is dark_sin.png and it's too sinful.

Simple self-made style sheet

I didn't intend to make my own style sheet, but I'll play with it a little.

You can find the folder containing the stylesheet by going down to Lib / site-packages / matplotlib / mpl-data / stylelib / from the python folder. There is a file called dark_background.mplstyle in it. Open. The contents are as follows.

dark_background.mplstyle


# Set black background default line colors to white.

lines.color: white
patch.edgecolor: white

text.color: white

axes.facecolor: black
axes.edgecolor: white
axes.labelcolor: white
axes.prop_cycle: cycler('color', ['8dd3c7', 'feffb3', 'bfbbd9', 'fa8174', '81b1d2', 'fdb462', 'b3de69', 'bc82bd', 'ccebc4', 'ffed6f'])

xtick.color: white
ytick.color: white

grid.color: white

figure.facecolor: black
figure.edgecolor: black

savefig.facecolor: black
savefig.edgecolor: black


As you can see, each item is just assigned a color, which is not too difficult. If you replace all the blacks in this file with another slightly milder black, you're likely to get the stylesheet you want. Look at the color code list and select an appropriate color. It looks good around 2e2e2e, so copy this file and replace all black with 2e2e2e and save it in the same folder with a filename such as gray_background.mplstyle.

gray_background.mplstyle


# Set 2e2e2e background default line colors to white.

lines.color: white
patch.edgecolor: white

text.color: white

axes.facecolor: 2e2e2e
axes.edgecolor: white
axes.labelcolor: white
axes.prop_cycle: cycler('color', ['8dd3c7', 'feffb3', 'bfbbd9', 'fa8174', '81b1d2', 'fdb462', 'b3de69', 'bc82bd', 'ccebc4', 'ffed6f'])

xtick.color: white
ytick.color: white

grid.color: white

figure.facecolor: 2e2e2e
figure.edgecolor: 2e2e2e

savefig.facecolor: 2e2e2e
savefig.edgecolor: 2e2e2e


Apply your own style

If you reopen the shell and check with plt.style.available, gray_background is newly added. Plot the sine curve as in the example.

python


import numpy as np

x = np.linspace(0, 2*np.pi, 200)
y = np.sin(x)

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y)

gray_sin.png

Now it's pretty gray.

Even if you want to apply your own style like this, basically, if you make minor changes to your liking by referring to the preset style sheet, it will not take much effort.

Recommended Posts

I want to make matplotlib a dark theme
I want to make a game with Python
[Python] I want to make a nested list a tuple
I want to manually create a legend with matplotlib
I want to make a blog editor with django admin
I want to make a click macro with pyautogui (desire)
I want to make a click macro with pyautogui (outlook)
I want to make input () a nice complement in python
I want to print in a comprehension
I want to build a Python environment
I want to make an automation program!
I tried to make a Web API
I want to embed Matplotlib in PySimpleGUI
[Introduction] I want to make a Mastodon Bot with Python! 【Beginners】
I want to make a parameter list from CloudFormation code (yaml)
[Python] I want to make a 3D scatter plot of the epicenter with Cartopy + Matplotlib!
I want to easily create a Noise Model
I want to INSERT a DataFrame into MSSQL
(Matplotlib) I want to draw a graph with a size specified in pixels
I want to create a window in Python
I want to display multiple images with matplotlib.
I don't want to take a coding test
I want to make fits from my head
I want to create a plug-in type implementation
I read "How to make a hacking lab"
I want to easily find a delicious restaurant
I want to make C ++ code from Python code!
I want to write to a file with Python
I tried to make a ○ ✕ game using TensorFlow
I want to make a web application using React and Python flask
I want to upload a Django app to heroku
I want to make a music player and file music at the same time
I tried to make a "fucking big literary converter"
I want to embed a variable in a Python string
I want to easily implement a timeout in python
I want to iterate a Python generator many times
I want DQN Puniki to hit a home run
100 image processing knocks !! (021-030) I want to take a break ...
I want to give a group_id to a pandas data frame
I want to generate a UUID quickly (memorandum) ~ Python ~
I want to transition with a button in flask
I want to climb a mountain with reinforcement learning
I want to write in Python! (2) Let's write a test
I want to find a popular package on PyPi
I want to randomly sample a file in Python
I want to easily build a model-based development environment
I want to work with a robot in python.
I want to split a character string with hiragana
I want to install a package of Php Redis
I want to send a business start email automatically
I want to run a quantum computer with Python
I want to bind a local variable with lambda
I want a mox generator
I want to solve Sudoku (Sudoku)
I want a mox generator (2)
[Mac] I want to make a simple HTTP server that runs CGI with Python
I want to start a jupyter environment with one command
[Python] I want to get a common set between numpy
I want to start a lot of processes from python
I want to automatically generate a modern metal band name
NikuGan ~ I want to see a lot of delicious meat! !!