python的gui中设置画笔渐变_『Python』matplotlib实现GUI效果

1. 类RadioButtons的使用方法

类似单选框

import numpy as np

import matplotlib.pyplot as plt

import matplotlib as mpl

from matplotlib.widgets import RadioButtons

mpl.use("Qt5Agg")

x = np.linspace(0.0, 2.0, 1000)

y1 = 1.5 * np.cos(2 * np.pi * x)

y2 = 1.0 * np.cos(2 * np.pi * x)

y3 = 0.8 * np.cos(2 * np.pi * x)

fig, ax = plt.subplots(1, 1)

line, = ax.plot(x, y1, color="red", lw=2)

plt.subplots_adjust(left=0.35)

facecolor = "cornflowerblue"

ax1 = plt.axes([0.1, 0.7, 0.15, 0.15], facecolor=facecolor)

radio1 = RadioButtons(ax1, ("1.5A", "1.0A", "0.8A"))

def amplitudefunc(label):

hzdict = {"1.5A": y1, "1.0A": y2, "0.8A": y3}

ydata = hzdict[label]

line.set_ydata(ydata)

plt.draw()

radio1.on_clicked(amplitudefunc)

ax2 = plt.axes([0.1, 0.4, 0.15, 0.15], facecolor=facecolor)

radio2 = RadioButtons(ax2, ("red", "green", "orange"))

def colorfunc(label):

line.set_color(label)

plt.draw()

radio2.on_clicked(colorfunc)

ax3 = plt.axes([0.1, 0.1, 0.15, 0.15], facecolor=facecolor)

radio3 = RadioButtons(ax3, ("-", "--", "-.", ":"))

def linestylefunc(label):

line.set_linestyle(label)

plt.draw()

radio3.on_clicked(linestylefunc)

plt.show()

2. 类Cursor的使用方法

帮你聚焦鼠标所在位置

import numpy as np

import matplotlib.pyplot as plt

import matplotlib as mpl

from matplotlib.widgets import Cursor

lineprops = dict(color="red", lw=2)

fig, ax = plt.subplots(1, 1, subplot_kw=dict(facecolor="lemonchiffon"))

x = np.random.random(100)

y = np.random.random(100)

ax.scatter(x, y, marker="o")

ax.set_xlim(-0.02, 1.02)

ax.set_ylim(-0.02, 1.02)

cursor = Cursor(ax, useblit=True, **lineprops) # 必须接收,不然会自动销毁掉。。。

plt.show()

3. 类CheckButtons的使用方法

类似复选框

import numpy as np

import matplotlib.pyplot as plt

import matplotlib as mpl

from matplotlib.widgets import CheckButtons

x = np.linspace(0.0, 2.0, 1000)

y1 = 1.5 * np.cos(2 * np.pi * x)

y2 = 1.0 * np.cos(2 * np.pi * x)

y3 = 0.8 * np.cos(2 * np.pi * x)

fig, ax = plt.subplots(1, 1)

line1, = ax.plot(x, y1, color="red", lw=2, visible=False, label="1.2 A")

line2, = ax.plot(x, y2, color="green", lw=2, visible=False, label="1.0 A")

line3, = ax.plot(x, y3, color="orange", lw=2, visible=False, label="0.8 A")

plt.subplots_adjust(left=0.30)

facecolor = "cornflowerblue"

cax = plt.axes([0.1, 0.4, 0.1, 0.15], facecolor=facecolor)

lines = [line1, line2, line3]

labels = [str(line.get_label()) for line in lines]

visibility = [line.get_visible() for line in lines]

check = CheckButtons(cax, labels, visibility)

def func(label):

index = labels.index(label)

lines[index].set_visible(not lines[index].get_visible())

plt.draw()

check.on_clicked(func)

plt.show()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值