简体   繁体   中英

Change matplotlib grid color with rcParams

How is this done? mpl.rcParams['grid.color'] doesn't work.

Default is white:

import matplotlib.pyplot as plt
import matplotlib as mpl

plt.plot([1, 2])

原图,白色网格

And changing with plt.grid works fine:

plt.plot([1, 2])
plt.grid(c='black')

黑色网格

But not rcParams :

mpl.rcParams['grid.color'] = 'black'
plt.plot([1, 2])

仍然是白色的网格

您首先要设置网格,然后确定其颜色

mpl.rcParams.update({"axes.grid" : True, "grid.color": "black"})

You should add plt.grid() in your second example.

Like this :

import matplotlib.pyplot as plt
import matplotlib as mpl

plt.plot([1, 2])
mpl.rcParams['grid.color'] = 'black'
plt.grid()

在此处输入图片说明

And further idea: You can also try to use seaborn, it is build on top of matplotlib and has really nice formatations:

import matplotlib.pyplot as plt
import seaborn as sns
sns.set()

plt.plot([1, 2])

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM