plotTools.py
Go to the documentation of this file.
1 '''
2 Created on Feb 25, 2014
3 
4 @author: waltj
5 '''
6 from pylab import plt
7 import matplotlib.dates as md
8 import datetime as dt
9 from mpl_toolkits.mplot3d import Axes3D
10 # import time as tm
11 
12 # import time as systime
13 # # Profiling code
14 # time1 = systime.time()
15 # # Profiling code
16 # time2 = systime.time()
17 # dtTime = time2 - time1
18 # print("Profiling time: %.2fs" % (dtTime))
19 
20 class cObj:
21  def __init__(self):
22  return
23 
24 class cPlot:
25  def __init__(self):
26  self.sharex = {} # set to -1 to disable zoom all plots x axis together
27  self.f = {} # dictionary of figures with axes
28  self.title = ''
29  self.units = ''
30  self.preTitle = None
31  self.timeIsUtc = 0
32 
33  def setPreTitle(self, serNum):
34  self.preTitle = serNum
35 
36  def labels(self, title='', units=''):
37  self.title = title;
38  self.units = units;
39 
40  def plot1(self, figNum, time, data, title='', units='', options=''):
41  plt.figure(figNum)
42  # plt.cla()
43 # plt.hold(True);
44  plt.grid(True)
45  if title:
46  self.title = title
47  if not units:
48  self.units = units
49  if self.preTitle:
50  fig = plt.gcf()
51  fig.canvas.set_window_title("Figure %d - %s" % (figNum,self.preTitle))
52  plt.title("%s"%(self.title))
53  plt.plot(time, data, options)
54  plt.ylabel('(%s)'%(self.units))
55  plt.xlabel('Time (s)')
56  plt.margins(0.04)
57 
58 
59  def plot2(self, figNum, time1, data1, time2, data2, title='', units='', options=''):
60  plt.figure(figNum)
61 # plt.hold(True);
62  plt.grid(True)
63  if title:
64  self.title = title
65  if not units:
66  self.units = units
67  # plt.cla()
68  if self.preTitle:
69  fig = plt.gcf()
70  fig.canvas.set_window_title("Figure %d - %s" % (figNum,self.preTitle))
71  plt.title("%s"%(self.title))
72  plt.plot(time1, data1, options)
73  plt.plot(time2, data2, options)
74  plt.ylabel('(%s)'%(self.units))
75  plt.xlabel('Time (s)')
76  plt.margins(0.04)
77 
78 
79  def plot3Axes(self, figNum, time, data, title='', units='', xlabel='Time (s)', options='', xlim=None, ylim=None):
80  fig = plt.figure(figNum)
81  # plt.cla()
82 
83  if title:
84  self.title = title
85  if units:
86  self.units = units
87  if self.preTitle:
88  fig.canvas.set_window_title("Figure %d - %s" % (figNum,self.preTitle))
89  if not figNum in self.sharex.keys():
90  self.sharex[figNum] = plt.subplot(3,1,1)
91 # plt.plot(time, data[:,0], options)
92 
93  # Plot 1
94  subplt = plt.subplot(3,1,1, sharex=self.sharex[figNum])
95 # plt.hold(True);
96  plt.grid(True)
97  plt.title("%s"%(self.title))
98  plt.ylabel('(%s)'%(self.units))
99  plt.xlabel(xlabel)
100  plt.margins(0.04)
101  if xlim:
102  subplt.set_xlim(xlim)
103  if ylim:
104  subplt.set_ylim(ylim)
105  if self.timeIsUtc:
106  dates=[dt.datetime.fromtimestamp(ts) for ts in time]
107  datenums=md.date2num(dates)
108  # plt.subplots_adjust(bottom=0.2)
109  plt.xticks( rotation=25 )
110  ax=plt.gca()
111  if self.timeIsUtc==2:
112  xfmt = md.DateFormatter('%H:%M:%S.%f')
113  else:
114  xfmt = md.DateFormatter('%H:%M:%S')
115  ax.xaxis.set_major_formatter(xfmt)
116  plt.plot(datenums, data[:,0], options)
117  else:
118  plt.plot(time, data[:,0], options)
119 
120  # Plot 2
121  subplt = plt.subplot(3,1,2, sharex=self.sharex[figNum])
122 # plt.hold(True);
123  plt.grid(True)
124  plt.ylabel('(%s)'%(self.units))
125  plt.xlabel(xlabel)
126  plt.margins(0.04)
127  if xlim:
128  subplt.set_xlim(xlim)
129  if ylim:
130  subplt.set_ylim(ylim)
131  if self.timeIsUtc:
132  dates=[dt.datetime.fromtimestamp(ts) for ts in time]
133  datenums=md.date2num(dates)
134  # plt.subplots_adjust(bottom=0.2)
135  plt.xticks( rotation=25 )
136  ax=plt.gca()
137  if self.timeIsUtc==2:
138  xfmt = md.DateFormatter('%H:%M:%S.%f')
139  else:
140  xfmt = md.DateFormatter('%H:%M:%S')
141  ax.xaxis.set_major_formatter(xfmt)
142  plt.plot(datenums, data[:,1], options)
143  else:
144  plt.plot(time, data[:,1], options)
145 
146  # Plot 3
147  subplt = plt.subplot(3,1,3, sharex=self.sharex[figNum])
148 # plt.hold(True);
149  plt.grid(True)
150  plt.ylabel('(%s)'%(self.units))
151  plt.xlabel(xlabel)
152  plt.margins(0.04)
153  if xlim:
154  subplt.set_xlim(xlim)
155  if ylim:
156  subplt.set_ylim(ylim)
157  if self.timeIsUtc:
158  dates=[dt.datetime.fromtimestamp(ts) for ts in time]
159  datenums=md.date2num(dates)
160  # plt.subplots_adjust(bottom=0.2)
161  plt.xticks( rotation=25 )
162  ax=plt.gca()
163  if self.timeIsUtc==2:
164  xfmt = md.DateFormatter('%H:%M:%S.%f')
165  else:
166  xfmt = md.DateFormatter('%H:%M:%S')
167  ax.xaxis.set_major_formatter(xfmt)
168  plt.plot(datenums, data[:,2], options)
169  else:
170  plt.plot(time, data[:,2], options)
171  # legend(['desire','actual','e/10','e2/10'])
172  return fig
173 
174  def plot3setYspan(self, figNum, yspan=None):
175  plt.figure(figNum)
176 
177  if yspan is None:
178  return
179 
180  if not figNum in self.sharex.keys():
181  self.sharex[figNum] = plt.subplot(3,1,1)
182 
183  # Plot 1
184  subplt = plt.subplot(3,1,1, sharex=self.sharex[figNum])
185  yl = subplt.get_ylim()
186  med = (yl[1] - yl[0])*0.5 + yl[0]
187  yl = [med-yspan*0.5,med+yspan*0.5]
188  subplt.set_ylim(yl)
189 
190  # Plot 2
191  subplt = plt.subplot(3,1,2, sharex=self.sharex[figNum])
192  yl = subplt.get_ylim()
193  med = (yl[1] - yl[0])*0.5 + yl[0]
194  yl = [med-yspan*0.5,med+yspan*0.5]
195  subplt.set_ylim(yl)
196 
197  # Plot 3
198  subplt = plt.subplot(3,1,3, sharex=self.sharex[figNum])
199  yl = subplt.get_ylim()
200  med = (yl[1] - yl[0])*0.5 + yl[0]
201  yl = [med-yspan*0.5,med+yspan*0.5]
202  subplt.set_ylim(yl)
203 
204  def plot3D(self, figNum, figTitle=''):
205  if not figNum in self.f.keys():
206  self.f[figNum] = cObj()
207  self.f[figNum].fig = plt.figure()
208  self.f[figNum].ax = self.f[figNum].fig.add_subplot(111, projection='3d')
209  self.f[figNum].fig.canvas.set_window_title("%s__%s" % (self.preTitle, figTitle))
210  return self.f[figNum].fig, self.f[figNum].ax
211 
212  def subplots(self, figNum, numRows, figTitle='', sharex=True, numCols=1):
213 # if not figNum in self.sharex.keys():
214 # self.sharex[figNum] = plt.subplot(numRows,1,plotNum)
215 # # plt.plot(time, data, options)
216  if not figNum in self.f.keys():
217  self.f[figNum] = cObj()
218  self.f[figNum].fig, self.f[figNum].ax = plt.subplots(numRows,numCols, sharex=sharex)
219 
220  self.f[figNum].fig.canvas.set_window_title("%s__%s" % (self.preTitle, figTitle))
221 
222  return self.f[figNum].fig, self.f[figNum].ax
223 
224  def subplotSingle(self, ax, time, data, title='', ylabel='', xlabel='', options=''):
225 
226 # print("subplotSingle ", title, " ", xlabel, " ", ylabel)
227 
228  if ylabel:
229  self.units = ylabel
230 
231 # plt.hold(True);
232  ax.grid(True)
233  if title:
234  ax.set_title("%s"%(title))
235  if xlabel:
236  ax.set_xlabel('(%s)'%(xlabel))
237  if ylabel:
238  ax.set_ylabel('(%s)'%(ylabel))
239  ax.margins(0.04)
240 
241  if self.timeIsUtc:
242  dates=[dt.datetime.fromtimestamp(ts) for ts in time]
243  datenums=md.date2num(dates)
244  # plt.subplots_adjust(bottom=0.2)
245  ax.xticks( rotation=25 )
246  if self.timeIsUtc==2:
247 # xfmt = md.DateFormatter('%H:%M:%S.%f')
248  xfmt = md.DateFormatter('%M:%S.%f')
249  else:
250 # xfmt = md.DateFormatter('%H:%M:%S')
251  xfmt = md.DateFormatter('%M:%S')
252  ax.xaxis.set_major_formatter(xfmt)
253  tm = datenums
254  else:
255  tm = time
256 
257  if isinstance(options, str):
258  ax.plot(time, data, options)
259  else:
260  ax.plot(time, data, **options)
261 
262 
263  def subplotSingle2x(self, figNum, plotNum, numRows, numCols, time, data, title='', units='', options=''):
264 
265  print("subplotSingle2x")
266 
267  plt.figure(figNum)
268  if title:
269  self.title = title
270  if not units:
271  self.units = units
272  if self.preTitle:
273  fig = plt.gcf()
274  fig.canvas.set_window_title("%s" % (figNum,self.preTitle))
275  if not figNum in self.sharex.keys():
276  self.sharex[figNum] = plt.subplot(numRows,numCols,plotNum)
277  plt.plot(time, data, options)
278 
279  plt.subplot(numRows,numCols,plotNum,sharex=self.sharex[figNum])
280 # plt.hold(True);
281  plt.grid(True)
282  plt.title("%s"%(self.title))
283  plt.plot(time, data, options)
284  plt.ylabel('(%s)'%(self.units))
285  plt.margins(0.04)
286 
287  def subplotSetYspan(self, ax, yspan):
288  yl = ax.get_ylim()
289  med = (yl[1] - yl[0])*0.5 + yl[0]
290  yl = [med-yspan*0.5,med+yspan*0.5]
291  ax.set_ylim(yl)
292 
293  def subplotSetYspanMin(self, ax, yspan):
294  yl = ax.get_ylim()
295  span = yl[1] - yl[0];
296  if span < yspan:
297  med = span*0.5 + yl[0]
298  yl = [med-yspan*0.5,med+yspan*0.5]
299  ax.set_ylim(yl)
300 
301  def plotNE(self, figNum, north, east, title='', units='', options=''):
302 
303  plt.figure(figNum)
304  # plt.cla()
305 # plt.hold(True);
306  plt.grid(True)
307  if title:
308  self.title = title
309  if not units:
310  self.units = units
311  if self.preTitle:
312  fig = plt.gcf()
313  fig.canvas.set_window_title("%s" % (self.preTitle))
314  plt.title("%s"%(self.title))
315  plt.plot(east, north, options)
316  plt.xlabel('East (%s)' %(self.units))
317  plt.ylabel('North (%s)'%(self.units))
318 
319 
def plot3D(self, figNum, figTitle='')
Definition: plotTools.py:204
def labels(self, title='', units='')
Definition: plotTools.py:36
def subplotSingle(self, ax, time, data, title='', ylabel='', xlabel='', options='')
Definition: plotTools.py:224
def plot1(self, figNum, time, data, title='', units='', options='')
Definition: plotTools.py:40
def subplotSingle2x(self, figNum, plotNum, numRows, numCols, time, data, title='', units='', options='')
Definition: plotTools.py:263
def plot3Axes(self, figNum, time, data, title='', units='', xlabel='Time(s)', options='', xlim=None, ylim=None)
Definition: plotTools.py:79
def subplotSetYspanMin(self, ax, yspan)
Definition: plotTools.py:293
def subplotSetYspan(self, ax, yspan)
Definition: plotTools.py:287
def setPreTitle(self, serNum)
Definition: plotTools.py:33
def subplots(self, figNum, numRows, figTitle='', sharex=True, numCols=1)
Definition: plotTools.py:212
def plotNE(self, figNum, north, east, title='', units='', options='')
Definition: plotTools.py:301
def plot3setYspan(self, figNum, yspan=None)
Definition: plotTools.py:174
def plot2(self, figNum, time1, data1, time2, data2, title='', units='', options='')
Definition: plotTools.py:59


inertial_sense_ros
Author(s):
autogenerated on Sat Sep 19 2020 03:19:04