Matplotlib: Changing the color of an axis

MatplotlibColors

Matplotlib Problem Overview


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?

Matplotlib Solutions


Solution 1 - Matplotlib

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

Solution 2 - Matplotlib

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

Solution 3 - Matplotlib

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

Solution 4 - Matplotlib

Setting edge color for all axes globally:

matplotlib.rcParams['axes.edgecolor'] = '#ff0000'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionknipknapView Question on Stackoverflow
Solution 1 - MatplotlibSaiyanGirlView Answer on Stackoverflow
Solution 2 - MatplotlibknipknapView Answer on Stackoverflow
Solution 3 - MatplotlibMarkView Answer on Stackoverflow
Solution 4 - MatplotlibEvgeniiView Answer on Stackoverflow