0

I am using the following code to get this:

image1

However, when saved as .eps, the face color of the figure turns from gray ( what I want) to black. See this:

image2

Any reason why?

dim = np.arange(1, 32, 1)
fig, ax = plt.subplots(figsize=(7,9))
heatmap = ax.imshow(h.T, cmap=plt.cm.get_cmap('Blues', 4), clim=[1,144])
cbaxes = fig.add_axes([.8, .35, .04, .3])
cbar = fig.colorbar(heatmap, ticks = [1, 36, 72, 108, 144], label =  'Number of valid records per day', cax = cbaxes)
ax.set_ylabel("Days", fontsize=15)
ax.set_xlabel("Months", fontsize=15)
ax.set_title("Number of valid records per day", fontsize=20)
ax.set_yticks(range(0,31))
ax.set_yticklabels(dim, ha='center', minor=False, fontsize=12)
ax.set_xticks(range(0,13,1))
ax.set_xticklabels(ylabel[7:20], rotation = 45, ha = 'right')

ax.set_facecolor('gray')
cbar.set_label('Number of valid records')
ax.xaxis.set_minor_locator(MultipleLocator(0.5))
ax.yaxis.set_minor_locator(MultipleLocator(0.5))

ax.tick_params(axis='y', which='major', pad=10)
ax.grid(which = 'minor', color = 'w')

fig.show()
plt.savefig("drive/My Drive/fig.eps")

tmdavison
  • 64,360
  • 12
  • 187
  • 165
nalfahel
  • 145
  • 9
  • This does not have a complete [mre]. Provide data with [How to provide a reproducible copy of your DataFrame using `df.head(30).to_clipboard(sep=',')`](https://stackoverflow.com/q/52413246/7758804), then **[edit] your question**, and paste the clipboard into a code block. – Trenton McKinney Aug 11 '21 at 12:39

1 Answers1

0

Your problem is probably caused by your 'gray' colour actually being black with transparency (which is the a channel in rgba), i.e. the rgba color (0,0,0,.5), which is black with a 50% transparency. If you ran your code interactively, you should have gotten a message:

"The PostScript backend does not support transparency; partially transparent artists will be rendered opaque."

.eps (Postscript in the message refers to encapsulated postscript) doesn't support transparency, I imagine under the hood when your figure is being saved as .eps it simply takes the color channels i.e. the rgb values, which is equivalent to setting alpha to 1.

You can either:

  1. Rasterize your figure/axes before saving as .eps using fig.set_rasterized(True)/ax.set_rasterized(True),
  2. Save in a different format which supports transparency (.pdf, .png etc.)
  3. Choose a color/cmap(colormap) which does not include transparency.

To expand #3 for your specific option, if it's the case that missing values aren't plotted and we're seeing the axes facecolor, you can try explicitly specifying an rgb color ax.set_facecolor(.5,.5,.5) instead of ax.set_facecolor('grey').

Jason
  • 4,346
  • 10
  • 49
  • 75