我可以使用哪个库在 Python 中绘制交互式“耦合”图?

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

我正在尝试绘制两个变量“x”和“y1”的交互式图,对于“y1”的每个值,都有一个“x”与第三个量“y2”的图。 (对于任何感兴趣的人,我正在构建一个二元相图。“x”是成分,“y1”是温度,“y2”是吉布斯自由能。我有 x 与 y2 的方程,用于计算 y1 )

[y1 在底部,几个 y1 的 y2 在顶部绘制] (https://i.stack.imgur.com/4ZByx.png)

在附图中,y1 绘制在底部的图中,对于每个 y1 都有一条 x 与 y2 曲线。我想重新创建它,以便根据 x vs y1 图上光标的位置更新 x vs y2 图。我将计算所有这些值并将其存储在一个数组中,因此我需要能够根据温度选择正确的数据点。

这可以使用任何常用的库在 Python 中实现吗?有人能指出我正确的方向吗?如果这不能在 Python 中完成,我愿意尝试其他工具。

提前致谢。

plot plotly interactive graphing
1个回答
0
投票

首先,让我们创建两个图形作为子图并绘制我们的第一个图:

fig, (ax1, ax2) = plt.subplots(2,1)
... # add title and labels for ax1 and ax2
line = ax1.plot(<plot as you would normally do>)

然后我们需要订阅图形上的鼠标事件(

motion_notify_event
,在我们的例子中为鼠标移动),并在事件触发后处理它。让我们先创建处理程序:

def mouse_mov_handler(event):
    if event.inaxes != ax1:
        return # return if not in first plot
    contains, details = line.contains(event)
    if contains: # if event occured "on" our x vs y1 line created before 
         update_ax2(details) # This will update our second plot 

当我们检测到鼠标移动(

mouse_notify_event
)时,需要调用此处理程序,因此在调用
plt.show()
之前进行设置:

fig.canvas.mpl_connect('motion_notify_event', mouse_mov_handler)
plt.show()

现在,这是

update_ax2
方法:

def update_ax2(details):
    x,y = line.get_data() # get all points (can be from your own data points to be optimal)
    selected_y1 = y[details['ind'][0]] # get y value with first element of dictionary of indices returned
    # redraw ax2 plot
    ax2.clear()
    new_data = XvsY2(selected_y1) # calculate new x vs y2 curve from selected y1
    ax2.plot(new_data)
    fig.canvas.draw()

这是一个非常接近您想要更好地了解如何设置的示例:

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