Continue on from previous question .. Here
This time I need to plot two sets of data with different sizes on the same plot. The issue is, since the two sets of data have different sizes, some points will fall out of index. When I pick on certain point(s), I'll get a warning like this:
File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\cbook\__init__.py", line 224, in process
func(*args, **kwargs)
File "C:\Users\U240335\.spyder-py3\Spyder_Dingyi\untitled0.py", line 42, in onpick
IndexError: index 16 is out of bounds for axis 0 with size 10
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\cbook\__init__.py", line 224, in process
func(*args, **kwargs)
File "C:\Users\U240335\.spyder-py3\Spyder_Dingyi\untitled0.py", line 42, in onpick
IndexError: index 17 is out of bounds for axis 0 with size 10
And I would also like to show the value of the point (including the label) on the plot of which the cursor is clicking on and be able to do something like "clear all" after marking some points on the plot.
Here's the full code:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import random
a = np.random.uniform(0, 20, 30)
b = np.random.uniform(0, 20, 30)
x = np.random.uniform(0, 20, 30)
y = np.random.uniform(0, 20, 30)
z = [0]*10 + [1]*20
random.shuffle(z)
# x y data and legend labels
df1 = pd.DataFrame({"x1": a, "y1": b, 'z': z})
df2 = pd.DataFrame({"x2": x, "y2": y, 'z': z})
x1 = df1['x1'].loc[df1['z']==0].reset_index(drop=True).values
y1 = df1['y1'].loc[df1['z']==0].reset_index(drop=True).values
x2 = df2['x2'].loc[df1['z']==1].reset_index(drop=True).values
y2 = df2['y2'].loc[df1['z']==1].reset_index(drop=True).values
def overlay_plot2(x1,y1,x2,y2,title,xlabel,ylabel):
# define the picker event
def onpick(event):
ind = event.ind
print('\n%s:' % xlabel, x1[ind] if event.artist.get_label() == '0' else x2[ind], # use event.artist.getlabel() when labels are made
'%s:' % ylabel, y1[ind] if event.artist.get_label() == '1' else y2[ind],
event.artist.get_label())
# plot
fig, ax = plt.subplots(figsize=(8, 6), dpi = 120)
ax.scatter(x1, y1, c='b', label = '0',s=14, marker='x', picker=True)
ax.scatter(x2, y2, c='r', label = '1',s=14, marker='o', picker=True)
ax.set_title(title)
ax.set_xlabel(xlabel)
ax.set_ylabel(ylabel)
ax.legend(
loc="center left",
bbox_to_anchor=(1, 0.5),
)
ax.ticklabel_format(useOffset=False)
ax.tick_params(axis = 'x',labelrotation = 45)
plt.tight_layout()
# call the event
fig.canvas.mpl_connect('pick_event', onpick)
overlay_plot2(x1,y1,x2,y2,"title","xlabel","ylabel")

