Reputation: 777
I am trying to make a plot that has three scatterplots in it, each showing two kinds of data. I would like to show to colorbars corresponding to this data (i.e., separately for the shades of orange and purples). I know how to make a single plot with multiple colorbars and I know how to make multiple plots with a common colorbar but I can't figure out how to put multiple colorbars on a plot with multiple subplots.
Here is an example for making multiple plots:
import numpy as np
import matplotlib.pyplot as plt
fig, axes = plt.subplots(nrows=1, ncols=3)
images =[]
for i in range(3):
images.append([axes[i].scatter(np.random.random(10), np.random.random(10), c = np.random.random(10), vmin=0, vmax=1, cmap="Purples_r"),
axes[i].scatter(np.random.random(10),np.random.random(10), c =
np.random.random(10), vmin=0, vmax=1, cmap="Oranges_r")])
plt.show()
UPDATE: Adding the following code returns two colorbars:
fig.colorbar(images[0][1], ax=axes, fraction=.05)
fig.colorbar(images[0][2], ax=axes, fraction=.05)
I am assuming that keeping fixed common vmin
and vmax
values for all scatterplots assures that the scale is consistent between plots.
Upvotes: 3
Views: 2832
Reputation: 777
Ok, so it seems like adding the following to the code:
fig.colorbar(images[0][0], ax=axes, fraction=.05)
fig.colorbar(images[0][1], ax=axes, fraction=.05)
I tried this yesterday and it wasn't working, I must have had something in my notebook memory.
And just for completeness, here is the complete code:
import numpy as np
import matplotlib.pyplot as plt
fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(12, 3))
images =[]
for i in range(3):
images.append([axes[i].scatter(np.random.random(10), np.random.random(10),
c = np.random.random(10), vmin=0, vmax=1, cmap="Purples_r"),
axes[i].scatter(np.random.random(10),np.random.random(10),
c = np.random.random(10), vmin=0, vmax=1, cmap="Oranges_r")])
fig.colorbar(images[0][0], ax=axes, fraction=.05)
fig.colorbar(images[0][1], ax=axes, fraction=.05)
plt.show()
and here is a visual of what I was looking for:
Upvotes: 0
Reputation: 7361
If you want to add a colorbar for each subplot, add a call to fig.colorbar
inside the for
loop. This may be useful if the data in each subplot are not in the same range. For example, in subplot 1 they span from 0 to 1, in subplot 2 from 0 to 2, etc.
Here an example:
fig, axes = plt.subplots(nrows=1, ncols=3)
images =[]
for i in range(3):
images.append([axes[i].scatter(np.random.random(10), np.random.random(10), c =
(1+i)*np.random.random(10), vmin=0, vmax=1+i, cmap="Purples_r"),
axes[i].scatter(np.random.random(10),np.random.random(10), c =
(1+i)*np.random.random(10), vmin=0, vmax=1+i, cmap="Oranges_r")])
fig.colorbar(images[-1][0], ax=axes[i])
fig.colorbar(images[-1][1], ax=axes[i])
plt.show()
To make something a bit nicer, it's better to place each colorbar in its own axis.
wrl = [1, 4, 1] * 3
fig, axes = plt.subplots(nrows=1, ncols=9, gridspec_kw={'width_ratios': wrl})
images =[]
for i in range(3):
leftaxpos = i*3
plotpos = (i*3)+1
rightaxpos = (i*3)+2
images.append([axes[plotpos].scatter(np.random.random(10), np.random.random(10), c =
(1+i)*np.random.random(10), vmin=0, vmax=1+i, cmap="Purples_r"),
axes[plotpos].scatter(np.random.random(10),np.random.random(10), c =
(1+i)*np.random.random(10), vmin=0, vmax=1+i, cmap="Oranges_r")])
fig.colorbar(images[-1][0], cax=axes[leftaxpos])
fig.colorbar(images[-1][1], cax=axes[rightaxpos])
plt.show()
Upvotes: 1