colorbar change text value matplotlib -


given following code

x = np.random.random((50))*2 -1 y = np.random.random((50)) plt.scatter(x,y,c=x**3,cmap='viridis') cb = plt.colorbar()  #i want smarter (& working) version of cb.ax.set_yticklabels(     [str(np.cbrt(eval(i.get_text()))) in cb.ax.get_yticklabels()]     )  

i want emphasize differences on small scales using x**3 set color in scatterplot, display value of x (not x**3 in shown plot) on colorbar.

this believe can done changing labels using inverse function (here cube-root). problem being matplotlib typically chooses round values, while not, in general, choose such values.

enter image description here

you can using funcformatter matplotlib.ticker module.

for example:

import numpy np import matplotlib.pyplot plt import matplotlib.ticker ticker  x = np.random.random((50))*2 -1 y = np.random.random((50))  plt.scatter(x,y,c=x**3,cmap='viridis',vmin=-1,vmax=1) cb = plt.colorbar()  def label_cbrt(x,pos):     return "{:4.2f}".format(np.cbrt(x))  cb.formatter = ticker.funcformatter(label_cbrt) cb.update_ticks()  plt.show() 

enter image description here


Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -