Factorisation de la création des plots

This commit is contained in:
Philippe Roy 2023-02-01 18:18:37 +01:00
parent dcf642df38
commit f2e450654e
3 changed files with 38 additions and 42 deletions

View File

@ -130,7 +130,7 @@ def plot_draw(plt, var, xdata, ydata, plot_config):
linestyle=plot_config_get(plot_config, var, 'linestyle'), marker=plot_config_get(plot_config, var, 'marker'))
# Création des graphiques statiques
def plots_static(canvas, fields, xdata, ydata, plot_config, title):
def plots_static(plt, fields, xdata, ydata, plot_config, title):
# Configuration des plots activée
plot_config_enable=plot_config_get_enable()
@ -172,45 +172,40 @@ def plots_static(canvas, fields, xdata, ydata, plot_config, title):
# Création du plot unique
if plot_nb(plot_config) ==1:
if plot_config_enable: # Configuration des plots activée
plot_draw(canvas.plt, var, xdata[i], ydata[i], plot_config)
plot_draw(plt, var, xdata[i], ydata[i], plot_config)
else:
canvas.plt.plot(xdata[i], ydata[i], '.-', label=var) # Configuration matplotlib par défaut
plt.plot(xdata[i], ydata[i], '.-', label=var) # Configuration matplotlib par défaut
# Légende ou titre d'axe y
if plt_grp[plt_current][1]==0:
canvas.plt.set_ylabel(var)
plt.set_ylabel(var)
else:
canvas.plt.legend()
plt.legend()
# Création des subplots
if plot_nb(plot_config) >1:
if plot_config_enable: # Configuration des plots activée
plot_draw(canvas.plt[plt_current], var, xdata[i], ydata[i], plot_config)
plot_draw(plt[plt_current], var, xdata[i], ydata[i], plot_config)
else:
canvas.plt[plt_current].plot(xdata[i], ydata[i], '.-', label=var) # Configuration matplotlib par défaut
plt[plt_current].plot(xdata[i], ydata[i], '.-', label=var) # Configuration matplotlib par défaut
# Légende ou titre d'axe y
if plt_grp[plt_current][1]==0:
canvas.plt[plt_current].set_ylabel(var)
plt[plt_current].set_ylabel(var)
else:
canvas.plt[plt_current].legend()
plt[plt_current].legend()
# Décoration
if plot_nb(plot_config) ==1: # 1 plot
canvas.plt.set_xlabel("Temps (s)")
canvas.plt.set_title(title)
canvas.plt.axhline(linewidth=1, color='k')
canvas.plt.grid(True, linestyle='--')
plt.set_xlabel("Temps (s)")
plt.set_title(title)
plt.axhline(linewidth=1, color='k')
plt.grid(True, linestyle='--')
# canvas.plt.legend()
else: # Plusieurs plots
canvas.plt[plt_i-1].set_xlabel("Temps (s)")
canvas.plt[0].set_title(title)
plt[plt_i-1].set_xlabel("Temps (s)")
plt[0].set_title(title)
for i in range (plt_i):
canvas.plt[i].axhline(linewidth=1, color='k')
canvas.plt[i].grid(True, linestyle='--')
# canvas.plt[i].legend()
# Redraw
canvas.draw()
plt[i].axhline(linewidth=1, color='k')
plt[i].grid(True, linestyle='--')
# plt[i].legend()

View File

@ -178,7 +178,7 @@ class MainWindow(QtWidgets.QMainWindow):
# Création des graphive à partir du fichier CSV
fields, xdata, ydata = csv_read(sys.argv[1])
plots_static(self.canvas, fields, xdata, ydata, plot_config, sys.argv[1])
plots_static(self.canvas.plt, fields, xdata, ydata, plot_config, sys.argv[1])
self.show()
###############################################################################

View File

@ -1,4 +1,6 @@
import sys
import csv # Creating/parsing CSV file
import xml.etree.ElementTree as ET # Creating/parsing XML file
from twin_plot import plot_config_get_dict, plot_config_get, plot_config_get_enable, plot_nb, csv_read, plots_static # Gestion communes des graphiques (Qt, wx)
import wx # GUI wxPython
@ -24,6 +26,8 @@ plot_config={}
def plot_draw(plt):
twin_config = ET.parse('twin_config.xml').getroot()
# Lecture fichier CSV
fields = []
rows = []
@ -82,10 +86,10 @@ def plot_draw(plt):
plt_i +=1
# Création du plot unique
if plot_nb() ==1:
if plot_nb(plot_config) ==1:
if twin_config[1][0].text == "True": # Configuration des plots activée
plt.plot(xdata[i], ydata[i], label=var, color=plot_config_get(var, 'color'), linewidth=plot_config_get(var, 'linewidth'),
linestyle=plot_config_get(var, 'linestyle'), marker=plot_config_get(var, 'marker'))
plt.plot(xdata[i], ydata[i], label=var, color=plot_config_get(plot_config, var, 'color'), linewidth=plot_config_get(plot_config, var, 'linewidth'),
linestyle=plot_config_get(plot_config, var, 'linestyle'), marker=plot_config_get(plot_config, var, 'marker'))
else:
plt.plot(xdata[i], ydata[i], '.-', label=var) # Configuration matplotlib par défaut
@ -96,10 +100,10 @@ def plot_draw(plt):
plt.legend()
# Création des subplots
if plot_nb() >1:
if plot_nb(plot_config) >1:
if twin_config[1][0].text == "True": # Configuration des plots activée
plt[plt_current].plot(xdata[i], ydata[i], label=var, color=plot_config_get(var, 'color'), linewidth=plot_config_get(var, 'linewidth'),
linestyle=plot_config_get(var, 'linestyle'), marker=plot_config_get(var, 'marker'))
plt[plt_current].plot(xdata[i], ydata[i], label=var, color=plot_config_get(plot_config, var, 'color'), linewidth=plot_config_get(plot_config, var, 'linewidth'),
linestyle=plot_config_get(plot_config, var, 'linestyle'), marker=plot_config_get(plot_config, var, 'marker'))
else:
plt[plt_current].plot(xdata[i], ydata[i], '.-', label=var) # Configuration matplotlib par défaut
@ -110,7 +114,7 @@ def plot_draw(plt):
plt[plt_current].legend()
# Décoration
if plot_nb() ==1: # 1 plot
if plot_nb(plot_config) ==1: # 1 plot
plt.set_xlabel("Temps (s)")
# self.canvas.plt[0].set_ylabel("Valeurs")
plt.set_title(sys.argv[1])
@ -137,7 +141,7 @@ class CanvasFrame(wx.Frame):
# Création des zones graphique (Plots)
self.figure = Figure()
if plot_nb() ==1: # plot_nb() : nombre de graphiques
if plot_nb(plot_config) ==1: # plot_nb() : nombre de graphiques
plt = self.figure.subplots() # plot_nb() : nombre de graphiques
else:
plt = self.figure.subplots(plot_nb(plot_config), 1, sharex=True) # plot_nb() : nombre de graphiques
@ -145,11 +149,8 @@ class CanvasFrame(wx.Frame):
# Création des graphive à partir du fichier CSV
fields, xdata, ydata = csv_read(sys.argv[1])
plots_static(self.canvas, fields, xdata, ydata, plot_config, sys.argv[1])
self.show()
# # Dessin
# plot_draw(plt)
plots_static(plt, fields, xdata, ydata, plot_config, sys.argv[1])
self.canvas.draw()
# Implantation de la fenêtre (canvas)
self.sizer = wx.BoxSizer(wx.VERTICAL)