Draw a double-color color panel

I would like to draw a (vertical) color bar that has two different scales (corresponding to two different units for the same quantity) on each side. Think Fahrenheit on the one hand, and Celsius on the other. Obviously, I will need to specify ticks for each side separately.

Any idea how I can do this?

+8
matplotlib colorbar
source share
2 answers

This should start:

import matplotlib.pyplot as plt import numpy as np # generate random data x = np.random.randint(0,200,(10,10)) plt.pcolormesh(x) # create the colorbar # the aspect of the colorbar is set to 'equal', we have to set it to 'auto', # otherwise twinx() will do weird stuff. cbar = plt.colorbar() pos = cbar.ax.get_position() cbar.ax.set_aspect('auto') # create a second axes instance and set the limits you need ax2 = cbar.ax.twinx() ax2.set_ylim([-2,1]) # resize the colorbar (otherwise it overlays the plot) pos.x0 +=0.05 cbar.ax.set_position(pos) ax2.set_position(pos) plt.show() 

enter image description here

+8
source share

If you create a subtask for the color panel, you can create double axes for this subtitle and manipulate it like regular axes.

 import matplotlib.colors as mcolors import matplotlib.pyplot as plt import numpy as np x = np.linspace(-1,2.7) X,Y = np.meshgrid(x,x) Z = np.exp(-X**2-Y**2)*.9+0.1 fig, (ax, cax) = plt.subplots(ncols=2, gridspec_kw={"width_ratios":[15,1]}) im =ax.imshow(Z, vmin=0.1, vmax=1) cbar = plt.colorbar(im, cax=cax) cax2 = cax.twinx() ticks=np.arange(0.1,1.1,0.1) iticks=1./np.array([10,3,2,1.5,1]) cbar.set_ticks(ticks) cbar.set_label("z") cbar.ax.yaxis.set_label_position("left") cax2.set_ylim(0.1,1) cax2.set_yticks(iticks) cax2.set_yticklabels(1./iticks) cax2.set_ylabel("1/z") plt.show() 

enter image description here

+3
source share

All Articles