194

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

369

Plain and simple:

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

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

Sign up to request clarification or add additional context in comments.

2 Comments

I can't see where the alpha parameter is documented. Could you update the answer to include where this is documented?
@AlexSpurling the plt.plot takes keyword arguments for Line2D which the alpha parameter, among others, is a property of.
40

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 Comments

All of the maplotlib plotting functions take an alpha parameter directly. You can just do plt.plot(x, y, 'r-', alpha=0.7).
I would be glad to see the updated picture -- could you add it to this answer please?
@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?
This is useful if you want to update the alpha after drawing -- e.g. when making some sort of animation.
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 Comment

The comment by @joe-kington says all the matplotlib functions take an alpha parameter, is your answer needing a correction or his comment?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.