2

I have a nrows=2 by ncols=2 subplot. I am trying to make a single orientation='vertical' colorbar, for which the left ticks correspond to the first row and the right ticks correspond to the second row. I cannot figure out how to make two-sided ticks. Below is an example code to make the colorbar.

import numpy as np
import matplotlib.pyplot as plt

# Initialize Data Parameters
xmu, ymu = 5, 50 # Normal Distribution values for first row
xsig, ysig = 2, 10 # Normal Distribution values for second row
size = 100 # Number of datapoints
shp = (10, 10) # shape of Z array
levels1 = np.linspace(0, 10, 11, dtype=int) # ticks of first colorbar
levels2 = np.linspace(20, 80, 11, dtype=int) # ticks of second colorbar

# Initialize Data
x1 = np.random.normal(xmu, xsig, size) # row=1 x col=1
x2 = np.random.normal(xmu, xsig, size) # row=1 x col=2
y1 = np.random.normal(ymu, ysig, size) # row=2 x col=1
y2 = np.random.normal(ymu, ysig, size) # row=2 x col=2
data = [x1, x2, y1, y2]
levels = [levels1, levels1, levels2, levels2]

def get_Z(datum, shape=shp):
    """ returns Z array, applied in zip routine below """
    return datum.reshape(shape)

fig, axes = plt.subplots(nrows=2, ncols=2)
for ax, datum, level in zip(axes.ravel(), data, levels):
    im = ax.imshow(get_Z(datum), vmin=min(level), vmax=max(level))

# create axes for colorbar and set colorbar ticks
cax = fig.add_axes([0.9, 0.1, 0.03, 0.8])
cbar = fig.colorbar(im, cax=cax)
cbar.set_ticks(levels2)
cbar.set_ticklabels(levels2)

plt.show()
plt.close(fig)

Example Subplot

Is there a simple matplotlib way of adding the ticks for the first row and second row to the left and right side of the colorbar, respectively?

(My goal is to apply matplotlib example to correct the values of the colorbar; still playing with that part. Not sure if that's relevant to the problem or not. My actual use case is to make two separate subplots; the first is a subplot of surface maps and the second is a subplot of contour plots.)

  • How is the reader supposed to know that the ticks on the left side of the color bar correspond to one row and the other side to the other row? In other words, why do you feel that you need to do this? – darthbith Oct 15 '18 at 01:04
  • For my subplot, I have two lognormal distributions. I use least-squares and another error function to estimate the optimized parameters. The top row of the subplot corresponds to the least-squares method and the bottom rows correspond to the other error function. I use the first and second data samples for the first and second columns, respectively. The range of Z-values changes by a lot between the first and second rows. I asked because I thought it'd be simpler to do the colorbar this way. I figured out a way to include two colorbars (one per row) as an alternative, but I was just curious. –  Oct 15 '18 at 01:17
  • I suppose it's possible to add other ticks to the left of the colorbar. However, the example code does not seem to reflect the use case. I.e. I would not know which ticks to show on the left in the above example. This makes it kind of hard to provide an answer here. – ImportanceOfBeingErnest Oct 15 '18 at 01:29
  • @ImportanceOfBeingErnest I was able to achieve this using your solution in [this post](https://stackoverflow.com/questions/27151098/draw-colorbar-with-twin-scales/43476118#43476118). –  Oct 15 '18 at 03:02
  • Ok, great, so I marked as duplicate now. – ImportanceOfBeingErnest Oct 15 '18 at 04:01

0 Answers0