wxPython+Matplotlib绘制折线图表

yipeiwu_com6年前Python基础

使用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()

效果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python操作配置文件ini的三种方法讲解

python 操作配置文件ini的三种方法 方法一:crudini 命令 说明 crudini命令是Linux下的一个操作配置文件的命令工具 用法 crudini --set [--...

对python中for、if、while的区别与比较方法

如下所示: if应用举例: #if 若条件成立,只执行一次 #if 条件:如果条件成立,执行条件后的代码块内容,不成立,直接跳过代码块 #判断如果年龄age小于18,输出未成年 #=...

如何基于python生成list的所有的子集

这篇文章主要介绍了如何基于python生成list的所有的子集,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 不使用递归且不引入标准库...

python-opencv在有噪音的情况下提取图像的轮廓实例

python-opencv在有噪音的情况下提取图像的轮廓实例

对于一般的图像提取轮廓,介绍了一个很好的方法,但是对于有噪声的图像,并不能很好地捕获到目标物体。 比如对于我的鼠标,提取的轮廓效果并不好,因为噪声很多: 所以本文增加了去掉噪声的部分。...

Python随机数用法实例详解【基于random模块】

本文实例讲述了Python随机数用法。分享给大家供大家参考,具体如下: 1. random.seed(int) 给随机数对象一个种子值,用于产生随机序列。 对于同一个种子值的输入,之后产...