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.
import
python
import matplotlib.pyplot as plt
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.
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.
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)
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.
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 black
s 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
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)
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