سؤال

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

http://imgur.com/To9D0UT

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.

هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top