简体   繁体   中英

Multiple scatter plots with two colorbars

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 (ie, 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.

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

在此输入图像描述

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:

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM