Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Pyplot axes text color to white

Tags:

matplotlib

I have a colorbar that I created for a heatmap image of surface temperature on Earth. The problem that I'm having is that the pyplot figure saves with a white background and I have the Earth image on a black background. I set the figure image to be transparent and need to change the text and ticks on the axes to be white.

I've tried everything I've seen on here and searched for hours on the matplotlib site but nothing seems to work for something as simple as changing text color.

a = np.array([[319.785, 198.988]])
plt.figure(figsize=(7, 1))
img = plt.imshow(a)
plt.gca().set_visible(False)

cax = plt.axes([0, .3, 1, 0.5])
cb = plt.colorbar(orientation='horizontal', cax=cax)
plt.savefig("colorbar.png", bbox_inches='tight', transparent=True)
plt.show()
like image 847
Bobby Strickland Avatar asked Oct 19 '25 01:10

Bobby Strickland


1 Answers

You can set the color of the ticks, the color of the labels and the color of the axes edges all to white using the rcParams as follows.

import matplotlib.pyplot as plt
import numpy as np
params = {"ytick.color" : "w",
          "xtick.color" : "w",
          "axes.labelcolor" : "w",
          "axes.edgecolor" : "w"}
plt.rcParams.update(params)

a = np.array([[319.785, 198.988]])
fig  =plt.figure(figsize=(7, 1))
# set facecolor black for testing:
fig.set_facecolor("k")

img = plt.imshow(a)
plt.gca().set_visible(False)

cax = plt.axes([0, .3, 1, 0.5])
cb = plt.colorbar(orientation='horizontal', cax=cax)

plt.show()

enter image description here

like image 102
ImportanceOfBeingErnest Avatar answered Oct 22 '25 05:10

ImportanceOfBeingErnest