Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fill_between in matplotlib does not handle “rasterized=True” option correctly #4837

Closed
ghost opened this issue Jul 31, 2015 · 9 comments
Closed

Comments

@ghost
Copy link

ghost commented Jul 31, 2015

The following script

import matplotlib as mp
import matplotlib.pyplot as plt

ALPHA=0.2
RASTERIZED=True

x = [0, 1, 2, 3]
y1 = [0, 0.1, 0.2, 0.3]
y2 = [0.01, 0.11, 0.21, 0.32]
fig, ax = plt.subplots(figsize=(4,4))
ax.fill_between(x, y1, y2, alpha=ALPHA, color='red', lw=0, rasterized=RASTERIZED)
ax.fill_between(x, list(reversed(y1)), list(reversed(y2)), alpha=ALPHA, color='blue', lw=0,     rasterized=RASTERIZED)
fig.savefig('plot.pdf')
fig.savefig('plot.eps')
fig.savefig('plot.ps')

Generates plots with empty fills, instead of rasterized transparencies.

@tacaswell
Copy link
Member

eps/ps do not support alpha at all. What you are seeing is happening is that the alpha channel is getting dropped so you are seeing just the blue line, with no alpha.

You can get something that looks correct here by doing

import matplotlib as mp
import matplotlib.pyplot as plt

ALPHA=0.2
RASTERIZED=True

x = [0, 1, 2, 3]
y1 = [0, 0.1, 0.2, 0.3]
y2 = [0.01, 0.11, 0.21, 0.32]
fig, ax = plt.subplots(figsize=(4,4))

# note the zorder changes
ax.fill_between(x, y1, y2, alpha=ALPHA, color='red', lw=0, zorder=-5)
ax.fill_between(x, list(reversed(y1)), list(reversed(y2)), alpha=ALPHA, color='blue', lw=0,  zorder=-4)

ax.set_rasterization_zorder(-1)  # this is the important line

fig.savefig('plot.pdf')
fig.savefig('plot.eps')
fig.savefig('plot.ps')
fig.savefig('plot.png')

which will cause all of the artists with a zorder less than -1 to be composited by agg which does know how to deal with alpha.
For me the pdf and png output look correct.

@ghost
Copy link
Author

ghost commented Aug 1, 2015

Doesn't work for me, I'm afraid. Matplotlib version 1.4.2-3.1

@tacaswell
Copy link
Member

Can you post the code and incorrect png output?

@tacaswell
Copy link
Member

Also, try updating to 1.4.3.

@ghost
Copy link
Author

ghost commented Aug 30, 2015

The code:

import matplotlib as mp
import matplotlib.pyplot as plt

ALPHA=0.2
RASTERIZED=True

x = [0, 1, 2, 3]
y1 = [0, 0.1, 0.2, 0.3]
y2 = [0.01, 0.11, 0.21, 0.32]
fig, ax = plt.subplots(figsize=(4,4))
ax.fill_between(x, y1, y2, alpha=ALPHA, color='red', lw=0, zorder=-5)
ax.fill_between(x, list(reversed(y1)), list(reversed(y2)), alpha=ALPHA, color='blue', lw=0, zorder=-4)
ax.set_rasterization_zorder(-1)
#ax.set_title('Plot')
#ax.set_ylabel('Units')
fig.savefig('plot.pdf')
fig.savefig('plot.png')

PNG output:
plot

PDF output looks the same.

The Linux distribution I use does not have 1.4.3, but I'll try to upgrade it manually.

@tacaswell
Copy link
Member

This is fixed in 1.4.3. I just ran your code in a conda environment with both 1.4.2 and 1.4.3:

1.4.3

plot

1.4.2

plot

You can also test at https://try.jupyter.org/

Closing as already fixed.

@ghost
Copy link
Author

ghost commented Aug 30, 2015

OK, thanks.

@tacaswell
Copy link
Member

Thank you for the bug report, sorry we can't do any more for you.

What linux distro do you use? I can make sure they are on the list of people I notify about future releases.

@ghost
Copy link
Author

ghost commented Aug 30, 2015

kubuntu 15.04

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant