如何在Plotly中添加交互式文本标签之间的箭头?
我正在创建一个能够以简单方式连接文本标签的应用,例如
import matplotlib.pyplot as plt
import networkx as nx
G = nx.DiGraph()
G.add_edges_from([(0,1)])
G.add_nodes_from([0, 1])
pos = {0:(0.1, 0.9), 1: (0.9, 0.5)}
fig, ax = plt.subplots()
annotations = {0: ax.annotate('Python', xy=pos[0], xycoords='data',
ha="center", va="center", bbox=dict(facecolor = "blue")),
1:ax.annotate('Programming', xy=pos[1], xycoords='data',
ha="center", va="center", bbox=dict(facecolor = "red"))}
annotations[1].draggable()
# if you don't have networkx installed, replace G.edges with [(0,1)]
for A, B in G.edges:
ax.annotate("", xy=pos[B], xycoords='data', xytext=pos[A], textcoords='data',
arrowprops=dict(arrowstyle="->", color="0.5", # shrinkA=85, shrinkB=85,
patchA=annotations[A],
patchB=annotations[B],
connectionstyle='arc3'))
plt.axis('off')
plt.show()
我目前正在寻找 plotly
解决方案,因为它具有一个支持Jupyter笔记本上的动态HTML,并允许没有JavaScript运行它。此外,我想实现这些目标:
- 功能,以准确连接矩形文本标签,这些标签是矩形的,如我的图表交互中所示的
- ,1:使与鼠标
- 互动的标签可拖动标签2:redraw箭头在拖动交互时连接文本标签
- 3:redirect 3:redirect在文本标签之后,对我的一些URL进行了双击,
对我来说最重要的是矩形文本标签连接的最小工作脚本。 matplotlib
还支持“ nofollow noreferrer”>处理事件。我还可能想讨论 plotly
是否支持使所有这三个交互成为代码。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
经过长时间的研究代码,我可以说大多数事情都涉及一些小缺点。
首先,我发现可以实现所有想要的功能:
互动图的硬结构:
自定义节点,边缘和标签样式
您需要定义
stylesheet
参数>参数>在app.layout
定义:在节点单击
基于 jupyter笔记本中的应用程序,使用 jupyterdash 。很简单,唯一需要的是使用其他类型的应用程序:
支持github和
nbviewer
如果您将应用程序上传到GitHub中,它将不会显示交互式应用程序,但是可以在 。有两个不好的方面:
https:// ...
nbviewer
http:// ... 。我不满意,但这是我唯一能找到的解决方法。支持乳胶
,直到发行 nofollow noreferrer“> dash) v2.3.0 一个月前。我在这些应用中一直使用的Dash Cytoscape中仍然不支持它。我希望这个问题将在不久的将来得到解决。
文字封闭在适合其形状的矩形标签中。
我可以找到任何方法可以做到这一点,正如您在带有
“非常长的线”测试'
的节点的示例中看到的那样。目前,如果文本标签的长度很长,则更好的设计是使用上面文本的圆形节点。我很高兴能够找到大多数问题的解决方案,但我仍然开放讨论有关乳胶/Mathjax,
nbviewer
以及标签中更好的文本外壳的支持。After long time spent studying codes I could say that majority of things are working with some minor drawbacks.
To start with, I found it possible to implement all the functionality wanted:
Construction of interactive graph:
Customising node, edge and label style
You'll need to define
stylesheet
parameter and uncomment it inapp.layout
definition:Navigation to url after node click
Based on this answer, just add a callback before server run:
Support for Jupyter Notebook
For embedding app inside Jupyter Notebook, use JupyterDash. It's simple, the only think you need is to use a different kind of app:
Support for GitHub and
nbviewer
If you upload your app in GitHub it won't display interactive apps but you could load your GitHub link in
nbviewer
. There are two bad sides:https://...
ofnbviewer
withhttp://...
. I'm not satisfied but this is the only workaround I could find.Support for Latex
There was no straight way in Dash Plotly to render Mathjax until release of Dash v2.3.0 one month ago. It's still not supported in Dash Cytoscape which I've been using in these apps. I hope this issue is going to be resolved in near future.
Text enclosing in rectangular labels that fits it's shape.
I could find any way to do it yet as you could see in an example of node with
'Very long line for testing'
. At the moment, if text labels occurs to be long, a better design is to use circular nodes with text above.I'm glad I was able to find solutions to majority of my questions and I'm still open to discuss about support for LaTeX/Mathjax,
nbviewer
and better text enclosion in labels.我认为最好使用
Plotly-Python
连接两个静态矩形文本标签。但是,在绘图中执行任何交互式的操作,以修改任何对象都需要在大多数情况下需要回调,这意味着您需要使用plotly-dash
。由于您在Jupyter笔记本电脑中工作,因此可以使用 jupyter-dash 。如果您对这样的解决方案感兴趣,我很乐意更新我的答案并提供示例。
I think the best you can do with
plotly-python
is connect two static rectangular text labels. However, doing anything interactive in Plotly that modifies any of the objects is going to requires callbacks for the most part, meaning that you will need to useplotly-dash
.Since you're working in a Jupyter Notebook, you could use Jupyter-Dash. If you would be interested in such a solution, I'd be happy to update my answer and provide an example.