日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python xycoords_python可视化节点关系(三):matplotlib(2)鼠标交互

發布時間:2024/9/27 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python xycoords_python可视化节点关系(三):matplotlib(2)鼠标交互 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

實現鼠標交互

1. 實現鼠標點擊節點高亮

直接上代碼:

# -*- coding: utf-8 -*-

# -*- coding: utf-8 -*-

import matplotlib.pyplot as plt

from matplotlib.patches import Rectangle

import numpy as np

fig, ax = plt.subplots(figsize=(10,10))

node_pos = [(1, 0), (0, 1), (2,1), (1,2)]

bbox_args = dict(boxstyle="round", fc="0.8")

arrow_args = dict(arrowstyle="->")

an1 = plt.annotate(s="Test 1\n(node1,node2)\n(node3,node4,node5)\n\n\n", xy=node_pos[0], xycoords="data",

va="center", ha="center",

bbox=dict(boxstyle="round", fc="w", facecolor='green'))

an2 = plt.annotate(s="Test 2\n(node1,node2)", xy=node_pos[1], xycoords="data",

va="center", ha="center",

bbox=dict(boxstyle="round", fc="w", facecolor='green'))

arrow1 = plt.annotate('', xy=(0, 0), xycoords=an1,

xytext=(0, 0), textcoords=an2, # an1 -> an2

size=20, ha="center", va="center",

bbox=bbox_args,

arrowprops=dict(patchA=an2,

patchB=an1,

connectionstyle="arc3,rad=0.2",

#color='red',

#linewidth=5,

**arrow_args))

arrow2 = plt.annotate('', xy=(0, 0), xycoords=an2,

xytext=(0, 0), textcoords=an1, # an1 -> an2

size=20, ha="center", va="center",

bbox=bbox_args,

arrowprops=dict(patchA=an1,

patchB=an2,

connectionstyle="arc3,rad=0.2",

#color='red',

#linewidth=5,

**arrow_args))

# callback fun

def mouse_click(event):

if an1.contains(event)[0] == True: # 獲取鼠標事件

an1.set_bbox(dict(facecolor='red', alpha= 0.5, boxstyle= 'round'))

fig.canvas.draw() # 切記要加上這句,否則,回調函數中的參數無法更新

axes = plt.gca()

axes.set_xlim([-2,2])

axes.set_ylim([-2,2])

fig.canvas.mpl_connect('button_press_event', mouse_click)

主要就是實現回調函數onclick。通過點擊test1,實現背景高亮。

注意

切記要加上fig.canvas.draw(),否則回調函數中的參數無法更新。這個bug找了好久,都是淚。

2. 實現鼠標懸停在節點,顯示節點信息。

# -*- coding: utf-8 -*-

import matplotlib.pyplot as plt

from matplotlib.patches import Rectangle

import numpy as np

fig, ax = plt.subplots(figsize=(10,10))

node_pos = [(1, 0), (0, 1), (2,1), (1,2)]

bbox_args = dict(boxstyle="round", fc="0.8")

arrow_args = dict(arrowstyle="->")

an1 = plt.annotate(s="Test 1\n(node1,node2)\n(node3,node4,node5)\n\n\n", xy=node_pos[0], xycoords="data",

va="center", ha="center",

bbox=bbox_args)

an2 = plt.annotate(s="Test 2\n(node1,node2)", xy=node_pos[1], xycoords="data",

va="center", ha="center",

bbox=bbox_args)

arrow1 = plt.annotate('', xy=(0, 0), xycoords=an1,

xytext=(0, 0), textcoords=an2, # an1 -> an2

size=20, ha="center", va="center",

bbox=bbox_args,

arrowprops=dict(patchA=an2,

patchB=an1,

connectionstyle="arc3,rad=0.2",

#color='red',

#linewidth=5,

**arrow_args))

arrow2 = plt.annotate('', xy=(0, 0), xycoords=an2,

xytext=(0, 0), textcoords=an1, # an1 -> an2

size=20, ha="center", va="center",

bbox=bbox_args,

arrowprops=dict(patchA=an1,

patchB=an2,

connectionstyle="arc3,rad=0.2",

#color='red',

#linewidth=5,

**arrow_args))

#fig.canvas.draw()

node_pos = an1.get_position()

tag1 = plt.annotate(s='hello node1', xy=node_pos, xycoords='data',

color='red',

xytext=(node_pos[0] + 0.3, node_pos[1] + 0.3),

textcoords='data', horizontalalignment="left",

bbox=bbox_args,

annotation_clip=True

)

tag1.set_visible(False)

# callback fun

def mouse_click(event):

if an1.contains(event)[0] == True: # 獲取鼠標事件

if an1.get_color() != 'red':

an1.set_color('red') # set font color

an1.set_bbox(dict(facecolor='yellow', alpha=1, boxstyle='round'))

else:

an1.set_color('black') # set font color

an1.set_bbox(bbox_args)

fig.canvas.draw() # 切記要加上這句,否則,回調函數中的參數無法更新

# check mouse enter

def mouse_enter(event):

visible_change_flag = False

visible_flag = an1.contains(event)[0] #注意,這里檢查節點an1是否包含鼠標,而不是tag1

if visible_flag != tag1.get_visible():

visible_change_flag = True

tag1.set_visible(visible_flag)

if visible_change_flag:

fig.canvas.draw()

axes = plt.gca()

axes.set_xlim([-2,2])

axes.set_ylim([-2,2])

fig.canvas.mpl_connect('button_press_event', mouse_click)

fig.canvas.mpl_connect('motion_notify_event', mouse_enter)

效果圖

注意

這里檢查節點an1是否包含鼠標,而不是tag1

鼠標懸停事件是motion_notify_event,而不是button_press_event

總結

以上是生活随笔為你收集整理的python xycoords_python可视化节点关系(三):matplotlib(2)鼠标交互的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。