Skip to main content
 首页 » 编程设计

python之在 Matplotlib 中,在鼠标单击事件发生时,填充鼠标单击右侧的图

2025年04月02日9虾米哥

我是编程新手,想知道我是否可以从这里非常有用的社区获得一些专家帮助。

我正在尝试创建一个鼠标单击事件,在该事件上,鼠标单击右侧的整个绘图区域都会被着色。因此,我希望通过鼠标单击来注册 x 值,在该 x 值上创建一条垂直线,并在垂直线右侧为整个绘图添加阴影。

我有 5 个子图显示分布。我希望此鼠标单击事件仅在第 4 个(PDF 图)和第 5 个(CDF 图)子图上触发。这样做的目的是设置边距和分析分布。

见下图

我设法编写了一个代码来执行此操作,但它不会在鼠标单击时更新(阴影区域)绘图。这是我的代码

import numpy as np 
from scipy.stats import norm, lognorm, uniform 
import matplotlib.pyplot as plt 
from matplotlib.widgets import Slider, Button, RadioButtons, CheckButtons 
from matplotlib.patches import Polygon 
 
 
#####Mean and standard deviation##### 
 
mu_a1 = 1 
mu_b1 = 10 
mu_c1 = -13 
sigma_a1 =  0.14 
sigma_b1 =  1.16 
sigma_c1 =  2.87 
mu_x01 = -11 
sigma_x01 =  1.9 
 
#####_____##### 
 
 
 
#####Generating random data##### 
 
a1 = 0.75*mu_a1 + (1.25 - 0.75)*sigma_a1*np.random.sample(10000) 
b1 = 8*mu_b1 + (12 - 8)*sigma_b1*np.random.sample(10000) 
c1 = -12*mu_c1 + 2*sigma_c1*np.random.sample(10000) 
x01 = (-b1 - np.sqrt(b1**2 - (4*a1*c1)))/(2*a1) 
 
#####_____##### 
 
 
 
#####Creating Subplots##### 
 
fig = plt.figure() 
plt.subplots_adjust(left=0.13,right=0.99,bottom=0.05) 
 
ax1 = fig.add_subplot(331)                                                  #Subplot 1 
ax1.set_xlabel('a' , fontsize = 14) 
ax1.grid(True) 
 
ax2 = fig.add_subplot(334)                                                  #Subplot 2 
ax2.set_xlabel('b', fontsize = 14) 
ax2.grid(True) 
 
ax3 = fig.add_subplot(337)                                                  #Subplot 3 
ax3.set_xlabel('c', fontsize = 14) 
ax3.grid(True) 
 
ax4 = fig.add_subplot(132)                                                  #Subplot 4 
ax4.set_xlabel('x0', fontsize = 14) 
ax4.set_ylabel('PDF', fontsize = 14) 
ax4.grid(True) 
 
ax5 = fig.add_subplot(133)                                                  #Subplot 5 
ax5.set_xlabel('x0', fontsize = 14) 
ax5.set_ylabel('CDF', fontsize = 14) 
ax5.grid(True) 
 
#####_____##### 
 
 
 
#####Plotting Distributions##### 
 
[n1,bins1,patches] = ax1.hist(a1, bins=50, color = 'red',alpha = 0.5, normed = True) 
[n2,bins2,patches] = ax2.hist(b1, bins=50, color = 'red',alpha = 0.5, normed = True) 
[n3,bins3,patches] = ax3.hist(c1, bins=50, color = 'red',alpha = 0.5, normed = True) 
[n4,bins4,patches] = ax4.hist(x01, bins=50, color = 'red',alpha = 0.5, normed = True) 
ax4.axvline(np.mean(x01), color = 'black', linestyle = 'dashed', lw = 2) 
dx = bins4[1] - bins4[0] 
CDF = np.cumsum(n4)*dx 
ax5.plot(bins4[1:], CDF, color = 'red') 
 
#####_____##### 
 
 
 
#####Event handler for button_press_event##### 
 
def onclick(event): 
    ''' 
    Event handler for button_press_event 
    @param event MouseEvent 
    ''' 
    global ix 
    ix = event.xdata 
    if ix is not None: 
        print 'x = %f' %(ix) 
 
    ax4.clear() 
    ax5.clear() 
    ax4.grid(True) 
    ax5.grid(True) 
    [n4,bins4,patches] = ax4.hist(x01, bins=50, color = 'red',alpha = 0.5, normed = True) 
    ax4.axvline(np.mean(x01), color = 'black', linestyle = 'dashed', lw = 2) 
    ax4.axvspan(ix, -90, facecolor='0.9', alpha=0.5) 
    dx = bins4[1] - bins4[0] 
    CDF = np.cumsum(n4)*dx 
    ax5.plot(bins4[1:], CDF, color = 'red') 
    ax5.axvspan(ix, -75, facecolor='0.9', alpha=0.5)   
    return ix 
 
cid = fig.canvas.mpl_connect('button_press_event', onclick) 
 
plt.show() 
 
#####_____##### 

此外,由于我使用的是 axvspan,它要求我输入 xmin 和 xmax 值。 xmin = mouse_click_value 和 xmax,我希望它一直到绘图结束,无论最终值是多少。目前,我使用的是固定值(图片中的 -90 或 -75)。随着变量值的变化,这似乎不起作用。

请您参考如下方法:

对于您的主要问题:

只需添加:

plt.draw() 

在您进行所有更新之后(即在 onClick 的return 之前)。

对于你的第二个问题:

您可以使用(例如在 ax5 上):

ax5.get_xlim()[1] 

获取ax5的xmax