186

I am plotting two similar trajectories in matplotlib and I'd like to plot each of the lines with partial transparency so that the red (plotted second) doesn't obscure the blue.

alt text

EDIT: Here's the image with transparent lines.

alt text

0

3 Answers 3

354

Plain and simple:

plt.plot(x, y, 'r-', alpha=0.7)

(I know I add nothing new, but the straightforward answer should be visible).

2
  • 1
    I can't see where the alpha parameter is documented. Could you update the answer to include where this is documented? Dec 13, 2017 at 13:45
  • 4
    @AlexSpurling the plt.plot takes keyword arguments for Line2D which the alpha parameter, among others, is a property of.
    – WaterGenie
    May 7, 2019 at 7:57
38

After I plotted all the lines, I was able to set the transparency of all of them as follows:

for l in fig_field.gca().lines:
    l.set_alpha(.7)

EDIT: please see Joe's answer in the comments.

3
  • 31
    All of the maplotlib plotting functions take an alpha parameter directly. You can just do plt.plot(x, y, 'r-', alpha=0.7). Dec 1, 2010 at 0:53
  • 4
    I would be glad to see the updated picture -- could you add it to this answer please? Dec 1, 2010 at 12:50
  • 4
    @JoeKington: that seems to composit the lines before applying the transparency. Is there a way to do it the other way around, so that the transparent lines add together to get darker?
    – naught101
    Oct 14, 2013 at 4:26
4

It really depends on what functions you're using to plot the lines, but try see if the on you're using takes an alpha value and set it to something like 0.5. If that doesn't work, try get the line objects and set their alpha values directly.

1
  • 2
    The comment by @joe-kington says all the matplotlib functions take an alpha parameter, is your answer needing a correction or his comment?
    – U3.1415926
    Sep 25, 2018 at 12:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.