Aug-05-2018, 06:03 PM
I have done a class hierarchy for product several plot stile , in which change almost any parameter, just inherit and define a set of parameter for change the default plot ..
but I have some problem with the derived class , basically from the abstract base class
derive a complete and usable class which is callable ! (in this way i can call directly subplot, or plot referring to the derived class)
The problem is that the usable base class that define the method __call__ works fine and accept and use in the right way the parameter passed by a generic script that instantiate it for create a plot, the derivatives classes don't.
here I'm report minimal working code
the base abstract class is:
then the derivate class for which the parameter passed as dictionary dosen't affect
the parameters (as expected ... here is the problem)
even thought the code works the result is not was I expected indeed the scheme 'vega' is not used in the plot ! while if you call in the same way the base class (the commented call) the code use the scheme 'vega' !! whyy ??? what I'm wronging ?
but I have some problem with the derived class , basically from the abstract base class
derive a complete and usable class which is callable ! (in this way i can call directly subplot, or plot referring to the derived class)
The problem is that the usable base class that define the method __call__ works fine and accept and use in the right way the parameter passed by a generic script that instantiate it for create a plot, the derivatives classes don't.
here I'm report minimal working code
the base abstract class is:
class BasePlot(metaclass=ABCMeta): def __init__(self, title : str = ' ' , filename : str = ' '): self.title = title self.filename = filename def schemes(self, style:str = 'nb'): if style == 'nb': return ['#8DA0CB', '#E58AC3', '#A6D853', '#FFD930', '#B2B2B2', '#5FC3A4', '#FC8D62', '#66C2A5'] elif style == 'vega': return ['#1F77B4', '#FF7F0E', '#2CA02C', '#D62728', '#9467BD', '#8C564B', '#E377C2', '#7F7F7F', '#BCBD22', '#17BECF']from this base class derive the "Default plot" before called "the usable base class"
class DefaultPlot(BasePlot) : def __init__(self, figsize,*args,**kwargs): self.var = [*args] self.params = kwargs def cycle(self,n : str): if n == '0': return plt.cycler("color", self.schemes(self.parameters['scheme']) ) #colors) elif n=='1': return plt.cycler("color", self.schemes(self.parameters['scheme']) ) + plt.cycler("linestyle", self.linestyles(self.parameters['linestyle'])) def setparams(self, kwargs): self.parameters = kwargs if 'scheme' not in self.parameters.keys(): self.parameters['scheme'] = self.schemes('nb') myparams = { 'axes.prop_cycle': 0, } plt.rcParams.update(myparams) def __call__ (self,nrows,ncols,*args,**kwargs): self.setparams(kwargs) self.fig, self.axs = plt.subplots(nrows,ncols,figsize=(9.5,4.5))
then the derivate class for which the parameter passed as dictionary dosen't affect
the parameters (as expected ... here is the problem)
#-*- coding : utf-8 -*- class Standard(DefaultPlot): def __init__(self , figsize , *args , **kwargs): self.args = [*args] self.params = kwargs super().__init__(figsize,*self.args, **self.params ) def setparams(self, kwargs): self.parameters = kwargs if 'scheme' in self.parameters.keys(): self.parameters['scheme'] = 'nb' if 'cycle' not in self.parameters.keys(): self.parameters['cycle'] = self.cycle('0') super().setparams(self.parameters)and finally the script in which the class is called :
import defaultplot import qualityplot def main(): x = np.linspace(0,2*np.pi,100) y1 = np.sin(x) y2 = np.sin(2*x) y3 = np.sin(3*x) fig,axs = qualityplot.Standard(figsize=(9.5,4.5))(1,1,**{'scheme':'vega'}) #fig,axs = defaultplot.DefaultPlot(figsize=(9.5,4.5))(1,1,**{'scheme':'vega'}) axs.plot(x,y1,x,y2,x,y3) plt.show() if __name__ == '__main__': main()In this script above the Standard class is instantiate and a dictionary containing parameters is passed , in particular the parameter for the scheme colors !
even thought the code works the result is not was I expected indeed the scheme 'vega' is not used in the plot ! while if you call in the same way the base class (the commented call) the code use the scheme 'vega' !! whyy ??? what I'm wronging ?