wxpython使用matplot实时显示折线图_wxPython+Matplotlib绘制折线图表

本文介绍如何使用wxPython和Matplotlib库在Python中创建一个面板,实现实时显示折线图。通过导入numpy、FigureCanvasWxAgg和Figure等模块,创建一个FirseFrame类,定义InitUI方法来设置背景颜色、计算平均值并绘制曲线图。MainApp类用于启动应用程序。最后展示了一个简单的折线图示例。
摘要由CSDN通过智能技术生成

使用Matplotlib在wxPython的Panel上绘制曲线图,需要导入:

import numpy

from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas

from matplotlib.figure import Figure

下面直接贴出源代码:

#coding=utf-8

"""

程序的主入口

"""

import wx

import numpy

from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas

from matplotlib.figure import Figure

class FirseFrame(wx.Frame):

def __init__(self, parent=None, id=-1, title='', pos=wx.DefaultSize, size=wx.DefaultSize, style= wx.DEFAULT_FRAME_STYLE):

wx.Frame.__init__(self, parent, id, title, pos, size, style)

self.InitUI()

pass

def InitUI(self):

self.SetBackgroundColour('white')

self.scorePanel = wx.Panel(self)

scores = [89, 98, 70, 80, 60, 78, 85, 90]

sum = 0

for s in scores:

sum += s

average = sum / len(scores)

t_score = numpy.arange(1, len(scores) + 1, 1)

s_score = numpy.array(scores)

self.figure_score = Figure()

self.figure_score.set_figheight(3.6)

self.figure_score.set_figwidth(7.8)

self.axes_score = self.figure_score.add_subplot(111)

self.axes_score.plot(t_score, s_score, 'ro', t_score, s_score, 'k')

self.axes_score.axhline(y=average, color='r')

self.axes_score.set_title(u'My Scores')

self.axes_score.grid(True)

self.axes_score.set_xlabel('T')

self.axes_score.set_ylabel('score')

FigureCanvas(self.scorePanel, -1, self.figure_score)

pass

class MainApp(wx.App):

def OnInit(self):

style = wx.DEFAULT_FRAME_STYLE^wx.MAXIMIZE_BOX

self.frame = FirseFrame(id=-1, title=u'第一个窗口', pos=(10, 10), size=(340, 550), style=style)

self.frame.Show()

return True

def main():

app = MainApp()

app.MainLoop()

if __name__ == "__main__":

main()

效果:

234ed4f9fb57176c38779e0bfc241ab8.png

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易采站长站。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值