我有两个熊猫数据帧,data1
和data2
它们显示在图形上。我可以单击该点以显示有关此点的信息。但仅来自一个数据集。如何将其用于两个数据集? 我还需要显示来自另一个数组的信息,该数组通过索引访问数据。
from matplotlib.pyplot import figure, show
import pandas as pd
# I want to show infrotmation from these dataframes data using indexes on click
initial_data1 = pd.read_csv("data1.csv")
initial_data2 = pd.read_csv("data2.csv")
data1 = initial_data1.copy()
data2 = initial_data2.copy()
data1 = SomeProcessing(data1) #I use this data to make a plot
data2 = SomeProcessing(data2) #I use this data to make a plot
def showScatter():
def onpick3(event):
ind = event.ind
display(initial_data2.loc[ind])
fig = figure()
ax1 = fig.add_subplot(111)
col = ax1.scatter(data1[:,0], data1[:,1], picker=True)
col = ax1.scatter(data2[:,0], data2[:,1], marker="+", color = 'black', picker=True)
for i in range(len(data2)):
ax1.annotate(i, (data2[i,0], data2[i,1]))
fig.canvas.mpl_connect('pick_event', onpick3)
show()
答案 0 :(得分:0)
一个人将使用event.artist
来确定给定索引event.ind
属于轴上的哪个艺术家。然后,可以使用该艺术家来确定下一步要做的事情。
import numpy as np
import matplotlib.pyplot as plt
def showScatter(data, otherdata):
def onpick(event):
ind = event.ind[0]
artist = event.artist
d = otherdata[scatters.index(artist)]
ax.set_title("You clicked {}".format(d[ind]))
fig, ax = plt.subplots()
scatters = []
for d in data:
sc = ax.scatter(d[:,0], d[:,1], picker=True)
scatters.append(sc)
for i in range(len(d)):
ax.annotate(i, (d[i,0], d[i,1]), xytext=(0,3),
textcoords="offset points", ha="center", va="baseline")
return fig.canvas.mpl_connect('pick_event', onpick)
x,y = np.meshgrid(np.arange(10), np.arange(6))
ind = np.random.randint(0,2,size=6*10).astype(bool)
data1 = np.column_stack((x.flatten()[ind], y.flatten()[ind]))
data2 = np.column_stack((x.flatten()[~ind], y.flatten()[~ind]))
cid = showScatter((data1, data2), (data1, data2))
plt.show()