Fat band using matplotlib in python
Asked Answered
E

1

6

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.

Encephalograph answered 9/7, 2014 at 16:37 Comment(2)
I think this is a repeat. Does this: #19391395 not do what you want?Santamaria
@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).Secund
S
8

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()
Secund answered 9/7, 2014 at 17:31 Comment(1)
thank you for the answer. As you mention I need an absolute width and not a width perpendicular to the line !Encephalograph

© 2022 - 2024 — McMap. All rights reserved.