colorbar - matplotlib standard colormap usage -
i'm using matplotlib 1.3.0 , have following:
import matplotlib.pyplot plt cmap = plt.cm.jet plt.contourf([[.12, .2], [.8, 2]], levels=[0, .1, .3, .5, 1, 3], cmap=cmap, vmin=0, vmax=3) plt.colorbar()
which produces:
the bit don't understand did of other colors go? understand, specifying vmin=0
, vmax=3
color bar should use full range of cmap
in image:
which produced without giving vmin
, vmax
, levels
arguments. so... missing here?
edit 1
in response tom10 & tcaswell. have expected say, but... unfortunately it's not. take @ this:
plt.contourf([[.12, .2], [.8, 3.2]], levels=[0, .1, .3, .5, 1, 3], cmap=cmap, vmin=0, vmax=3) plt.colorbar()
with:
maybe clarify bit: have data , important features of around 0.1, there around 3 let's say. give levels=[0, 0.005, 0.075, 0.1, 0.125, 0.15, 0.2, 1, 2.5, 2.75, 3, 3.25]
, vmin=0, vmax=3.25
. expect see full range of colors, instead of important data-points 0.005 0.125 end in blue region (by using standard plt.cm.jet
color map). i'm saying guess is... if give levels=[0, 1, 2, 3], vmin=0, vmax=3
data goes 0 3 expect see colors in given color map, if give levels=[0, 0.9, 0.1, 0.11, 1, 3], vmi=0, vmax=3
expect same, see colors in given color map, except mapped right intervals, instead see bunch of blues coloring 0-0.11 region , green / yellow coloring other part of region. hope makes it... bit clear.
edit 2
the same happens if don't give norm
or vmin, vmax
.
edit 3
referring tcaswell's comment, behaving way is... me @ least counter-intuitive. expected color independent of data-points in way. expect full range of colors colormap used time (except when vmin, vmax
larger/smaller levels
min, max values). in other words, looking @ code did while (python 3):
import matplotlib.colors mc def addnorm(cmapdata): cmapdata['norm'] = mc.boundarynorm(cmapdata['bounds'], cmapdata['cmap'].n) return true def discretize(cmap, bounds): rescmap = {} rescmap['cmap'] = mc.listedcolormap( \ [cmap(i/len(bounds[1:])) in range(len(bounds[1:]))] ) rescmap['bounds'] = bounds addnorm(rescmap) return rescmap
then use as:
levels = [0, .1, .3, .5, 1, 3] cmapdata = discretize(plt.cm.jet, bounds=levels) plt.contourf([[.12, .2], [.8, 3.2]], levels=levels, cmap=cmapdata['cmap'], norm=cmapdata['norm']) plt.colorbar()
which gives plot can distinguish features (0.1-0.5), i.e. no longer in blue region using above method plt.cm.jet
:
i mean, know solved this, , while too... question guess is... how come default in matplotlib not this? have expected way... or maybe configuration / argument / enable default i'm missing?
after playing around bit seems answer question way easier ever thought. explanation first. while reading documentation on normalizing classes matplotlib.colors
figured... well, matplotlib.colors.boundarynorm
should used here! wrong can see in following example:
import matplotlib.pyplot plt import matplotlib.colors mc levels = [0, .1, .3, .5, 1, 3] norm = mc.boundarynorm(levels, len(levels)-1) plt.contourf([[.12, .2], [.8, 2]], levels=levels, norm=norm) plt.colorbar() plt.show()
which gives this: , don't want! , thinking... why have give constructor of boundarynorm
number of colors use?... shouldn't boundarynorm
use full extent of colormap? , struck me, little change code above:
# use here 256 instead of len(levels)-1 becuase # it's mentioned in documentation # colormaps, default colormaps use 256 colors in # definition: print(plt.cm.jet.n) example norm = mc.boundarynorm(levels, 256)
and get: want!
or can do:
cmap = # user define cmap norm = mc.boundarynorm(levels, cmap.n) # guess little bit more programatically (is word?!) correct
Comments
Post a Comment