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.
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() 

Comments
Post a Comment