简体   繁体   中英

Having just one legend when using matplotlib zoomed_inset_axes

I'd like to make a fairly straight forward plot with 4 sets of data, two in the main plot, and two in the insert (on a different scale).

This is the starting code::

w1snr_limit = np.arange(0, 50, 0.5)
w2snr_limit = np.arange(0, 50, 0.5)
w3snr_limit = np.arange(0, 10, 0.5)
w4snr_limit = np.arange(0, 10, 0.5)

w1snr_percent = w1snr_limit**(1/2.)
w2snr_percent = w2snr_limit**(1/2.)
w3snr_percent = w3snr_limit**(1/3.)
w4snr_percent = w4snr_limit**(1/4.)

fig, ax = plt.subplots(figsize=(8.0, 8.0))
xmin =   0.00 
xmax =  50.00
ymin =   0.00  
ymax = 100.00

ax.scatter(w1snr_limit, w1snr_percent, s=ms, alpha=0.85, label='W1 SNR')
ax.scatter(w2snr_limit, w2snr_percent, s=ms, alpha=0.85, label='W2 SNR')

ax.set_xlim((xmin, xmax))
ax.set_ylim((ymin, ymax))

Then there would be this inset plot::

axins = zoomed_inset_axes(ax, 2.0,  loc='lower right')
# sub region of the original image
x1, x2, y1, y2 = 0.0, 10., 0.0, 20.0
axins.set_xlim(x1, x2)
axins.set_ylim(y1, y2)
axins.scatter(w3snr_limit, w3snr_percent, s=ms, alpha=0.85, label='W3 SNR', color='green')
axins.scatter(w4snr_limit, w4snr_percent, s=ms, alpha=0.85, label='W4 SNR', color='red')

But then I first struggle to get the inset plot properly placed (ie the two x-axes are all munched up) and also it's unclear to me how you get all 4 datasets into the same legend

handles, labels = ax.get_legend_handles_labels()

handles = [handles[0], handles[1], handles[2], handles[3]]
labels = [labels[0], labels[1], labels[2],labels[3]]

ax.legend(handles,labels,loc=2)

leads to a

IndexError: list index out of range

error. Little help just to get these things sorted?

  1. Not sure what "properly placed" means, but since the data is in the lower part of the figure, I'd suggest to use "upper right" as loc .
  2. You need to supply the handles and labels of both the ax and the axins to the legend to have them all in the legend.

Complete example:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes

w1snr_limit = w2snr_limit = np.arange(0, 50, 0.5)
w3snr_limit = w4snr_limit = np.arange(0, 10, 0.5)

w1snr_percent = w1snr_limit**(1/2.)
w2snr_percent = w2snr_limit**(1/2.)
w3snr_percent = w3snr_limit**(1/3.)
w4snr_percent = w4snr_limit**(1/4.)

fig, ax = plt.subplots(figsize=(8.0, 8.0))
xmin, xmax = 0., 50.
ymin, ymax = 0., 100. 

ax.scatter(w1snr_limit, w1snr_percent, s=6, alpha=0.85, label='W1 SNR')
ax.scatter(w2snr_limit, w2snr_percent, s=6, alpha=0.85, label='W2 SNR')

ax.set_xlim((xmin, xmax))
ax.set_ylim((ymin, ymax))

axins = zoomed_inset_axes(ax, 2.0,  loc='upper right')
# sub region of the original image
x1, x2, y1, y2 = 0.0, 10., 0.0, 20.0
axins.set_xlim(x1, x2)
axins.set_ylim(y1, y2)
axins.scatter(w3snr_limit, w3snr_percent, s=6, alpha=0.85, label='W3 SNR', color='green')
axins.scatter(w4snr_limit, w4snr_percent, s=6, alpha=0.85, label='W4 SNR', color='red')

handles, labels = ax.get_legend_handles_labels()
handles1, labels1 = axins.get_legend_handles_labels()

ax.legend(handles+handles1, labels+labels1, loc=2)

plt.show()

在此处输入图片说明

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