@@ -1,240 +1,245 | |||
|
1 | 1 | import numpy |
|
2 | 2 | import mpldriver |
|
3 | 3 | |
|
4 | 4 | |
|
5 | 5 | class Figure: |
|
6 | 6 | |
|
7 | 7 | __driver = mpldriver |
|
8 | fig = None | |
|
8 | 9 | |
|
9 | 10 | idfigure = None |
|
10 | 11 | wintitle = None |
|
11 | 12 | width = None |
|
12 | 13 | height = None |
|
13 | 14 | nplots = None |
|
14 | 15 | |
|
15 | 16 | axesObjList = [] |
|
16 | 17 | |
|
17 | 18 | WIDTH = None |
|
18 | 19 | HEIGHT = None |
|
19 | 20 | |
|
20 | 21 | def __init__(self): |
|
21 | 22 | |
|
22 | 23 | raise ValueError, "This method is not implemented" |
|
23 | 24 | |
|
25 | def __del__(self): | |
|
26 | ||
|
27 | self.__driver.closeFigure() | |
|
28 | ||
|
24 | 29 | def getAxesObjList(self): |
|
25 | 30 | |
|
26 | 31 | return self.axesObjList |
|
27 | 32 | |
|
28 | 33 | def getSubplots(self): |
|
29 | 34 | |
|
30 | 35 | raise ValueError, "Abstract method: This method should be defined" |
|
31 | 36 | |
|
32 | 37 | def getScreenDim(self): |
|
33 | 38 | |
|
34 | 39 | nrow, ncol = self.getSubplots() |
|
35 | 40 | |
|
36 | 41 | width = self.WIDTH*ncol |
|
37 | 42 | height = self.HEIGHT*nrow |
|
38 | 43 | |
|
39 | 44 | return width, height |
|
40 | 45 | |
|
41 | 46 | def init(self, idfigure, nplots, wintitle): |
|
42 | 47 | |
|
43 | 48 | raise ValueError, "This method has been replaced with createFigure" |
|
44 | 49 | |
|
45 | 50 | def createFigure(self, idfigure, wintitle): |
|
46 | 51 | |
|
47 | 52 | """ |
|
48 | 53 | Crea la figura de acuerdo al driver y parametros seleccionados seleccionados. |
|
49 | 54 | Las dimensiones de la pantalla es calculada a partir de los atributos self.WIDTH |
|
50 | 55 | y self.HEIGHT y el numero de subplots (nrow, ncol) |
|
51 | 56 | |
|
52 | 57 | Input: |
|
53 | 58 | idfigure : Los parametros necesarios son |
|
54 | 59 | wintitle : |
|
55 | 60 | |
|
56 | 61 | """ |
|
57 | 62 | |
|
58 | 63 | self.idfigure = idfigure |
|
59 | 64 | |
|
60 | 65 | self.wintitle = wintitle |
|
61 | 66 | |
|
62 | 67 | self.width, self.height = self.getScreenDim() |
|
63 | 68 | |
|
64 | 69 | self.fig = self.__driver.createFigure(self.idfigure, |
|
65 | 70 | self.wintitle, |
|
66 | 71 | self.width, |
|
67 | 72 | self.height) |
|
68 | 73 | |
|
69 | 74 | self.axesObjList = [] |
|
70 | 75 | |
|
71 | 76 | def setDriver(self, driver=mpldriver): |
|
72 | 77 | |
|
73 | 78 | self.__driver = driver |
|
74 | 79 | |
|
75 | 80 | def setTitle(self, title): |
|
76 | 81 | |
|
77 | 82 | self.__driver.setTitle(self.fig, title) |
|
78 | 83 | |
|
79 | 84 | def setWinTitle(self, title): |
|
80 | 85 | |
|
81 | 86 | self.__driver.setWinTitle(self.fig, title=title) |
|
82 | 87 | |
|
83 | 88 | def setTextFromAxes(self, text): |
|
84 | 89 | |
|
85 | 90 | raise ValueError, "Este metodo ha sido reemplazaado con el metodo setText de la clase Axes" |
|
86 | 91 | |
|
87 | 92 | def makeAxes(self, nrow, ncol, xpos, ypos, colspan, rowspan): |
|
88 | 93 | |
|
89 | 94 | raise ValueError, "Este metodo ha sido reemplazaado con el metodo addAxes" |
|
90 | 95 | |
|
91 | 96 | def addAxes(self, *args): |
|
92 | 97 | """ |
|
93 | 98 | |
|
94 | 99 | Input: |
|
95 | 100 | *args : Los parametros necesarios son |
|
96 | 101 | nrow, ncol, xpos, ypos, colspan, rowspan |
|
97 | 102 | """ |
|
98 | 103 | |
|
99 | 104 | axesObj = Axes(self.fig, *args) |
|
100 | 105 | self.axesObjList.append(axesObj) |
|
101 | 106 | |
|
102 | 107 | def draw(self): |
|
103 | 108 | |
|
104 | 109 | self.__driver.draw(self.fig) |
|
105 | 110 | |
|
106 | 111 | def run(self): |
|
107 | 112 | |
|
108 | 113 | raise ValueError, "This method is not implemented" |
|
109 | 114 | |
|
110 | 115 | axesList = property(getAxesObjList) |
|
111 | 116 | |
|
112 | 117 | |
|
113 | 118 | class Axes: |
|
114 | 119 | |
|
115 | 120 | __driver = mpldriver |
|
116 | 121 | fig = None |
|
117 | 122 | ax = None |
|
118 | 123 | plot = None |
|
119 | 124 | |
|
120 | 125 | firsttime = None |
|
121 | 126 | |
|
122 | 127 | __showprofile = False |
|
123 | 128 | |
|
124 | 129 | def __init__(self, *args): |
|
125 | 130 | |
|
126 | 131 | """ |
|
127 | 132 | |
|
128 | 133 | Input: |
|
129 | 134 | *args : Los parametros necesarios son |
|
130 | 135 | fig, nrow, ncol, xpos, ypos, colspan, rowspan |
|
131 | 136 | """ |
|
132 | 137 | |
|
133 | 138 | ax = self.__driver.createAxes(*args) |
|
134 | 139 | self.fig = args[0] |
|
135 | 140 | self.ax = ax |
|
136 | 141 | self.plot = None |
|
137 | 142 | |
|
138 | 143 | self.firsttime = True |
|
139 | 144 | |
|
140 | 145 | def setText(self, text): |
|
141 | 146 | |
|
142 | 147 | self.__driver.setAxesText(self.ax, text) |
|
143 | 148 | |
|
144 | 149 | def pline(self, x, y, |
|
145 | 150 | xmin=None, xmax=None, |
|
146 | 151 | ymin=None, ymax=None, |
|
147 | 152 | xlabel='', ylabel='', |
|
148 | 153 | title='', |
|
149 | 154 | **kwargs): |
|
150 | 155 | |
|
151 | 156 | """ |
|
152 | 157 | |
|
153 | 158 | Input: |
|
154 | 159 | x : |
|
155 | 160 | y : |
|
156 | 161 | xmin : |
|
157 | 162 | xmax : |
|
158 | 163 | ymin : |
|
159 | 164 | ymax : |
|
160 | 165 | xlabel : |
|
161 | 166 | ylabel : |
|
162 | 167 | title : |
|
163 | 168 | **kwargs : Los parametros aceptados son |
|
164 | 169 | |
|
165 | 170 | ticksize |
|
166 | 171 | ytick_visible |
|
167 | 172 | """ |
|
168 | 173 | |
|
169 | 174 | if self.firsttime: |
|
170 | 175 | |
|
171 | 176 | if xmin == None: xmin = numpy.nanmin(x) |
|
172 | 177 | if xmax == None: xmax = numpy.nanmax(x) |
|
173 | 178 | if ymin == None: ymin = numpy.nanmin(y) |
|
174 | 179 | if ymax == None: ymax = numpy.nanmax(y) |
|
175 | 180 | |
|
176 | 181 | self.plot = self.__driver.createPline(self.ax, x, y, |
|
177 | 182 | xmin, xmax, |
|
178 | 183 | ymin, ymax, |
|
179 | 184 | xlabel=xlabel, |
|
180 | 185 | ylabel=ylabel, |
|
181 | 186 | title=title, |
|
182 | 187 | **kwargs) |
|
183 | 188 | self.firsttime = False |
|
184 | 189 | return |
|
185 | 190 | |
|
186 | 191 | self.__driver.pline(self.plot, x, y, xlabel=xlabel, |
|
187 | 192 | ylabel=ylabel, |
|
188 | 193 | title=title) |
|
189 | 194 | |
|
190 | 195 | |
|
191 | 196 | def pcolor(self, x, y, z, |
|
192 | 197 | xmin=None, xmax=None, |
|
193 | 198 | ymin=None, ymax=None, |
|
194 | 199 | zmin=None, zmax=None, |
|
195 | 200 | xlabel='', ylabel='', |
|
196 | 201 | title='', |
|
197 | 202 | **kwargs): |
|
198 | 203 | |
|
199 | 204 | """ |
|
200 | 205 | Input: |
|
201 | 206 | x : |
|
202 | 207 | y : |
|
203 | 208 | x : |
|
204 | 209 | xmin : |
|
205 | 210 | xmax : |
|
206 | 211 | ymin : |
|
207 | 212 | ymax : |
|
208 | 213 | zmin : |
|
209 | 214 | zmax : |
|
210 | 215 | xlabel : |
|
211 | 216 | ylabel : |
|
212 | 217 | title : |
|
213 | 218 | **kwargs : Los parametros aceptados son |
|
214 | 219 | ticksize=9, |
|
215 | 220 | cblabel='' |
|
216 | 221 | """ |
|
217 | 222 | |
|
218 | 223 | if self.firsttime: |
|
219 | 224 | |
|
220 | 225 | if xmin == None: xmin = numpy.nanmin(x) |
|
221 | 226 | if xmax == None: xmax = numpy.nanmax(x) |
|
222 | 227 | if ymin == None: ymin = numpy.nanmin(y) |
|
223 | 228 | if ymax == None: ymax = numpy.nanmax(y) |
|
224 | 229 | if zmin == None: zmin = numpy.nanmin(z) |
|
225 | 230 | if zmax == None: zmax = numpy.nanmax(z) |
|
226 | 231 | |
|
227 | 232 | self.plot = self.__driver.createPcolor(self.ax, x, y, z, |
|
228 | 233 | xmin, xmax, |
|
229 | 234 | ymin, ymax, |
|
230 | 235 | zmin, zmax, |
|
231 | 236 | xlabel=xlabel, |
|
232 | 237 | ylabel=ylabel, |
|
233 | 238 | title=title, |
|
234 | 239 | **kwargs) |
|
235 | 240 | self.firsttime = False |
|
236 | 241 | return |
|
237 | 242 | |
|
238 | 243 | mesh = self.__driver.pcolor(self.plot, z, xlabel=xlabel, |
|
239 | 244 | ylabel=ylabel, |
|
240 | 245 | title=title) |
@@ -1,239 +1,246 | |||
|
1 | 1 | import numpy |
|
2 | 2 | import matplotlib |
|
3 | 3 | matplotlib.use("TKAgg") |
|
4 | 4 | import matplotlib.pyplot |
|
5 | 5 | #import scitools.numpyutils |
|
6 | 6 | from mpl_toolkits.axes_grid1 import make_axes_locatable |
|
7 | 7 | |
|
8 | 8 | def init(idfigure, wintitle, width, height, facecolor="w"): |
|
9 | 9 | |
|
10 | 10 | matplotlib.pyplot.ioff() |
|
11 | 11 | fig = matplotlib.pyplot.matplotlib.pyplot.figure(num=idfigure, facecolor=facecolor) |
|
12 | 12 | fig.canvas.manager.set_window_title(wintitle) |
|
13 | 13 | fig.canvas.manager.resize(width, height) |
|
14 | 14 | matplotlib.pyplot.ion() |
|
15 | 15 | |
|
16 | 16 | return fig |
|
17 | 17 | |
|
18 | 18 | def setWinTitle(fig, title): |
|
19 | 19 | |
|
20 | 20 | fig.canvas.manager.set_window_title(title) |
|
21 | 21 | |
|
22 | 22 | def setTitle(idfigure, title): |
|
23 | 23 | fig = matplotlib.pyplot.figure(idfigure) |
|
24 | 24 | fig.suptitle(title) |
|
25 | 25 | |
|
26 | 26 | def makeAxes(idfigure, nrow, ncol, xpos, ypos, colspan, rowspan): |
|
27 | 27 | fig = matplotlib.pyplot.figure(idfigure) |
|
28 | 28 | ax = matplotlib.pyplot.subplot2grid((nrow, ncol), (xpos, ypos), colspan=colspan, rowspan=rowspan) |
|
29 | 29 | return ax |
|
30 | 30 | |
|
31 | 31 | def setTextFromAxes(idfigure, ax, title): |
|
32 | 32 | fig = matplotlib.pyplot.figure(idfigure) |
|
33 | 33 | ax.annotate(title, xy=(.1, .99), |
|
34 | 34 | xycoords='figure fraction', |
|
35 | 35 | horizontalalignment='left', verticalalignment='top', |
|
36 | 36 | fontsize=10) |
|
37 | 37 | |
|
38 | 38 | def pline(ax, x, y, xmin, xmax, ymin, ymax, xlabel, ylabel, title, firsttime): |
|
39 | 39 | |
|
40 | 40 | if firsttime: |
|
41 | 41 | ax.plot(x, y) |
|
42 | 42 | ax.set_xlim([xmin,xmax]) |
|
43 | 43 | ax.set_ylim([ymin,ymax]) |
|
44 | 44 | ax.set_xlabel(xlabel, size=8) |
|
45 | 45 | ax.set_ylabel(ylabel, size=8) |
|
46 | 46 | ax.set_title(title, size=10) |
|
47 | 47 | matplotlib.pyplot.tight_layout() |
|
48 | 48 | else: |
|
49 | 49 | ax.lines[0].set_data(x,y) |
|
50 | 50 | |
|
51 | 51 | def draw(idfigure): |
|
52 | 52 | |
|
53 | 53 | fig = matplotlib.pyplot.figure(idfigure) |
|
54 | 54 | fig.canvas.draw() |
|
55 | 55 | |
|
56 | 56 | def pcolor(ax, x, y, z, xmin, xmax, ymin, ymax, zmin, zmax, xlabel, ylabel, title, firsttime, mesh): |
|
57 | 57 | |
|
58 | 58 | if firsttime: |
|
59 | 59 | divider = make_axes_locatable(ax) |
|
60 | 60 | ax_cb = divider.new_horizontal(size="4%", pad=0.05) |
|
61 | 61 | fig1 = ax.get_figure() |
|
62 | 62 | fig1.add_axes(ax_cb) |
|
63 | 63 | |
|
64 | 64 | ax.set_xlim([xmin,xmax]) |
|
65 | 65 | ax.set_ylim([ymin,ymax]) |
|
66 | 66 | ax.set_xlabel(xlabel) |
|
67 | 67 | ax.set_ylabel(ylabel) |
|
68 | 68 | ax.set_title(title) |
|
69 | 69 | print x |
|
70 | 70 | imesh=ax.pcolormesh(x,y,z.T,vmin=zmin,vmax=zmax) |
|
71 | 71 | matplotlib.pyplot.colorbar(imesh, cax=ax_cb) |
|
72 | 72 | ax_cb.yaxis.tick_right() |
|
73 | 73 | for tl in ax_cb.get_yticklabels(): |
|
74 | 74 | tl.set_visible(True) |
|
75 | 75 | ax_cb.yaxis.tick_right() |
|
76 | 76 | matplotlib.pyplot.tight_layout() |
|
77 | 77 | return imesh |
|
78 | 78 | else: |
|
79 | 79 | # ax.set_xlim([xmin,xmax]) |
|
80 | 80 | # ax.set_ylim([ymin,ymax]) |
|
81 | 81 | ax.set_xlabel(xlabel) |
|
82 | 82 | ax.set_ylabel(ylabel) |
|
83 | 83 | ax.set_title(title) |
|
84 | 84 | |
|
85 | 85 | z = z.T |
|
86 | 86 | # z = z[0:-1,0:-1] |
|
87 | 87 | mesh.set_array(z.ravel()) |
|
88 | 88 | |
|
89 | 89 | return mesh |
|
90 | 90 | |
|
91 | 91 | ########################################### |
|
92 | 92 | #Actualizacion de las funciones del driver |
|
93 | 93 | ########################################### |
|
94 | 94 | |
|
95 | 95 | def createFigure(idfigure, wintitle, width, height, facecolor="w"): |
|
96 | 96 | |
|
97 | 97 | matplotlib.pyplot.ioff() |
|
98 | 98 | fig = matplotlib.pyplot.matplotlib.pyplot.figure(num=idfigure, facecolor=facecolor) |
|
99 | 99 | fig.canvas.manager.set_window_title(wintitle) |
|
100 | 100 | fig.canvas.manager.resize(width, height) |
|
101 | 101 | matplotlib.pyplot.ion() |
|
102 | 102 | |
|
103 | 103 | return fig |
|
104 | 104 | |
|
105 | def closeFigure(): | |
|
106 | ||
|
107 | matplotlib.pyplot.ioff() | |
|
108 | matplotlib.pyplot.show() | |
|
109 | ||
|
110 | retur | |
|
111 | ||
|
105 | 112 | def setWinTitle(fig, title): |
|
106 | 113 | |
|
107 | 114 | fig.canvas.manager.set_window_title(title) |
|
108 | 115 | |
|
109 | 116 | def setTitle(fig, title): |
|
110 | 117 | |
|
111 | 118 | fig.suptitle(title) |
|
112 | 119 | |
|
113 | 120 | def createAxes(fig, nrow, ncol, xpos, ypos, colspan, rowspan): |
|
114 | 121 | |
|
115 | 122 | matplotlib.pyplot.figure(fig.number) |
|
116 | 123 | axes = matplotlib.pyplot.subplot2grid((nrow, ncol), |
|
117 | 124 | (xpos, ypos), |
|
118 | 125 | colspan=colspan, |
|
119 | 126 | rowspan=rowspan) |
|
120 | 127 | return axes |
|
121 | 128 | |
|
122 | 129 | def setAxesText(ax, text): |
|
123 | 130 | |
|
124 | 131 | ax.annotate(text, |
|
125 | 132 | xy = (.1, .99), |
|
126 | 133 | xycoords = 'figure fraction', |
|
127 | 134 | horizontalalignment = 'left', |
|
128 | 135 | verticalalignment = 'top', |
|
129 | 136 | fontsize = 10) |
|
130 | 137 | |
|
131 | 138 | def printLabels(ax, xlabel, ylabel, title): |
|
132 | 139 | |
|
133 | 140 | ax.set_xlabel(xlabel, size=11) |
|
134 | 141 | ax.set_ylabel(ylabel, size=11) |
|
135 | 142 | ax.set_title(title, size=12) |
|
136 | 143 | |
|
137 | 144 | def createPline(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='', |
|
138 | 145 | ticksize=9, xtick_visible=True, ytick_visible=True, |
|
139 | 146 | nxticks=4, nyticks=10, |
|
140 | 147 | grid=None): |
|
141 | 148 | |
|
142 | 149 | """ |
|
143 | 150 | |
|
144 | 151 | Input: |
|
145 | 152 | grid : None, 'both', 'x', 'y' |
|
146 | 153 | """ |
|
147 | 154 | |
|
148 | 155 | ax.plot(x, y) |
|
149 | 156 | ax.set_xlim([xmin,xmax]) |
|
150 | 157 | ax.set_ylim([ymin,ymax]) |
|
151 | 158 | |
|
152 | 159 | printLabels(ax, xlabel, ylabel, title) |
|
153 | 160 | |
|
154 | 161 | ###################################################### |
|
155 | 162 | xtickspos = numpy.arange(nxticks)*int((xmax-xmin)/nxticks) + int(xmin) |
|
156 | 163 | ax.set_xticks(xtickspos) |
|
157 | 164 | |
|
158 | 165 | for tick in ax.get_xticklabels(): |
|
159 | 166 | tick.set_visible(xtick_visible) |
|
160 | 167 | |
|
161 | 168 | for tick in ax.xaxis.get_major_ticks(): |
|
162 | 169 | tick.label.set_fontsize(ticksize) |
|
163 | 170 | |
|
164 | 171 | ###################################################### |
|
165 | 172 | for tick in ax.get_yticklabels(): |
|
166 | 173 | tick.set_visible(ytick_visible) |
|
167 | 174 | |
|
168 | 175 | for tick in ax.yaxis.get_major_ticks(): |
|
169 | 176 | tick.label.set_fontsize(ticksize) |
|
170 | 177 | |
|
171 | 178 | ###################################################### |
|
172 | 179 | if grid != None: |
|
173 | 180 | ax.grid(b=True, which='major', axis=grid) |
|
174 | 181 | |
|
175 | 182 | matplotlib.pyplot.tight_layout() |
|
176 | 183 | |
|
177 | 184 | iplot = ax.lines[-1] |
|
178 | 185 | |
|
179 | 186 | return iplot |
|
180 | 187 | |
|
181 | 188 | def pline(iplot, x, y, xlabel='', ylabel='', title=''): |
|
182 | 189 | |
|
183 | 190 | ax = iplot.get_axes() |
|
184 | 191 | |
|
185 | 192 | printLabels(ax, xlabel, ylabel, title) |
|
186 | 193 | |
|
187 | 194 | iplot.set_data(x, y) |
|
188 | 195 | |
|
189 | 196 | def createPcolor(ax, x, y, z, xmin, xmax, ymin, ymax, zmin, zmax, xlabel='', ylabel='', title='', ticksize = 9, cblabel=''): |
|
190 | 197 | |
|
191 | 198 | divider = make_axes_locatable(ax) |
|
192 | 199 | ax_cb = divider.new_horizontal(size="4%", pad=0.05) |
|
193 | 200 | fig = ax.get_figure() |
|
194 | 201 | fig.add_axes(ax_cb) |
|
195 | 202 | |
|
196 | 203 | ax.set_xlim([xmin,xmax]) |
|
197 | 204 | ax.set_ylim([ymin,ymax]) |
|
198 | 205 | |
|
199 | 206 | printLabels(ax, xlabel, ylabel, title) |
|
200 | 207 | |
|
201 | 208 | imesh = ax.pcolormesh(x,y,z.T,vmin=zmin,vmax=zmax) |
|
202 | 209 | cb = matplotlib.pyplot.colorbar(imesh, cax=ax_cb) |
|
203 | 210 | cb.set_label(cblabel) |
|
204 | 211 | |
|
205 | 212 | ax_cb.yaxis.tick_right() |
|
206 | 213 | |
|
207 | 214 | for tl in ax_cb.get_yticklabels(): |
|
208 | 215 | tl.set_visible(True) |
|
209 | 216 | |
|
210 | 217 | for tick in ax.yaxis.get_major_ticks(): |
|
211 | 218 | tick.label.set_fontsize(ticksize) |
|
212 | 219 | |
|
213 | 220 | for tick in ax.xaxis.get_major_ticks(): |
|
214 | 221 | tick.label.set_fontsize(ticksize) |
|
215 | 222 | |
|
216 | 223 | for tick in cb.ax.get_yticklabels(): |
|
217 | 224 | tick.set_fontsize(ticksize) |
|
218 | 225 | |
|
219 | 226 | ax_cb.yaxis.tick_right() |
|
220 | 227 | matplotlib.pyplot.tight_layout() |
|
221 | 228 | |
|
222 | 229 | return imesh |
|
223 | 230 | |
|
224 | 231 | def pcolor(imesh, z, xlabel='', ylabel='', title=''): |
|
225 | 232 | |
|
226 | 233 | z = z.T |
|
227 | 234 | |
|
228 | 235 | ax = imesh.get_axes() |
|
229 | 236 | |
|
230 | 237 | printLabels(ax, xlabel, ylabel, title) |
|
231 | 238 | |
|
232 | 239 | imesh.set_array(z.ravel()) |
|
233 | 240 | |
|
234 | 241 | def draw(fig): |
|
235 | 242 | |
|
236 | 243 | if type(fig) == 'int': |
|
237 | 244 | raise ValueError, "This parameter should be of tpye matplotlib figure" |
|
238 | 245 | |
|
239 | 246 | fig.canvas.draw() No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now