Python Matplotlib简易教程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先需要导入matplotlib库和numpy库: ```python import matplotlib.pyplot as plt import numpy as np ``` 接着,定义计算器上的按钮和显示屏的位置: ```python # 定义按钮位置和尺寸 btns = { '7': [0, 0], '8': [1, 0], '9': [2, 0], '/': [3, 0], '4': [0, 1], '5': [1, 1], '6': [2, 1], '*': [3, 1], '1': [0, 2], '2': [1, 2], '3': [2, 2], '-': [3, 2], '0': [0, 3], '.': [1, 3], '=': [2, 3], '+': [3, 3], } # 定义显示屏位置和尺寸 disp = [0, 4, 4, 1] ``` 然后,定义按钮的点击事件处理函数,以及计算器上的操作: ```python # 定义按钮点击事件处理函数 def button_click(event): global disp_text btn = event.inaxes.title.get_text() if btn == 'C': # 清空 disp_text = '' elif btn == '=': # 计算结果 try: disp_text = str(eval(disp_text)) except: disp_text = 'Error' else: # 拼接数字和操作符 disp_text += btn # 更新显示屏 display.set_text(disp_text) # 初始化显示屏文字 disp_text = '' # 定义计算器上的操作 operations = ['7', '8', '9', '/', '4', '5', '6', '*', '1', '2', '3', '-', '0', '.', '=', '+', 'C'] ``` 接着,创建一个图形窗口并添加显示屏和按钮: ```python # 创建图形窗口 fig, ax = plt.subplots(figsize=(8, 8)) # 隐藏坐标轴 plt.axis('off') # 添加显示屏 display = plt.text(disp[0], disp[1], disp_text, ha='right', va='top', fontsize=20) # 添加按钮 for btn, pos in btns.items(): if btn == 'C': color = 'orange' elif btn == '=': color = 'green' else: color = 'gray' rect = plt.Rectangle((pos[0], pos[1]), 1, 1, color=color) ax.add_patch(rect) plt.text(pos[0] + 0.5, pos[1] + 0.5, btn, ha='center', va='center', fontsize=20) rect.set_picker(True) rect.set_pickradius(5) rect.set_alpha(0.7) rect.set_linewidth(0) rect.set_edgecolor('none') ax.figure.canvas.mpl_connect('pick_event', button_click) ``` 最后,显示图形窗口: ```python plt.show() ``` 完整代码如下: ```python import matplotlib.pyplot as plt import numpy as np # 定义按钮位置和尺寸 btns = { '7': [0, 0], '8': [1, 0], '9': [2, 0], '/': [3, 0], '4': [0, 1], '5': [1, 1], '6': [2, 1], '*': [3, 1], '1': [0, 2], '2': [1, 2], '3': [2, 2], '-': [3, 2], '0': [0, 3], '.': [1, 3], '=': [2, 3], '+': [3, 3], } # 定义显示屏位置和尺寸 disp = [0, 4, 4, 1] # 定义按钮点击事件处理函数 def button_click(event): global disp_text btn = event.inaxes.title.get_text() if btn == 'C': # 清空 disp_text = '' elif btn == '=': # 计算结果 try: disp_text = str(eval(disp_text)) except: disp_text = 'Error' else: # 拼接数字和操作符 disp_text += btn # 更新显示屏 display.set_text(disp_text) # 初始化显示屏文字 disp_text = '' # 定义计算器上的操作 operations = ['7', '8', '9', '/', '4', '5', '6', '*', '1', '2', '3', '-', '0', '.', '=', '+', 'C'] # 创建图形窗口 fig, ax = plt.subplots(figsize=(8, 8)) # 隐藏坐标轴 plt.axis('off') # 添加显示屏 display = plt.text(disp[0], disp[1], disp_text, ha='right', va='top', fontsize=20) # 添加按钮 for btn, pos in btns.items(): if btn == 'C': color = 'orange' elif btn == '=': color = 'green' else: color = 'gray' rect = plt.Rectangle((pos[0], pos[1]), 1, 1, color=color) ax.add_patch(rect) plt.text(pos[0] + 0.5, pos[1] + 0.5, btn, ha='center', va='center', fontsize=20) rect.set_picker(True) rect.set_pickradius(5) rect.set_alpha(0.7) rect.set_linewidth(0) rect.set_edgecolor('none') ax.figure.canvas.mpl_connect('pick_event', button_click) plt.show() ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值