python - In matplotlib plot cell averages instead of line plot -


given set of n 'co-ordinates', x=[0, 1, 2, 3, 4] , n-1 'values' y = [10, 20, 30, 40] want plot piecewise-constant function equal 10 0 < x <1, equal 20 1 < x < 2 etc.

in other words, x-list contains co-ordinates of cells boundaries while y-list represents value of function inside cells. standard way plot such data?

i interested in non-regular grids , 2d plots.

for 1d data, use bar plot. first argument center of bars, , second height.

x=np.asarray([0, 1, 2, 3, 4]) centers=(x[1:]+x[:-1])/2.0 plt.bar(centers,[10, 20, 30, 40],width=1) 

barplot

for 2d data, can use pcolor , colorbar

# x coordinates of cells x=np.asarray([0, 1, 2, 3, 4]) # y coordinates of cells y=np.asarray([0,1,2]) # values inside cells c = np.asarray([[10, 20, 30, 40], [50,60,70,80]])  # plot cells plt.pcolor(x,y,c) # plot bar match colors values plt.colorbar() 

pcolor

to plot non regular grids, pcolor can take arguments 2d arrays x , y: can specify x , y coordinates each cell.
note pcolormesh plots same thing pcolor faster, can useful.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -