python - Matplotlib: use a colormap to show concentration of a process around its mean -


i have pandas dataframe contains 100 realization of given process, observed @ 10 different dates (all realization start same point @ date 0). such dataframe can generated with:

import pandas pd import numpy np nbdates = 10 nbpaths = 100 rnd = np.random.normal(loc=0, scale=1, size=nbpaths*nbdates).reshape(nbdates,nbpaths) sim = dict() sim[0] = [100.0] * nbpaths t in range(nbdates):     sim[t+1] = sim[t] + rnd[t] sim = pd.dataframe(sim) 

now know can plot 100 paths contained in dataframe this

sim.t.plot(legend=false) 

and obtain graph this:

enter image description here

but plot minimum , maximum of process @ every date, , color area between 2 extremas color map reflect concentration of paths in plot (so instance red around mean , gradually cooler go extremes).

i have looked @ using colormaps achieve this, have not managed yet. if knows straightforward way helpful.

thank !

you can (albeit not in elegant way) first making contour plot of "concentration" contourf, making line plot of max & min, , using fill_between method cover unwanted portions of contour plot. proper way mask array in contour plot don't quite have time figure out right (have @ numpy mask array options , take stab @ it). you'd want mask array show between max , min values.

here's example using fill_between instead of masking array:

import matplotlib.pyplot plt import pandas pd import numpy np  ## simulation code here ##  # extract max, min, , mean values each x coordinate minimums = np.array(sim.min()) maximums = np.array(sim.max()) means = np.array(sim.mean())  x = np.array(sim.min().index) y = np.arange(90,111) x, y = np.meshgrid(x, y)  # insert calculation "concentration" in line below z = (maximums[x] - minimums[x]) * y // 100  # set axes matplotlib fig, ax = plt.subplots()  # plot contour, you'll want change levels # depending on range of values in "concentration" ax.contourf(x, y, z, levels=np.arange(0,20,.1), cmap=plt.get_cmap('jet'))  # plot min, max, , mean ax.plot(x, minimums, c='k') ax.plot(x, maximums, c='k') ax.plot(x, means, c='k', lw=2)  # fill space outside min , max white ax.fill_between(x, maximums, 110, color='w') ax.fill_between(x, 90, minimums, color='w') 

this should produce following output:

colormap fill between

you can choose own function calculating "concentration" kind of fill pattern want. 1 in code above intended show how can have depend on both x , y position in plot. luck!


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -