How to specify colorbar range and keep it regardless of plotting values

你。 提交于 2019-12-24 08:56:41

问题


I typed this up last night then as I was about to submit it I figured it out. Submitting in case anyone else needs it.

I am plotting meteorological values for every hour for multiple days on basemap.

I want to keep the same values of the colorbar at all times for each map. Lets say from 0-10 for each plot.

Sometimes the values are all very close to zero and others they range from 0-10.

for file in files:
    ncfile = Dataset(file)
    cbarticks=np.arange(0.0,10.0,0.5)
    bm.contour(x, y, to_np(energyproduction), 10, colors="black",vmin=0,vmax=10.0)
    bm.contourf(x, y, to_np(energyproduction), 10,cmap = get_cmap('jet'),vmin=0,vmax=10.0)
    plt.colorbar(shrink=.62,ticks=cbarticks)
    plt.show()

I have it set so that the min and max values are always 0 and 10. And that the ticks are always 0-10 by increments of 0.5. How can I force the colorbar to stay the same.

I would prefer that the colorbar always has the same range of


回答1:


The issue was in my contour() and contourf(). Prior I was passing a 10 within the function.

bm.contour(x, y, to_np(energyproduction), 10, colors="black",vmin=0,vmax=10.0)
bm.contourf(x, y, to_np(energyproduction), 10,cmap = get_cmap('jet'),vmin=0,vmax=10.0)

The 10 designation means that there are 10 steps between the min and max value of the plot. So if there are only values of 0-1 you will get .1 increments of plotting contours etc.

Be removing 10 and seeing it to cbarticks, I was able to get the same colorbar values for each plot regardless of the values.

for file in files:
     ncfile = Dataset(file)
     cbarticks=np.arange(0.0,10.0,0.5)
     bm.contour(x, y, to_np(energyproduction), cbarticks, colors="black",vmin=0,vmax=10.0)
     bm.contourf(x, y, to_np(energyproduction), cbarticks, cmap = get_cmap('jet'),vmin=0,vmax=10.0)
     plt.colorbar(shrink=.62,ticks=cbarticks)
     plt.show()

I believe this is the same designation of "levels" within basemap contour.



来源:https://stackoverflow.com/questions/50700325/how-to-specify-colorbar-range-and-keep-it-regardless-of-plotting-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!