如何调整具有透明背景的图形的某些特征的颜色

时间:2015-06-03 07:21:20

标签: python matplotlib

我正在保存具有透明背景的地块以粘贴在黑暗的背景上。我可以解决大多数事情,但有两件事我不知道如何“照亮”才能看得见。

1)图中每个图的轴的方框。

2)那个小小的黑色“1e11”。

我想如果这更简单,我想把所有的情节都做同样的事情,但是现在如果有办法分别做各自的话,我会感兴趣(例如一个情节的红色边框,另一个情节的绿色)。

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

1 个答案:

答案 0 :(得分:2)

最简单的方法是更改​​默认设置:

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 ....

结果:

enter image description here

但是,这会将更改应用于所有轴。因此,如果您想在ax2上保留标准颜色,则必须深入了解axis对象并找到所有行和文本元素的相应艺术家:

# 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()

结果:

enter image description here