📄 plot.py
字号:
#-----------------------------------------------------------------------------# Name: wx.lib.plot.py# Purpose: Line, Bar and Scatter Graphs## Author: Gordon Williams## Created: 2003/11/03# RCS-ID: $Id: plot.py 8707 2008-06-25 20:12:06Z jcorgan $# Copyright: (c) 2002,2007# Licence: Use as you wish.#-----------------------------------------------------------------------------# 12/15/2003 - Jeff Grimmett (grimmtooth@softhome.net)## o 2.5 compatability update.# o Renamed to plot.py in the wx.lib directory.# o Reworked test frame to work with wx demo framework. This saves a bit# of tedious cut and paste, and the test app is excellent.## 12/18/2003 - Jeff Grimmett (grimmtooth@softhome.net)## o wxScrolledMessageDialog -> ScrolledMessageDialog## Oct 6, 2004 Gordon Williams (g_will@cyberus.ca)# - Added bar graph demo# - Modified line end shape from round to square.# - Removed FloatDCWrapper for conversion to ints and ints in arguments## Oct 15, 2004 Gordon Williams (g_will@cyberus.ca)# - Imported modules given leading underscore to name.# - Added Cursor Line Tracking and User Point Labels. # - Demo for Cursor Line Tracking and Point Labels.# - Size of plot preview frame adjusted to show page better.# - Added helper functions PositionUserToScreen and PositionScreenToUser in PlotCanvas.# - Added functions GetClosestPoints (all curves) and GetClosestPoint (only closest curve)# can be in either user coords or screen coords.# # May 27, 2007 Johnathan Corgan (jcorgan@corganenterprises.com)# - Converted from numarray to numpy"""This is a simple light weight plotting module that can be used withBoa or easily integrated into your own wxPython application. Theemphasis is on small size and fast plotting for large data sets. Ithas a reasonable number of features to do line and scatter graphseasily as well as simple bar graphs. It is not as sophisticated or as powerful as SciPy Plt or Chaco. Both of these are great packages but consume huge amounts of computer resources for simple plots.They can be found at http://scipy.comThis file contains two parts; first the re-usable library stuff, then,after a "if __name__=='__main__'" test, a simple frame and a few defaultplots for examples and testing.Based on wxPlotCanvasWritten by K.Hinsen, R. Srinivasan;Ported to wxPython Harm van der Heijden, feb 1999Major Additions Gordon Williams Feb. 2003 (g_will@cyberus.ca) -More style options -Zooming using mouse 'rubber band' -Scroll left, right -Grid(graticule) -Printing, preview, and page set up (margins) -Axis and title labels -Cursor xy axis values -Doc strings and lots of comments -Optimizations for large number of points -Legends Did a lot of work here to speed markers up. Only a factor of 4improvement though. Lines are much faster than markers, especiallyfilled markers. Stay away from circles and triangles unless youonly have a few thousand points.Times for 25,000 pointsLine - 0.078 secMarkersSquare - 0.22 secdot - 0.10circle - 0.87cross,plus - 0.28triangle, triangle_down - 0.90Thanks to Chris Barker for getting this version working on Linux.Zooming controls with mouse (when enabled): Left mouse drag - Zoom box. Left mouse double click - reset zoom. Right mouse click - zoom out centred on click location."""import string as _stringimport time as _timeimport wx# Needs numpy or numarraytry: import numpy as _numpyexcept: try: import numarray as _numpy #if numarray is used it is renamed numpy except: msg= """ This module requires the numpy or numarray module, which could not be imported. It probably is not installed (it's not part of the standard Python distribution). See the Python site (http://www.python.org) for information on downloading source or binaries.""" raise ImportError, "numpy or numarray not found. \n" + msg## Plotting classes...#class PolyPoints: """Base Class for lines and markers - All methods are private. """ def __init__(self, points, attr): self.points = _numpy.array(points) self.currentScale= (1,1) self.currentShift= (0,0) self.scaled = self.points self.attributes = {} self.attributes.update(self._attributes) for name, value in attr.items(): if name not in self._attributes.keys(): raise KeyError, "Style attribute incorrect. Should be one of %s" % self._attributes.keys() self.attributes[name] = value def boundingBox(self): if len(self.points) == 0: # no curves to draw # defaults to (-1,-1) and (1,1) but axis can be set in Draw minXY= _numpy.array([-1,-1]) maxXY= _numpy.array([ 1, 1]) else: minXY= _numpy.minimum.reduce(self.points) maxXY= _numpy.maximum.reduce(self.points) return minXY, maxXY def scaleAndShift(self, scale=(1,1), shift=(0,0)): if len(self.points) == 0: # no curves to draw return if (scale is not self.currentScale) or (shift is not self.currentShift): # update point scaling self.scaled = scale*self.points+shift self.currentScale= scale self.currentShift= shift # else unchanged use the current scaling def getLegend(self): return self.attributes['legend'] def getClosestPoint(self, pntXY, pointScaled= True): """Returns the index of closest point on the curve, pointXY, scaledXY, distance x, y in user coords if pointScaled == True based on screen coords if pointScaled == False based on user coords """ if pointScaled == True: #Using screen coords p = self.scaled pxy = self.currentScale * _numpy.array(pntXY)+ self.currentShift else: #Using user coords p = self.points pxy = _numpy.array(pntXY) #determine distance for each point d= _numpy.sqrt(_numpy.add.reduce((p-pxy)**2,1)) #sqrt(dx^2+dy^2) pntIndex = _numpy.argmin(d) dist = d[pntIndex] return [pntIndex, self.points[pntIndex], self.scaled[pntIndex], dist] class PolyLine(PolyPoints): """Class to define line type and style - All methods except __init__ are private. """ _attributes = {'colour': 'black', 'width': 1, 'style': wx.SOLID, 'legend': ''} def __init__(self, points, **attr): """Creates PolyLine object points - sequence (array, tuple or list) of (x,y) points making up line **attr - key word attributes Defaults: 'colour'= 'black', - wx.Pen Colour any wx.NamedColour 'width'= 1, - Pen width 'style'= wx.SOLID, - wx.Pen style 'legend'= '' - Line Legend to display """ PolyPoints.__init__(self, points, attr) def draw(self, dc, printerScale, coord= None): colour = self.attributes['colour'] width = self.attributes['width'] * printerScale style= self.attributes['style'] pen = wx.Pen(wx.NamedColour(colour), width, style) pen.SetCap(wx.CAP_BUTT) dc.SetPen(pen) if coord == None: dc.DrawLines(self.scaled) else: dc.DrawLines(coord) # draw legend line def getSymExtent(self, printerScale): """Width and Height of Marker""" h= self.attributes['width'] * printerScale w= 5 * h return (w,h)class PolyMarker(PolyPoints): """Class to define marker type and style - All methods except __init__ are private. """ _attributes = {'colour': 'black', 'width': 1, 'size': 2, 'fillcolour': None, 'fillstyle': wx.SOLID, 'marker': 'circle', 'legend': ''} def __init__(self, points, **attr): """Creates PolyMarker object points - sequence (array, tuple or list) of (x,y) points **attr - key word attributes Defaults: 'colour'= 'black', - wx.Pen Colour any wx.NamedColour 'width'= 1, - Pen width 'size'= 2, - Marker size 'fillcolour'= same as colour, - wx.Brush Colour any wx.NamedColour 'fillstyle'= wx.SOLID, - wx.Brush fill style (use wx.TRANSPARENT for no fill) 'marker'= 'circle' - Marker shape 'legend'= '' - Marker Legend to display Marker Shapes: - 'circle' - 'dot' - 'square' - 'triangle' - 'triangle_down' - 'cross' - 'plus' """ PolyPoints.__init__(self, points, attr) def draw(self, dc, printerScale, coord= None): colour = self.attributes['colour'] width = self.attributes['width'] * printerScale size = self.attributes['size'] * printerScale fillcolour = self.attributes['fillcolour'] fillstyle = self.attributes['fillstyle'] marker = self.attributes['marker'] dc.SetPen(wx.Pen(wx.NamedColour(colour), width)) if fillcolour: dc.SetBrush(wx.Brush(wx.NamedColour(fillcolour),fillstyle)) else: dc.SetBrush(wx.Brush(wx.NamedColour(colour), fillstyle)) if coord == None: self._drawmarkers(dc, self.scaled, marker, size) else: self._drawmarkers(dc, coord, marker, size) # draw legend marker def getSymExtent(self, printerScale): """Width and Height of Marker""" s= 5*self.attributes['size'] * printerScale return (s,s) def _drawmarkers(self, dc, coords, marker,size=1): f = eval('self._' +marker) f(dc, coords, size) def _circle(self, dc, coords, size=1): fact= 2.5*size wh= 5.0*size rect= _numpy.zeros((len(coords),4),_numpy.float)+[0.0,0.0,wh,wh] rect[:,0:2]= coords-[fact,fact] dc.DrawEllipseList(rect.astype(_numpy.int32)) def _dot(self, dc, coords, size=1): dc.DrawPointList(coords) def _square(self, dc, coords, size=1): fact= 2.5*size wh= 5.0*size rect= _numpy.zeros((len(coords),4),_numpy.float)+[0.0,0.0,wh,wh] rect[:,0:2]= coords-[fact,fact] dc.DrawRectangleList(rect.astype(_numpy.int32)) def _triangle(self, dc, coords, size=1): shape= [(-2.5*size,1.44*size), (2.5*size,1.44*size), (0.0,-2.88*size)] poly= _numpy.repeat(coords,3) poly.shape= (len(coords),3,2) poly += shape dc.DrawPolygonList(poly.astype(_numpy.int32)) def _triangle_down(self, dc, coords, size=1): shape= [(-2.5*size,-1.44*size), (2.5*size,-1.44*size), (0.0,2.88*size)] poly= _numpy.repeat(coords,3) poly.shape= (len(coords),3,2) poly += shape dc.DrawPolygonList(poly.astype(_numpy.int32)) def _cross(self, dc, coords, size=1): fact= 2.5*size for f in [[-fact,-fact,fact,fact],[-fact,fact,fact,-fact]]: lines= _numpy.concatenate((coords,coords),axis=1)+f dc.DrawLineList(lines.astype(_numpy.int32)) def _plus(self, dc, coords, size=1): fact= 2.5*size for f in [[-fact,0,fact,0],[0,-fact,0,fact]]: lines= _numpy.concatenate((coords,coords),axis=1)+f dc.DrawLineList(lines.astype(_numpy.int32))class PlotGraphics: """Container to hold PolyXXX objects and graph labels
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -