Skip to content

Commit

Permalink
Modules: grapher layout and file export fixed for Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
stephendade committed Aug 1, 2024
1 parent e1aaf6a commit 0f6e25a
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions MAVProxy/modules/lib/grapher.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
import sys, struct, time, os, datetime, platform
import math, re
import matplotlib
if platform.system() != "Darwin" and os.getenv("MPLBACKEND") is None:
if platform.system() == "Windows":
# wxAgg doesn't properly show the graph values in the lower right on Windows
matplotlib.use('TkAgg')
elif platform.system() != "Darwin" and os.getenv("MPLBACKEND") is None:
# on MacOS we can't set WxAgg here as it conflicts with the MacOS version
matplotlib.use('WXAgg')
from math import *
from pymavlink.mavextra import *
import pylab
import matplotlib.pyplot as plt
from pymavlink import mavutil
import threading
import numpy as np
Expand Down Expand Up @@ -266,8 +269,8 @@ def button_click(self, event):
def plotit(self, x, y, fields, colors=[], title=None, interactive=True):
'''plot a set of graphs using date for x axis'''
if interactive:
pylab.ion()
self.fig = pylab.figure(num=1, figsize=(12,6))
plt.ion()
self.fig = plt.figure(num=1, figsize=(12,6))
self.ax1 = self.fig.gca()
self.ax2 = None
for i in range(0, len(fields)):
Expand All @@ -287,6 +290,7 @@ def plotit(self, x, y, fields, colors=[], title=None, interactive=True):
self.fig.canvas.mpl_connect('draw_event', self.draw_event)
self.fig.canvas.mpl_connect('close_event', self.close_event)
self.fig.canvas.mpl_connect('button_press_event', self.button_click)
self.fig.canvas.get_default_filename = lambda: ''.join(x if x.isalnum() else '_' for x in self.title) + '.png'
empty = True
ax1_labels = []
ax2_labels = []
Expand Down Expand Up @@ -379,7 +383,7 @@ def plotit(self, x, y, fields, colors=[], title=None, interactive=True):
empty = False

if self.grid:
pylab.grid()
plt.grid()

if self.show_flightmode != 0:
alpha = 0.3
Expand All @@ -401,7 +405,7 @@ def plotit(self, x, y, fields, colors=[], title=None, interactive=True):
return

if title is not None:
pylab.title(title)
plt.title(title)
else:
title = fields[0]
if self.fig.canvas.manager is not None:
Expand All @@ -417,10 +421,10 @@ def plotit(self, x, y, fields, colors=[], title=None, interactive=True):
label=mode, alpha=alpha*1.5))
labels = [patch.get_label() for patch in mode_patches]
if ax1_labels != [] and self.show_flightmode != 2:
patches_legend = matplotlib.pyplot.legend(mode_patches, labels, loc=self.legend_flightmode)
patches_legend = plt.legend(mode_patches, labels, loc=self.legend_flightmode)
self.fig.gca().add_artist(patches_legend)
else:
pylab.legend(mode_patches, labels)
plt.legend(mode_patches, labels)

if ax1_labels != []:
self.ax1.legend(ax1_labels,loc=self.legend)
Expand Down Expand Up @@ -674,16 +678,16 @@ def show(self, lenmavlist, block=True, xlim_pipe=None, output=None):
self.xlim_t.start()

if output is None:
pylab.draw()
pylab.show(block=block)
plt.draw()
plt.show(block=block)
elif output.endswith(".html"):
import mpld3
html = mpld3.fig_to_html(self.fig)
f_out = open(output, 'w')
f_out.write(html)
f_out.close()
else:
pylab.savefig(output, bbox_inches='tight', dpi=200)
plt.savefig(output, bbox_inches='tight', dpi=200)

if __name__ == "__main__":
from argparse import ArgumentParser
Expand Down

0 comments on commit 0f6e25a

Please sign in to comment.