0

I am trying to draw a nice matrix with some columns highlighted. I am very happy with the result of the code below.

import matplotlib.pyplot as plt
import numpy as np

plt.pcolormesh(np.random.rand(10,10), cmap = 'Greys')
plt.axvspan(3,6, color = 'red', alpha = 0.2)
plt.show()

which gives an image, saved to .png, that looks something like

https://i.stack.imgur.com/bmQSw.jpg

this. The problem is that if I save the image as eps, the image becomes much worse, looking like

https://dl.dropboxusercontent.com/u/8162527/figure_1.eps

this. As you can imagine, the second variant is not an option for me.

I tried converting the nice png image to eps, and it sort of works. The problem is the resulting image is not scalable and is 100 times larger (in file size) than the original. I am desperate for ideas.

5xum
  • 5,250
  • 8
  • 36
  • 56
  • It will be worth [reading the answers to this SO question](http://stackoverflow.com/questions/4408813/png-to-eps-conversion-massive-increase-in-file-size) regarding the increase in file size. If you need eps just because it is vector graphics then saving the file as `.svg` produces the transparency correctly. – Greg Jan 14 '14 at 13:00
  • I don't know enough about it to give a definitive answer, but reading http://en.wikipedia.org/wiki/Transparency_%28graphic%29#Transparency_in_PostScript suggests that support for this type of transparency is hit-or-miss in postscript. For what it's worth, it works with SVG and PDF formats. – Warren Weckesser Jan 14 '14 at 13:08
  • Thank you, indeed, saving as .svg produces correct transparencies, however I need an .eps version to put into my LaTeX file. Problem is, when I transform the svg to eps, the image becomes strange. I only tried the transforms with a few online services, do you have any suggestions? – 5xum Jan 14 '14 at 13:09
  • I guess it won't be too helpful to suggest using pdflatex? – Warren Weckesser Jan 14 '14 at 13:17
  • @WarrenWeckesser Not really an option, no. I'm not the main author of the text, so compiling method is not my call :) – 5xum Jan 14 '14 at 13:28

1 Answers1

2

You can directly modify the colors of the grid created by pcolormesh. For example:

import numpy as np
import matplotlib.pyplot as plt

nrows = 10
ncols = 10
a = np.random.rand(nrows, ncols)
pcm = plt.pcolormesh(a, cmap="Greys")

# Apparently need to render once in order to assign facecolors
# to the grid created by pcolormesh:
plt.draw()

fc = pcm.get_facecolors()
fc_grid = fc.reshape(nrows, ncols, -1)

alpha = 0.2
fc_grid[:, 3:6] = (1-alpha)*fc_grid[:, 3:6] + alpha*np.array([1.0, 0, 0, 1])
fc_grid[4:7, 7:] = (1-alpha)*fc_grid[4:7, 7:] + alpha*np.array([0, 1.0, 0, 1])

plt.show()

Here's the PNG version of the plot it creates: grid plot

If you save it as EPS, the colors are correct.

Warren Weckesser
  • 110,654
  • 19
  • 194
  • 214
  • This is the same solution I came to after some thought. The concept of transparency is obviously not to be joined with the eps format. Not the solution I was hoping for, but I guess it will do. Thank you. – 5xum Jan 14 '14 at 14:25
  • The reason you need to render it once is to force the color mapping to happen. A more direct route would be to do all of the color mapping by hand and than imshow on the resulting `[..., 3]` RGB array. – tacaswell Jan 14 '14 at 14:31