Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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])

original plot, white grid

And changing with plt.grid works fine:

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

black grid

But not rcParams:

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

still white grid

like image 640
Max Ghenis Avatar asked Apr 22 '26 10:04

Max Ghenis


2 Answers

You would first want to set the grid on, then determine its color

mpl.rcParams.update({"axes.grid" : True, "grid.color": "black"})
like image 193
ImportanceOfBeingErnest Avatar answered Apr 23 '26 22:04

ImportanceOfBeingErnest


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()

enter image description here

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])

enter image description here

like image 29
Kolibril Avatar answered Apr 23 '26 22:04

Kolibril



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!