Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving "The Economist" Style in matplotlib as default in mplstyle

I created "The Economist" style by modifying the example given here. However, I would like to make this style appear under plt.style.use(your_style). I am having trouble converting in the format it is required. For example, here's my code that creates "The Economist" style:

import matplotlib.pyplot as plt
import numpy as np

x = np.random.randn(1000)
y = np.sin(x)

fig, ax = plt.subplots(facecolor='#CAD9E1', figsize=(12, 10))
ax.set_facecolor('#CAD9E1')
ax.yaxis.grid(color='#ffffff', linewidth=2)
ax.spines['left'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.tick_params(axis='y', length=0)
ax.xaxis.set_ticks_position('bottom')
# Lengthen the bottom x-ticks and set them to dark gray
ax.tick_params(direction='in', axis='x', length=7, color='0.1')
plt.scatter(x, y, color='#006767')

The output is the following:

enter image description here

I opened the default mplstyles that are available and found that I can change the face and grid lines color using the following:

axes.facecolor: cad9e1
grid.color: ffffff

However, I do not know how to implement the rest, for example:

ax.yaxis.grid(color='#ffffff', linewidth=2)
ax.spines['left'].set_visible(False)
ax.tick_params(axis='y', length=0)
ax.xaxis.set_ticks_position('bottom')
ax.tick_params(direction='in', axis='x', length=7, color='0.1')
like image 316
Rohit Avatar asked Oct 15 '25 03:10

Rohit


1 Answers

Most, but not all settings you can do, have an equivalent matplotlib rc paramter. I think here you are lucky, the following would the rc Parameters for the "economist" style in question.

To put them in a file, see the matplotlib customize guide.

import numpy as np
import matplotlib.pyplot as plt

params = {"figure.facecolor": "#cad9e1",
              "axes.facecolor": "#cad9e1",
              "axes.grid" : True,
              "axes.grid.axis" : "y",
              "grid.color"    : "#ffffff",
              "grid.linewidth": 2,
              "axes.spines.left" : False,
              "axes.spines.right" : False,
              "axes.spines.top" : False,
              "ytick.major.size": 0,     
              "ytick.minor.size": 0,
              "xtick.direction" : "in",
              "xtick.major.size" : 7,
              "xtick.color"      : "#191919",
              "axes.edgecolor"    :"#191919",
              "axes.prop_cycle" : plt.cycler('color',
                                    ['#006767', '#ff7f0e', '#2ca02c', '#d62728',
                                     '#9467bd', '#8c564b', '#e377c2', '#7f7f7f',
                                     '#bcbd22', '#17becf'])}
plt.rcParams.update(params)


x = np.random.randn(1000)
y = np.sin(x)

fig, ax = plt.subplots(figsize=(12, 10))
ax.scatter(x, y)

plt.show()

enter image description here

like image 120
ImportanceOfBeingErnest Avatar answered Oct 16 '25 16:10

ImportanceOfBeingErnest