4

For example we plot a line with transparent color here

import numpy as np
import matplotlib.pyplot as plt

a = np.array([1, 2, 3, 4, 5])
b = 2*a
plt.plot(a, b, 'blue', alpha=0.3)
plt.show()

enter image description here

but wenn I plot the same line multiple times, that overlaps with itself, so that the more it overlaps with itself, the darker it becomes.

import numpy as np
import matplotlib.pyplot as plt


a = np.array([1, 2, 3, 4, 5])
b = 2*a
for i in range(3):
    plt.plot(a, b, 'blue', alpha=0.3)
plt.show()

enter image description here

so How can I prevent the color-overlaps?

Thank you all in advance!

Update: Why I need this?

I am doing a Tolerance-Analysis. That means, the parameter change themselfs within a samll range and I will plot the curve for each change. Then I can find the worst case.

enter image description here

If I pick a solid but lighter color. It will looks like:

enter image description here

As you can see, with untransparent color I can not observe the node, which is covered by other line.

Update 2:

enter image description here

sun0727
  • 374
  • 4
  • 19

1 Answers1

2

A single line does not overlay itself. Hence you can concatenate the multiple plots into a single one.

import numpy as np
import matplotlib.pyplot as plt


a = np.array([1, 2, 3, 4, 5])
b = 2*a

A = np.tile(np.append(a,[np.nan]),3)
B = np.tile(np.append(b,[np.nan]),3)

plt.plot(A, B, 'blue', alpha=0.3)
plt.show()

enter image description here

This is essentially the inverse of this question How can I draw transparent lines where the color becomes stronger when they overlap?, where this effect was undesired.

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • thanks for your answer, but it works only for straight line. For Curve the start-point and the end point will be connected with a straight line. I have updated the question – sun0727 Jun 19 '18 at 15:53
  • I updated the answer. However given the updated question, I have problems understanding the issue. You seem to be using two lines per line, one broad transparent one and one narrow, opaque one. Why not simply put the transparent ones in the background? – ImportanceOfBeingErnest Jun 19 '18 at 16:01
  • 1
    Thank you o lot for your nice solution. The broad transparent line is not single line. It is a Area, which contains many transparent lines. The narrow opaque line is the curve with ideal value. and the transparent curves rmean parameter with error. – sun0727 Jun 19 '18 at 16:55
  • Anyways, why not simply plot the "area" behind the ideal value? – ImportanceOfBeingErnest Jun 19 '18 at 16:59
  • The area is constituted by many cuves. How to simply plot the area? My english is bad, please see the updated figure – sun0727 Jun 19 '18 at 17:12
  • 2
    Ah, ok, now I understand the issue. Well in that case, the proposed answer here is probably the better choice indeed. – ImportanceOfBeingErnest Jun 19 '18 at 17:14
  • thanks for your help, another solution is. Find the min and max y-value in each x-value. Then connect all max or min value with line. By this way the area can be outlined. (Envelope?) But I don't know how to implement. – sun0727 Jun 19 '18 at 17:21
  • 1
    If all data of all curves have the same x coordinate, you can concatenate them, find the min and max along the concatenating dimension and use `fill_between` to plot the area. – ImportanceOfBeingErnest Jun 19 '18 at 17:24
  • very nice! you help me again. I will try it soon – sun0727 Jun 19 '18 at 17:42