Skip to content
Snippets Groups Projects
Panel_Plot.py 54.7 KiB
Newer Older
  • Learn to ignore specific revisions
  •               - Lwidth     : List of width of the bars
                  - Llinecolor  : List of line color of the bar edges
                  - Llinewidth  : List of lines thickness of the bar edges
                  - Lfacconv   : List of factors for unit conversion of each variables
                  - Lxlim  : List of x (min, max) value plotted
                  - Lylim  : List of y (min, max) value plotted
                  - Ltime  : List of time (validity)
                  - ax         : List of fig.axes for ploting multiple different types of plots in a subplot panel
                  - Lid_overlap: List of number index of plot to overlap current variables
                  - LaxisColor : List of colors for multiple x-axis overlap
                  - LlocLegend : List of localisation of the legend : 'best',  'upper left', 'upper right', 'lower left', 'lower right',
                                 'upper center', 'lower center', 'center left', 'center right', 'center'
            """
            self.ax = ax
            firstCall = (len(self.ax) == 0)
            #  Defaults value convert to x number of variables list
            if not Lfacconv:
                Lfacconv = [1.0] * len(Lvar)
            if not LaxisColor:
                LaxisColor = ['black'] * len(Lvar)
            if not Lylab:
                Lylab = [''] * len(Lvar)
            if not Lxlab:
                Lxlab = [''] * len(Lvar)
            if not Lcolor:
                Lcolor = ['black'] * len(Lvar)
            if not Lwidth:
                Lwidth = [1] * len(Lvar)
            if not Llinecolor:
                Llinecolor = ['black'] * len(Lvar)
            if not Llinewidth:
                Llinewidth = [0] * len(Lvar)
            if not LlocLegend:
                LlocLegend = ['upper right'] * len(Lvar)
    
            #  On all variables to plot
            for i, var in enumerate(Lvar):
                if firstCall:  # 1st call
                    iax = i
                    self.ax.append(self.fig.add_subplot(self.nb_l, self.nb_c, i + 1, label='graph axe x down'))
                    self.nb_graph += 1
                elif id_overlap:  # overlapping plot with a different x-axis
                    self.ax.append(self.fig.add_subplot(self.nb_l, self.nb_c, self.nb_graph, label='graph axe x top', frame_on=False))
                    iax = len(self.ax) - 1
                else:  # existing ax with no overlapping (graph appended to existing panel)
                    self.ax.append(self.fig.add_subplot(self.nb_l, self.nb_c, self.nb_graph + 1))
                    self.nb_graph += 1
                    iax = len(self.ax) - 1  # The ax index of the new coming plot is the length of the existant ax -1 for indices matter
    
                #  Print time validity
                if Ltime:
                    self.showTimeText(self.ax, iax, str(Ltime[i]))
    
                #  Bins by labels
                labels = np.array([str(L) for L in Lbins[i]])
    
                #  Plot
                cf = self.ax[iax].bar(labels, var * Lfacconv[i], width=Lwidth[i], color=Lcolor[i], linewidth=Llinewidth[i], edgecolor=Llinecolor[i])
    
                #  Legend
                # TODO : Handling legend with overlap two axis lines in the same box. For now, placement is by hand
                if not id_overlap:
                    self.ax[iax].legend(loc=LlocLegend[i], bbox_to_anchor=(1, 0.95))
                else:
                    self.ax[iax].legend(loc=LlocLegend[i], bbox_to_anchor=(1, 0.90))
    
                #  Title
                if Ltitle:
                    self.set_Title(self.ax, iax, Ltitle[i], id_overlap, Lxlab[i], Lylab[i])
    
                #  X/Y Axis label
                if id_overlap:
                    self.ax[iax].xaxis.tick_top()
                    self.ax[iax].xaxis.set_label_position('top')
                    self.ax[iax].set_xlabel(Lxlab[i])
                    self.ax[iax].set_ylabel(Lylab[i])
                else:
                    self.set_XYaxislab(self.ax, iax, Lxlab[i], Lylab[i])
    
                self.ax[iax].tick_params(axis='x', colors=LaxisColor[i])
    
                # X/Y Axis limits value
                if Lxlim:
                    try:
                        self.set_xlim(self.ax, iax, Lxlim[i])
                    except BaseException:
                        pass
                if Lylim:
                    try:
                        self.set_ylim(self.ax, iax, Lylim[i])
                    except BaseException:
                        pass
    
            return self.fig
    # def initialize_default_val(Lvar, Dparam):
    
    #    #TODO : initialize default value for all parameter of all type of graphs
    #    #Returns All the parameters given in Dparam where :
    #    "- If no value is found (empty list []) : return the default value (if exist) * number of graph
    #    #- If ONE value only is found : return the value copied x times the number of graph
    #    #- If the list is complete, nothing is done
    #    #CURRENT PROBLEM
    
    #    #The returned value do not change the true referenced variable given as argument
    
    #    #  Number of graphs
    #    l = len(Lvar)
    
    #    #  Add an extra percentage of the top max value for forcing the colorbar show the true user maximum value (correct a bug)
    #    #if Dparam['Lstep'] and Dparam['Lmaxval']: Dparam['Lmaxval'] = list(map(lambda x, y: x + 1E-6*y, Dparam['Lmaxval'], Dparam['Lstep']) ) #The extra value is 1E-6 times the step ticks of the colorbar
    #    print(Dparam.items())
    
    #    #  Read on which parameters initialize the default values
    #    for args_t in list(Dparam.items()):  #  Test on all arguments present, if they are empty list, default values apply for each graph
    #      args = list(args_t)
    #      print(args)
    #      if args[0] == 'Lfacconv' and not args[1]: args[1] = [1.0]*l
    #      elif args[0] == 'Lcolormap' and not args[1]: args[1] = ['gist_rainbow_r']*l
    #      elif args[0] == 'LcolorLine' and not args[1]: args[1] = ['black']*l
    #      elif args[0] == 'Lpltype' and not args[1]: args[1]= ['cf']*l
    #      elif args[0] == 'LaddWhite_cm' and not args[1]: args[1] = ['False']*l
    #      elif args[0] == 'Lstep' and not args[1]: args[1] = [None]*l #  default value filled later
    #      elif args[0] == 'Lstepticks' and not args[1]: args[1] = [None]*l
    #      Dparam[args[0]] = args[1]
    #      print(args)
    
    #    #  Check if there is no value for a parameter
    
    # for args_t in list(Dparam.items()):
    
    # for i in range(l):
    # if 'Lstepticks' in arguments.args and 'Lmaxval' in arguments.args and 'Lminval' in arguments.args:
    # Lstep[i] = (Lmaxval[i] - Lminval[i])/20  #  Default value of number of steps is 20
    # Lstepticks[i] = Lstep[i]  #  Default value is stepticks are the same as steps values
    #