gpt4 book ai didi

python - 如何区分点散布matplotlib on pick

转载 作者:太空宇宙 更新时间:2023-11-03 21:29:00 32 4
gpt4 key购买 nike

我有两个 pandas 数据框,data1data2

它们显示在图表上。我可以点击该点来显示该点的信息。但仅来自一个数据集。如何将它用于两个数据集?我还需要显示来自另一个通过索引访问数据的数组的信息。

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

最佳答案

可以使用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()

关于python - 如何区分点散布matplotlib on pick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53652627/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com