Is there a way to change the color of an axis (not the ticks) in matplotlib? I have been looking through the docs for Axes, Axis, and Artist, but no luck; the matplotlib gallery also has no hint. Any idea?
Asked
Active
Viewed 1.6e+01k times
4 Answers
222
When using figures, you can easily change the spine color with:
ax.spines['bottom'].set_color('#dddddd')
ax.spines['top'].set_color('#dddddd')
ax.spines['right'].set_color('red')
ax.spines['left'].set_color('red')
Use the following to change only the ticks:
which="both"
changes both the major and minor tick colors
ax.tick_params(axis='x', colors='red')
ax.tick_params(axis='y', colors='red')
And the following to change only the label:
ax.yaxis.label.set_color('red')
ax.xaxis.label.set_color('red')
And finally the title:
ax.title.set_color('red')

Trenton McKinney
- 56,955
- 33
- 144
- 158

SaiyanGirl
- 16,376
- 11
- 41
- 57
-
3`ax.tick_params(axis='x', colors='red')` seems to change the color of both the tick and the label... – Jonathan Feb 16 '16 at 17:00
-
Is it possible to use `ax.yaxis.label.set_color('grey')` in such a way that only the ticks from `y1` to `y2` change their color, and the others remain unaltered? – FaCoffee Mar 31 '16 at 14:27
-
Any Idea on how to change the color of points to be scattered? – Harit Vishwakarma May 11 '16 at 10:34
-
1@FaCoffee You can set the tick label colors independently of the tick color by calling `set_ticklabels()` and passing the `kwarg` `color`. Like so: `ax.xaxis.set_ticklabels([0.0,0.2,0.4,0.6,0.8,1.0], color = 'k')` – marisano Dec 30 '17 at 04:50
-
@Jonathan's comment works for me. It is critical to use the `colors` parameter, not the `color` parameter (note the `s`). – Sia Jun 02 '21 at 19:45
-
Note, that I found `plt.style.use('dark_background')` helpful to do this kind of thing on the fly. – wellplayed Feb 14 '22 at 04:31
26
You can do it by adjusting the default rc settings.
import matplotlib
from matplotlib import pyplot as plt
matplotlib.rc('axes',edgecolor='r')
plt.plot([0, 1], [0, 1])
plt.savefig('test.png')

Mark
- 106,305
- 20
- 172
- 230
-
3Matplotlib also has a [context manager](http://matplotlib.org/users/style_sheets.html#temporary-styling) which allows for temporary changes to the rc parameters http://stackoverflow.com/a/41527038/2166823 – joelostblom Jan 07 '17 at 22:08
-
1Thanks @joelostblom . I am using a dark background on my browser and jupyter notebook. One single line did the whole job. From the docs for context: `import matplotlib.pyplot as plt plt.style.use('dark_background')` – Diego Mello Jun 04 '21 at 01:37
23
For the record, this is how I managed to make it work:
fig = pylab.figure()
ax = fig.add_subplot(1, 1, 1)
for child in ax.get_children():
if isinstance(child, matplotlib.spines.Spine):
child.set_color('#dddddd')

knipknap
- 5,934
- 7
- 39
- 43
4
Setting edge color for all axes globally:
matplotlib.rcParams['axes.edgecolor'] = '#ff0000'

Evgenii
- 36,389
- 27
- 134
- 170