Python 3d 散点图在子图之间链接注释

问题描述 投票:0回答:1

我在子图中有两个(或更多)3D 散点图,每个散点图显示数据集的不同 3 个变量。当我将鼠标悬停在一个子图中的数据上时,我希望其他子图也自动显示相同的数据样本。目前,我可以使用 mplcursors 模块(在 jupyter 笔记本中)显示一个图的注释(悬停时),但我希望悬停注释在所有子图之间链接。

下面是当前实现生成的示例图像: enter image description here

最少的工作代码:

%matplotlib ipympl
import plotly.express as px
import matplotlib.pyplot as plt 
import mplcursors

df = px.data.iris()

fig = plt.figure(figsize=(12, 8))
ax = fig.add_subplot(121, projection='3d')

ax.scatter(df['sepal_length'], df['sepal_width'], df['petal_length'], marker='.')
ax.set_xlabel('Sepal Length')
ax.set_ylabel('Sepal Width')
ax.set_zlabel('Petal Length')
ax.set_title("Scatter plot of sepal length, sepal width, and petal length")

ax2 = fig.add_subplot(122, projection='3d')

ax2.scatter(df['sepal_length'], df['sepal_width'], df['petal_width'], marker='.')
ax2.set_xlabel('Sepal Length')
ax2.set_ylabel('Sepal Width')
ax2.set_zlabel('Petal Width')
ax2.set_title("Scatter plot of sepal length, sepal width, and petal width")

mplcursors.cursor(hover=True)

plt.show()

提前谢谢您。

python matplotlib hover scatter-plot
1个回答
0
投票

如果它足够让您突出显示“链接”散点,您可以这样做:

import mplcursors
import numpy as np
import plotly.express as px
import matplotlib.pyplot as plt

df = px.data.iris().head(10) # to make it minimal

fig, axes = plt.subplots(2, 3, subplot_kw={"projection": "3d"}, figsize=(10, 6))

SC = np.array([
    [ax.scatter(*xyz, color="C0")
    for xyz in df[["sepal_length", "sepal_width", "petal_length"]].to_numpy()]
    for ax in axes.flat
])

cursor = mplcursors.cursor(SC.flat, hover=True)

def getwins(art):
    row, cols = np.argwhere(SC == art)[0]
    return np.delete(SC, row, axis=0)[:, cols]

@cursor.connect("add")
def on_add(sel):
    for art in getwins(sel.artist):
        sel.extras.append(cursor.add_highlight(art))

NB:添加的子图和数据点越多,交互就越缓慢。

© www.soinside.com 2019 - 2024. All rights reserved.