~ubuntu-branches/ubuntu/saucy/python-scipy/saucy

« back to all changes in this revision

Viewing changes to Lib/sandbox/gplt/new_plot.py

  • Committer: Bazaar Package Importer
  • Author(s): Ondrej Certik
  • Date: 2008-06-16 22:58:01 UTC
  • mfrom: (2.1.24 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080616225801-irdhrpcwiocfbcmt
Tags: 0.6.0-12
* The description updated to match the current SciPy (Closes: #489149).
* Standards-Version bumped to 3.8.0 (no action needed)
* Build-Depends: netcdf-dev changed to libnetcdf-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#this just adds errorbar plotting capabilities
2
 
from Numeric import *
3
 
from numpy.lib.scimath import *
4
 
from types import StringType
5
 
import pyPlot
6
 
from pyPlot import *
7
 
 
8
 
def squeeze(a):
9
 
    s = shape(a)
10
 
    dims = len(s) - sum(equal(1,s),axis=0)
11
 
    new_s = compress(not_equal(1,s),s,axis=-1)
12
 
    return reshape(a,new_s)
13
 
 
14
 
 
15
 
class Plot(pyPlot.Plot):
16
 
    errorbar_lt = 1
17
 
    def errorbar(self,*data):
18
 
        self._init_plot()
19
 
        self._errorbar_plot(data)
20
 
 
21
 
    def _errorbar_plot(self,data):
22
 
        remains = data
23
 
        counter = 0
24
 
        plotcmd = ''
25
 
        tmpf = []
26
 
        if not self.m_hold: self.errorbar_lt = 1
27
 
        while len(remains) > 0:
28
 
            group, remains = self._errorbar_get_plot_group(remains)
29
 
            if(len(group) > 0):
30
 
                try:
31
 
                    cmd, file = self._errorbar_prepare_plot(group)
32
 
                except SizeMismatch, data:
33
 
                    print "Warning: ignoring plot group ", counter
34
 
                    print "The arrays should have the same number of " + data[0]
35
 
                    print 'array sizes: ', data[1],'and', data[2]
36
 
                else:
37
 
                    plotcmd = plotcmd + cmd
38
 
                    tmpf.append(file)
39
 
            counter = counter + 1
40
 
 
41
 
        # if the plot command was valid, and we aren't
42
 
        # adding to the current plot, delete all the old plot files
43
 
        if(len(plotcmd) > 0):
44
 
            if(not self.m_hold):
45
 
                self._delete_files()
46
 
            plotcmd = plotcmd[0:-2] # get rid of trailing comma
47
 
            if DEBUG == 1:
48
 
                print plotcmd
49
 
            if(self.m_hold == 1 and len(self.m_tmpfiles) > 0):
50
 
                self._send('replot ' + plotcmd)
51
 
            else:
52
 
                self._send('plot ' + plotcmd)
53
 
            self.m_tmpfiles = self.m_tmpfiles + tmpf
54
 
 
55
 
    def _errorbar_prepare_plot(self, group):
56
 
        plotinfo = ''
57
 
        x = group[0]
58
 
        y = group[1]
59
 
        e = group[2]
60
 
        ar_num = 3
61
 
        if(len(group) > 3):
62
 
            plotinfo = group[3]
63
 
 
64
 
#force 1D arrays to 2D columns
65
 
        if(is1D(x) ): x = col(x)
66
 
        if(is1D(y) ): y = col(y)
67
 
        if(is1D(e) ): e = col(e)
68
 
        xs = shape(x)
69
 
        ys = shape(y)
70
 
        es = shape(e)
71
 
#test that x and y have compatible shapes
72
 
        #check that each array has the same number of rows
73
 
        if(xs[0] != ys[0] or xs[0] != es[0]):
74
 
            raise SizeMismatch, ('rows', xs, ys, es)
75
 
        #check that x.cols = y.cols
76
 
        #no error x has 1 column
77
 
        if(xs[1] > 1 and (xs[1] != ys[1] or xs[1] !=es[1])):
78
 
            raise SizeMismatch, ('cols', xs, ys, es)
79
 
        if(ys[1] != es[1]):
80
 
            raise SizeMismatch, ('cols', xs, ys, es)
81
 
#now write the data to a temp file and then plot it
82
 
        filename = tempfile.mktemp()
83
 
        f = open(filename, 'w')
84
 
        using = ()
85
 
 
86
 
        mn,dummy = self._find_min_max(y-e)
87
 
        dummy,mx = self._find_min_max(y+e)
88
 
        if(mn < self.m_rmin):
89
 
            self.m_rmin = mn
90
 
        if(mx > self.m_rmax):
91
 
            self.m_rmax = mx
92
 
 
93
 
        #x is effectively 1D
94
 
        if(xs[1] == 1):
95
 
            for i in range(0,xs[0] ):
96
 
                f.write( self._format( (x[i][0],) ))
97
 
                for j in range(0,ys[1] ):
98
 
                    f.write( self._format( (y[i][j],e[i][j]) ))
99
 
                f.write('\n')
100
 
            for j in range(0,ys[1] ):
101
 
                using = using + (' using 1:%d:%d' % (j+2,j+3),)
102
 
            #x is 2D
103
 
        else:
104
 
            for i in range(0,xs[0] ):
105
 
                for j in range(0,xs[1] ):
106
 
                    f.write( self._format( (x[i][j],y[i][j],e[i][j]) ) )
107
 
                f.write('\n')
108
 
            for j in range(0,xs[1] ):
109
 
                using = using + (' using %d:%d:%d' % (j*2+1,j*2+2,j*2+3),)
110
 
        if plotinfo == '':
111
 
            #defualt to printing with lines
112
 
            plotinfo = " notitle w lp  lt " +`self.errorbar_lt` + ' '
113
 
        cmd = ''
114
 
        # gnuplot doesn't seem to like backslashes
115
 
        fn =    string.replace(filename,'\\','/')
116
 
        for us in using:
117
 
            cmd = cmd + ' "' + fn + '" ' + us + 'notitle w errorbars lt ' +`self.errorbar_lt` + ', '
118
 
            cmd = cmd + ' "' + fn + '" ' + us + plotinfo + ', '
119
 
        self.errorbar_lt = (self.errorbar_lt + 1) % 15
120
 
        return cmd, filename
121
 
 
122
 
    def _errorbar_get_plot_group(self,data):
123
 
        group = ()
124
 
        remains = data
125
 
        state = 0
126
 
        finished = 0
127
 
        while(len(remains) > 0 and not finished):
128
 
            el = remains[0]
129
 
            if(state == 0):
130
 
                el = asarray(el)
131
 
                state = 1
132
 
            elif(state == 1):
133
 
                el = asarray(el)
134
 
                state = 2
135
 
            elif(state == 2):
136
 
                if(type(el) == StringType):
137
 
                    finished = 1
138
 
                else:
139
 
                    el = asarray(el)
140
 
                    state = 3
141
 
            elif(state == 3):
142
 
                finished = 1
143
 
                if(type(el) != StringType):
144
 
                    break
145
 
            group = group + (el,)
146
 
            remains = remains[1:]
147
 
        return group, remains