Python 中利用 matplotlib 进行曲线的部分填充 fill_betweenx

Python 中利用 matplotlib 进行曲线的部分填充 fill_betweenx

实现效果:


代码:

def Plot_LSH_Curve(b, r, Sim_T):
    s = np.arange(0, 1, 0.01)
    p = 1 - np.power(1 - np.power(s, r), b)

    fig = plt.figure()
    ax = plt.gca()
    # ax = fig.add_subplot(111)
    ax.plot(s, p, color='red', linewidth=2)
    plt.vlines(Sim_T, [0], 1, color="green", linewidth=
可以使用`after`方法来实现定时更新`ax.fill_between`区域。具体步骤如下: 1. 创建一个`FigureCanvasTkAgg`对象,并将其放置在Tkinter窗口。 2. 创建一个matplotlib的figure对象,然后在其添加一个`Axes`对象。 3. 在`Axes`对象使用`fill_between`方法绘制区域。 4. 使用`after`方法定时更新`fill_between`区域,更新后需要重新绘制整个图形。 下面是一个简单的示例代码,可以实现每秒钟更新一次`fill_between`区域: ```python import tkinter as tk from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg from matplotlib.figure import Figure import numpy as np # 创建Tkinter窗口 root = tk.Tk() root.geometry('500x500') # 创建Matplotlib图形 fig = Figure(figsize=(5, 5)) ax = fig.add_subplot(111) # 绘制填充区域 x = np.linspace(0, 10, 100) y1 = np.sin(x) y2 = np.cos(x) line1, = ax.plot(x, y1, 'b') line2, = ax.plot(x, y2, 'r') fill = ax.fill_between(x, y1, y2, alpha=0.5) # 将Matplotlib图形添加到Tkinter窗口 canvas = FigureCanvasTkAgg(fig, master=root) canvas.get_tk_widget().pack() # 定义更新函数 def update(): # 更新数据 y1 = np.sin(x + update.count) y2 = np.cos(x + update.count) fill.remove() fill = ax.fill_between(x, y1, y2, alpha=0.5) update.count += 0.1 # 重新绘制图形 canvas.draw() # 使用after方法定时更新 root.after(1000, update) update.count = 0 update() root.mainloop() ``` 在这个示例,我们使用`update`函数来定时更新`fill_between`区域。每次更新时,我们通过修改数据和重新绘制图形来实现更新。使用`after`方法可以让我们实现定时更新,同时不会阻塞GUI线程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值