[PYTHON] How to assign multiple values to the Matplotlib colorbar

For the time being, I did this. I am changing labels with cbar.ax.set_yticklabelsticks, but not all values apply. I would like to know if there is a formal method.

pic.png

import matplotlib.pyplot as plt
import numpy as np 
plt.rcParams['font.size']=18

#A function that returns the value you want to display from the original value
def convert(org):
    return org*org


rand=np.random.rand(100, 100)*1e4
max=rand.max()
fig = plt.figure(figsize=(7,5))
ax = fig.add_subplot(111)
im=ax.imshow(rand,cmap='gray')

#Color bar from here
cbar=fig.colorbar(im, ax=ax,pad=0.02,shrink=1.0,ticks = [0,max/4,max/2,3*max/4, max])
ticks = cbar.get_ticks()
new_ticks=[]
for t in ticks:
    new_ticks.append(str(t.round(-3))+'/'+str(convert(t).round(-3)))
cbar.ax.set_yticklabels(new_ticks)
plt.show()

Recommended Posts

How to assign multiple values to the Matplotlib colorbar
How to title multiple figures with matplotlib
Match the colorbar to the figure with matplotlib
[Python] How to draw multiple graphs with Matplotlib
How to use Matplotlib
[Python] How to remove duplicate values from the list
[Python] How to output the list values in order
How to use the generator
The road to download Matplotlib
How to use the decorator
How to increase the axis
How to start the program
[python] How to use the library Matplotlib for drawing graphs
How to get all the keys and values in the dictionary
How to turn the for statement when there are multiple values for one key in the dictionary
How to calculate the autocorrelation coefficient
How to use the zip function
How to use the optparse module
How to read the SNLI dataset
How to get the Python version
How to run matplotlib on heroku
[Python] How to import the library
[Python] How to swap array values
How to overwrite the output to the console
How to use the ConfigParser module
How to handle multiple versions of CUDA in the same environment
How to get all the possible values in a regular expression
How to unify the bin width when displaying multiple histograms on top of each other (matplotlib)
How to display the progress bar (tqdm)
How to suppress display error in matplotlib
How to use the Spark ML pipeline
[Python] How to specify the window display position and size of matplotlib
Align the size of the colorbar with matplotlib
How to check the version of Django
How to draw a graph using Matplotlib
How to solve the bin packing problem
How to set the server time to Japanese time
How to manually update the AMP cache
How to utilize multi-core from multiple languages
[Linux] How to use the echo command
How to use the Linux grep command
How to draw a bar graph that summarizes multiple series with matplotlib
How to get colored output to the console
How to operate Linux from the console
How to handle consecutive values in MySQL
How to access the Datastore from the outside
How to use the IPython debugger (ipdb)
How to pass values to JavaScript variables directly from the [Django] template tag
How to turn off the scale value display while leaving the grid with matplotlib
[Python Tips] How to retrieve multiple keys with the maximum value from the dictionary
[Flask + Keras] How to infer multiple models at high speed on the server
Mouse over Matplotlib to display the corresponding image
How to calculate the volatility of a brand
How to use the C library in Python
How to specify the launch browser for JupyterLab 3.0.0
How to find the area of the Voronoi diagram
How to use MkDocs for the first time
How to specify the NIC to scan with amazon-dash
How to avoid writing% matplotlib inline every time
[Python] How to change the date format (display format)
How to make multiple kernels selectable on Jupyter