193

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

366

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? Commented Dec 13, 2017 at 13:45
  • 5
    @AlexSpurling the plt.plot takes keyword arguments for Line2D which the alpha parameter, among others, is a property of.
    – WaterGenie
    Commented May 7, 2019 at 7:57
39

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.

4
  • 32
    All of the maplotlib plotting functions take an alpha parameter directly. You can just do plt.plot(x, y, 'r-', alpha=0.7). Commented Dec 1, 2010 at 0:53
  • 4
    I would be glad to see the updated picture -- could you add it to this answer please? Commented 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
    Commented Oct 14, 2013 at 4:26
  • This is useful if you want to update the alpha after drawing -- e.g. when making some sort of animation. Commented May 3 at 21: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
    Commented Sep 25, 2018 at 12:04

Your Answer

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

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