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连接PostgreSQL数据库的过程详解

1. 常用模块 # 连接数据库 connect()函数创建一个新的数据库连接对话并返回一个新的连接实例对象 PG_CONF_123 = { 'user':'emma', 'p...

Python3 加密(hashlib和hmac)模块的实现

以下代码以Python3.6.1为例 hashlib : 不可逆加密 hmac : 不可逆键值对方式加密 hashlib模块简介: hashlib模块为不同的安全哈希/安全散...

python类和继承用法实例

本文实例讲述了python类和继承定义与用法。分享给大家供大家参考。具体如下: class Employee: pass lee = Employee() lee.name =...

python自动zip压缩目录的方法

本文实例讲述了python自动zip压缩目录的方法。分享给大家供大家参考。具体实现方法如下: 这段代码来压缩数据库备份文件,没有使用python内置的zip模块,而是使用了zip.exe...

Django 静态文件配置过程详解

静态文件配置 概述: 静态文件交由Web服务器处理,Django本身不处理静态文件。简单的处理逻辑如下(以nginx为例): URI请求 --> 按照Web服务器里面的配置规...