Search code examples
pythonnumpymatplotlibphysicsthickness

Fat band using matplotlib in python


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.


Solution

  • 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()