Changing the color of an axis in Matplotlib


First, we can get the axes. Then, ax.spines could help to set the color by specifying the name of the axes, i.e., top, bottom, right and left.

Steps

  • Add an axes to the current figure and make it the current axes.

  • Using step 1 axes, we can set the color of all the axes.

  • Using ax.spines[axes].set_color(‘color’), set the color of the axes. Axes could be bottom, top, right, and left. Color could be yellow, red, black, and blue.

  • To show the figure, use the plt.show() method.

Example

from matplotlib import pyplot as plt

ax = plt.axes()

ax.spines['bottom'].set_color('yellow')
ax.spines['top'].set_color('red')
ax.spines['right'].set_color('black')
ax.spines['left'].set_color('blue')

plt.show()

Output

Updated on: 16-Mar-2021

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements