Changeset 7056 for misc/tools/plotcsv/plotcsv.py
- Timestamp:
- May 19, 2009, 10:32:10 AM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
misc/tools/plotcsv/plotcsv.py
r6897 r7056 15 15 import string 16 16 try: 17 import cpickle 17 import cpickle as pickle 18 18 except: 19 19 import pickle … … 54 54 # program name and version 55 55 APP_NAME = 'plotcsv' 56 APP_VERSION = '0. 5'56 APP_VERSION = '0.7' 57 57 58 58 # name of the configuration filename … … 73 73 BOX_CSV_HEIGHT = 265 74 74 BOX_PLOT_WIDTH = 400 75 BOX_PLOT_HEIGHT = 140 75 BOX_PLOT_HEIGHT = 140+25+25+30 76 76 77 77 TXT_CSVFILE_WIDTH = 390 … … 84 84 85 85 FORM_WIDTH = BOX_WIDTH + 15 86 FORM_HEIGHT = 450 86 FORM_HEIGHT = 450+25+25+30 87 87 88 88 START_YOFFSET = 7 … … 206 206 self.changed = True 207 207 208 def get(self, key, default=None): 209 """Override to allow: <var> = cfg.get(<key>, default=None)""" 210 211 return self.cfgdict.get(key, default) 212 208 213 def __getitem__(self, key): 209 214 """Override to allow: <var> = cfg[<key>]""" … … 306 311 # @brief Plot data files. 307 312 # @param filenames List of full pathnames to plot. 308 # @param x_hdr The X axis labelstring.309 # @param y_hdr The Y axis labelstring.313 # @param x_hdr The X axis data header string. 314 # @param y_hdr The Y axis data header string. 310 315 # @param title The string used to title the graph. 311 316 # @param legend True if a legend is to be displayed. 312 317 # @param legend_path True if legend is to contain full file paths. 318 # @param grid True if grid is to be displayed. 319 # @param fontsize Point size of font. 320 # @param plot_width Width of plot lines. 321 # @param x_label Override X axis label. 322 # @param y_label Override Y axis label. 313 323 def plot_files(filenames, x_hdr, y_hdr, title='', legend=False, 314 legend_path=False): 324 legend_path=False, grid=False, fontsize=10, 325 plot_width=2, x_label=None, y_label=None): 326 if x_label is None: 327 x_label = x_hdr 328 if y_label is None: 329 y_label = y_hdr 330 315 331 pylab.rc('axes', linewidth=2) 316 pylab.xlabel(x_hdr.title()) 317 pylab.ylabel(y_hdr.title()) 318 pylab.grid(True) 332 pylab.rc('lines', linewidth=float(plot_width)) 333 pylab.rc(('xtick', 'ytick'), labelsize=float(fontsize)*0.75,) 334 pylab.rc(('xtick.major', 'ytick.major'), size=5, pad=5) 335 pylab.rc('axes', labelsize=float(fontsize)) 336 pylab.xlabel(x_label) 337 pylab.ylabel(y_label) 338 339 pylab.grid(grid) 319 340 320 341 for f in filenames: … … 328 349 329 350 if title: 330 pylab.title(title) 351 pylab.title(title, fontsize=float(fontsize)*1.5) 352 331 353 if legend: 354 pylab.rc('legend', fancybox=True) 332 355 (min_y, max_y) = pylab.ylim() 333 356 range_y = max_y - min_y … … 383 406 size=(100, BUTTON_HEIGHT)) 384 407 x = FORM_WIDTH/2 + DOUBLE_BUTTON_OFFSET 385 self.btnDelCSVFile = wx.Button(p, label="Delete ", pos=(x, Y_OFFSET),408 self.btnDelCSVFile = wx.Button(p, label="Delete All", pos=(x, Y_OFFSET), 386 409 size=(100, BUTTON_HEIGHT)) 387 410 Y_OFFSET += BUTTON_HEIGHT + MARGIN … … 390 413 size=(BOX_PLOT_WIDTH, BOX_PLOT_HEIGHT)) 391 414 Y_OFFSET += GEN_DELTAY 415 392 416 wx.StaticText(p, -1, 'X-Column', 393 417 pos=(COLLAB_X_OFFSET, Y_OFFSET+LAB_CTRL_OFFSET), style=wx.ALIGN_LEFT) 394 self.cbXColHdr = wx.Choice(p, -1, pos=(COLLAB_X_OFFSET+ 65, Y_OFFSET),418 self.cbXColHdr = wx.Choice(p, -1, pos=(COLLAB_X_OFFSET+50, Y_OFFSET), 395 419 size=(80, -1)) 396 420 self.XColHdr = [] 397 421 wx.StaticText(p, -1, 'Y-Column', 398 pos=(FORM_WIDTH/2 +COLLAB_X_OFFSET,422 pos=(FORM_WIDTH/2, 399 423 Y_OFFSET+LAB_CTRL_OFFSET), style=wx.ALIGN_LEFT) 400 x = FORM_WIDTH/2 + COLLAB_X_OFFSET401 self.cbYColHdr = wx.Choice(p, -1, pos=(x +65, Y_OFFSET), size=(80, -1))424 x = FORM_WIDTH/2+50 425 self.cbYColHdr = wx.Choice(p, -1, pos=(x, Y_OFFSET), size=(80, -1)) 402 426 self.YColHdr = [] 403 Y_OFFSET += GEN_DELTAY*2 427 Y_OFFSET += GEN_DELTAY*1.5 428 429 wx.StaticText(p, -1, 'X-Label', 430 pos=(COLLAB_X_OFFSET, Y_OFFSET+LAB_CTRL_OFFSET), style=wx.ALIGN_LEFT) 431 self.txtXLabel = wx.TextCtrl(p, -1, "", size=(100, -1), 432 pos=(COLLAB_X_OFFSET+50, 433 Y_OFFSET+LAB_CTRL_OFFSET), 434 style=wx.ALIGN_LEFT) 435 wx.StaticText(p, -1, 'Y-Label', 436 pos=(FORM_WIDTH/2, 437 Y_OFFSET+LAB_CTRL_OFFSET), style=wx.ALIGN_LEFT) 438 x = FORM_WIDTH/2 + 50 439 self.txtYLabel = wx.TextCtrl(p, -1, "", size=(100, -1), 440 pos=(x, 441 Y_OFFSET+LAB_CTRL_OFFSET), 442 style=wx.ALIGN_LEFT) 443 Y_OFFSET += GEN_DELTAY*2.5 404 444 405 445 self.chkLegend = wx.CheckBox(p, -1, " Show graph legend", … … 407 447 self.chkLegendPath = wx.CheckBox(p, -1, " Full file path in legend", 408 448 pos=(COLLAB_X_OFFSET+180, Y_OFFSET)) 449 Y_OFFSET += GEN_DELTAY*2 450 451 font_sizes = ['10', '12', '14', '16', '18', '20', 452 '22', '24', '26', '28', '30', '32'] 453 self.cbLabFontSize = wx.ComboBox(p, -1, "10", 454 (COLLAB_X_OFFSET+20, Y_OFFSET-5), 455 (47, -1), font_sizes, 456 wx.CB_DROPDOWN) 457 wx.StaticText(p, -1, 'Label fontsize', 458 pos=(COLLAB_X_OFFSET+72, Y_OFFSET), 459 style=wx.ALIGN_LEFT) 460 461 self.chkShowGrid = wx.CheckBox(p, -1, " Show grid", 462 pos=(COLLAB_X_OFFSET+180, Y_OFFSET)) 463 464 Y_OFFSET += GEN_DELTAY*2 465 466 plot_widths = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'] 467 self.cbPlotWidth = wx.ComboBox(p, -1, "2", 468 (COLLAB_X_OFFSET+20, Y_OFFSET-5), 469 (47, -1), plot_widths, 470 wx.CB_DROPDOWN) 471 wx.StaticText(p, -1, 'Plot width', 472 pos=(COLLAB_X_OFFSET+72, Y_OFFSET), 473 style=wx.ALIGN_LEFT) 409 474 Y_OFFSET += GEN_DELTAY + 3 410 475 … … 415 480 Y_OFFSET+LAB_CTRL_OFFSET), 416 481 style=wx.ALIGN_LEFT) 417 418 Y_OFFSET += BUTTON_HEIGHT + MARGIN 482 Y_OFFSET += BUTTON_HEIGHT # + MARGIN 419 483 x = FORM_WIDTH/2 - BUTTON_WIDTH/2 420 484 self.btnPlot = wx.Button(p, label="Plot", pos=(x, Y_OFFSET), … … 426 490 self.Bind(wx.EVT_BUTTON, self.PlotFiles, self.btnPlot) 427 491 self.Bind(wx.EVT_CHECKBOX, self.ChangeLegend, self.chkLegend) 492 493 self.Bind(wx.EVT_CHOICE, self.ChangeXLabel, self.cbXColHdr) 494 self.Bind(wx.EVT_CHOICE, self.ChangeYLabel, self.cbYColHdr) 428 495 429 496 self.Bind(wx.EVT_CLOSE, self.doStateSave) … … 434 501 self.doStateRestore() 435 502 self.updateHdrChoices() 436 503 504 def ChangeXLabel(self, event): 505 sel = self.cbXColHdr.GetCurrentSelection() 506 self.txtXLabel.SetValue(self.cbXColHdr.GetItems()[sel].title()) 507 508 def ChangeYLabel(self, event): 509 sel = self.cbYColHdr.GetCurrentSelection() 510 self.txtYLabel.SetValue(self.cbYColHdr.GetItems()[sel].title()) 511 437 512 def AddCSVFile(self, event): 438 513 """Add a CSV file to the listbox""" … … 442 517 defaultFile='', 443 518 wildcard=CSVWildcard, 444 style=wx.OPEN | wx. CHANGE_DIR)519 style=wx.OPEN | wx.MULTIPLE | wx.CHANGE_DIR) 445 520 446 521 if dlg.ShowModal() == wx.ID_OK: 447 522 # This returns a list of files that were selected. 448 path = dlg.GetPath()523 files = dlg.GetFilenames() 449 524 self.file_dir = dlg.GetDirectory() 450 525 dlg.Destroy() 451 if path not in self.CSVFiles: 452 headers = self.getHeaders(path) 453 if headers: 454 common_headers = self.orHeaders(headers) 455 if not common_headers or len(common_headers) < 2: 456 self.error("Sorry, file '%s' doesn't have enough headers in common with current files" % path) 526 for f in files: 527 path = os.path.join(self.file_dir, f) 528 if path not in self.CSVFiles: 529 headers = self.getHeaders(path) 530 if headers: 531 common_headers = self.orHeaders(headers) 532 if not common_headers or len(common_headers) < 2: 533 self.error("Sorry, file '%s' doesn't have enough headers in common with current files" % path) 534 else: 535 self.headers.append(headers) 536 self.common_headers = common_headers 537 self.CSVFiles.append(path) 538 self.txtCSVFiles.Append(path) 457 539 else: 458 self.headers.append(headers) 459 self.common_headers = common_headers 460 self.CSVFiles.append(path) 461 self.txtCSVFiles.Append(path) 462 else: 463 self.error("Sorry, file '%s' doesn't appear to be a CSV file" % path) 464 self.updateHdrChoices() 540 self.error("Sorry, file '%s' doesn't appear to be a CSV file" % path) 541 self.updateHdrChoices() 465 542 466 543 def DelCSVFile(self, event): 467 """Delete a CSV file from the listbox""" 468 469 sel = self.txtCSVFiles.GetSelections() 470 if sel: 471 sel = sel[0] 472 self.txtCSVFiles.Delete(sel) 473 del self.CSVFiles[sel] 474 del self.headers[sel] 475 self.common_headers = self.GenCommonHeaders(self.headers) 476 self.updateHdrChoices() 477 544 """Delete all CSV files from the listbox""" 545 546 self.txtCSVFiles.Clear() 547 self.CSVFiles = [] 548 self.common_headers = None 549 self.updateHdrChoices() 550 478 551 def ChangeLegend(self, event): 479 552 if self.chkLegend.GetValue(): … … 494 567 self.cfg['GraphLegend'] = self.chkLegend.GetValue() 495 568 self.cfg['LegendPath'] = self.chkLegendPath.GetValue() 569 self.cfg['ShowGrid'] = self.chkShowGrid.GetValue() 570 self.cfg['FontSize'] = self.cbLabFontSize.GetValue() 571 self.cfg['PlotWidth'] = self.cbPlotWidth.GetValue() 572 self.cfg['XLabel'] = self.txtXLabel.GetValue() 573 self.cfg['YLabel'] = self.txtYLabel.GetValue() 496 574 497 575 self.cfg.save() … … 529 607 if self.YColHdrSelection >= 0: 530 608 self.cbYColHdr.SetSelection(self.YColHdrSelection) 531 title = self.cfg['GraphTitle']532 if title is None:533 title = ''534 self. txtTitle.SetValue(title)535 if self.cfg['GraphLegend']:536 self.chkLegend.SetValue(True)537 if self.cfg['LegendPath']:538 self.chkLegendPath.SetValue(True)609 self.txtTitle.SetValue(self.cfg.get('GraphTitle', '')) 610 self.chkLegend.SetValue(self.cfg.get('GraphLegend', False)) 611 self.chkLegendPath.SetValue(self.cfg.get('LegendPath', False)) 612 self.cbLabFontSize.SetValue(self.cfg.get('FontSize', '14')) 613 self.cbPlotWidth.SetValue(self.cfg.get('PlotWidth', '1')) 614 self.chkShowGrid.SetValue(self.cfg.get('ShowGrid', True)) 615 self.txtXLabel.SetValue(self.cfg.get('XLabel', '')) 616 self.txtYLabel.SetValue(self.cfg.get('YLabel', '')) 539 617 self.ChangeLegend(None) 540 618 … … 578 656 index = self.common_headers.index(selected_x) 579 657 self.cbXColHdr.SetSelection(index) 658 # self.txtXLabel.SetValue(selected_x.title()) 580 659 if selected_y in self.common_headers: 581 660 index = self.common_headers.index(selected_y) 582 661 self.cbYColHdr.SetSelection(index) 662 # self.txtYLabel.SetValue(selected_y.title()) 663 self.txtXLabel.Enable() 664 self.txtYLabel.Enable() 583 665 else: 584 666 self.cbXColHdr.Disable() 585 667 self.cbYColHdr.Disable() 668 self.txtXLabel.Disable() 669 self.txtYLabel.Disable() 586 670 self.btnPlot.Disable() 587 671 … … 620 704 selected_y = self.cbYColHdr.GetStringSelection() 621 705 706 x_label = self.txtXLabel.GetValue() 707 y_label = self.txtYLabel.GetValue() 708 622 709 if selected_x and selected_y: 710 grid_on = self.chkShowGrid.GetValue() 711 fontsize = self.cbLabFontSize.GetValue() 712 plot_width = self.cbPlotWidth.GetValue() 623 713 plot_files(self.CSVFiles, selected_x, selected_y, 714 x_label=x_label, y_label=y_label, 624 715 title=self.txtTitle.GetValue(), 625 716 legend=self.chkLegend.GetValue(), 626 legend_path=self.chkLegendPath.GetValue()) 717 legend_path=self.chkLegendPath.GetValue(), 718 grid=grid_on, fontsize=fontsize, 719 plot_width = plot_width) 720 627 721 # hide problem with wxPython and matplotlib - close app! 628 722 self.Close(True) 723 else: 724 self.error('Sorry, you must select X- and Y-column data values') 629 725 630 726 def error(self, msg):
Note: See TracChangeset
for help on using the changeset viewer.