5

I would like to plot a line with varying thickness using matplotlib in python.

To be clearer, I have the following variable

import matplotlib.pyplot as P 
import numpy as N

x_value = N.arange(0,10,1)
y_value = N.random.rand(10)
bandwidth = N.random.rand(10)*10
P.plot(x_value,y_value,bandwidth)

I would like to plot a line with x_value and y_value and a thickness that vary with the x_value position and given by the bandwidth vector.

A possible solution that I see would be to draw the upper and lower line (i.e. I take y_value[index] +- bandwidth[index]/2 and plot those two lines.

Then I could try to fill the space between the two lines (how?)

If you have any suggestions?

Thank you,

Samuel.

sponce
  • 1,279
  • 2
  • 11
  • 17
  • I think this is a repeat. Does this: http://stackoverflow.com/questions/19390895/matplotlib-plot-with-variable-line-width not do what you want? – DailRowe Jul 09 '14 at 17:59
  • 1
    @DailRowe: not sure what the OP wants specifically, but a line with variable thickness is different than a variable width band because the line width specifies the width perpendicular to the line, while a band width is specified along the y-axis so its width could be read from the y-values (at least as I've done here). – tom10 Jul 09 '14 at 19:18

1 Answers1

7

You can do this using fill_between.

For example, to have half the bandwidth above and half below (and also drawing the original line using plot):

enter image description here

import matplotlib.pyplot as P 
import numpy as N

x_value = N.arange(0,10,1)
y_value = N.random.rand(10)
bandwidth = N.random.rand(10)*10
print bandwidth
P.fill_between(x_value, y_value+bandwidth/2, y_value-bandwidth/2, alpha=.5)
P.plot(x_value,y_value)
P.show()
tom10
  • 67,082
  • 10
  • 127
  • 137
  • thank you for the answer. As you mention I need an absolute width and not a width perpendicular to the line ! – sponce Jul 09 '14 at 20:35