uhoh
uhoh

Reputation: 3745

How to adjust color of certain features for figure with transparent background

I am saving plots with transparent background to paste on a dark background. I can fix most things, but two things I don't know how to "brighten up" to be visible.

1) The square box of the axes for each of the plots in the figure.

2) That little black "1e11" thing.

I suppose I'd like to do all plots the same if that is simpler, but now my interest is piqued if there is a way to do each separately (e.g. red border for one plot, green for another).

import matplotlib.pyplot as plt
import numpy as np

tau = 2.*np.pi

theta = np.linspace(0, tau)
x = 1.4E+11 + 0.9E+10 * np.cos(theta)
y = 3.3E+10 + 4.5E+09 * np.sin(theta)

fig = plt.figure(figsize=(10,5))
fig.patch.set_alpha(0.0)

ax1 = fig.add_axes([0.08, 0.15, 0.4, 0.8])
ax2 = fig.add_axes([0.55, 0.15, 0.4, 0.8])

grey1 = (0.5, 0.5, 0.5)
grey2 = (0.7, 0.7, 0.7)

ax1.patch.set_facecolor(grey1)
ax1.patch.set_alpha(1.0)

ax1.set_xlabel('time (sec)', fontsize = 16, color=grey2)
ax1.tick_params(axis='both', which='major', labelsize = 16, colors=grey2)

ax1.plot(x, y)

plt.savefig('nicefig')   # don't need: transparent=True
plt.show()

Upvotes: 2

Views: 124

Answers (1)

hitzg
hitzg

Reputation: 12711

The simplest approach is to change the default settings:

import matplotlib.pyplot as plt
import numpy as np

# define colors
grey1 = (0.5, 0.5, 0.5)
grey2 = (0.7, 0.7, 0.7)

plt.rcParams['lines.color'] = grey2
plt.rcParams['xtick.color'] = grey2
plt.rcParams['ytick.color'] = grey2
plt.rcParams['axes.labelcolor'] = grey2
plt.rcParams['axes.edgecolor'] = grey2

# the rest of your code goes here ....

Result:

enter image description here

However, this will apply the changes to all axes. So if you want to keep the standard colors on ax2, you will have to dig into the axes object and find the corresponding artists for all the lines and text elements:

# all your plotting code goes here ...

# change the box color
plt.setp(ax1.spines.values(), color=grey2)

# change the colors of the ticks
plt.setp([ax1.get_xticklines(), ax1.get_yticklines()], color=grey2)

# change the colors of the labels
plt.setp([ax1.xaxis.get_label(), ax1.yaxis.get_label()], color=grey2)

# change the color of the tick labels
plt.setp([ax1.get_xticklabels(), ax1.get_yticklabels()], color=grey2)

# change the color of the axis offset texts (the 1e11 thingy)
plt.setp([ax1.xaxis.get_offset_text(), ax1.yaxis.get_offset_text()], color=grey2)

plt.show()

Result:

enter image description here

Upvotes: 2

Related Questions