3

I'm having a baffling issue with saving figures with a specified line alpha in matplotlib. I would really appreciate any input. I found this previous thread but changing the rasterization does not seem to fix the issue.

Here's the minimal example I have of where this issue shows up:

import numpy as np
import matplotlib.pyplot as plt

arr = np.random.randn(10000, 2)
plt.plot(arr[:,0], arr[:,1],alpha=.09, color='black')
plt.savefig('dots_vector.pdf')
plt.show()

In the the display window, it looks like the following (what I want):

desired figure

However, when I save it as a PDF or PNG, it looks like the following:

sad, incorrect figure

Note, I've tried the following setting:

ax.set_rasterized(True)

As that's what the previous thread suggested, but this does not fix the problem.

Bizarrely, when I change the marker type of the line, to the following:

plt.plot(arr[:,0], arr[:,1], 'o', alpha=.09, color='black')

The alpha seems to behave exactly as expected either as a PDF or a PNG, seen here:

Working figure with dots

Any ideas would be greatly appreciated on how to get the alpha to save properly for the line plot, as I'm pretty stumped.

Community
  • 1
  • 1
  • Did you ever solve this? I'm having exactly the same issue. – S E Clark Aug 20 '16 at 23:36
  • [Here was the best response I could find.](https://www.reddit.com/r/learnpython/comments/477rch/line_alpha_not_saving_properly_in_matplotlib/d0ayti1) It seems like this is a limitation of matplotlib? – Chris Miles Aug 22 '16 at 00:29
  • My problem (with python 3.6.1, matplotlib 3.0.0, run from IDLE under Windows 7) is even worse: Your minimal example looks like your 2nd figure even in the display window. It works with markers. – Rainald62 Oct 22 '18 at 14:46

0 Answers0