diff --git a/kaplot/__init__.py b/kaplot/__init__.py index c3bfe19..f2555e8 100644 --- a/kaplot/__init__.py +++ b/kaplot/__init__.py @@ -5,6 +5,7 @@ TODO ==== + - find a way to implement '\!' (negative space) into latex strings to remove that annoying whitespace after a super/sub script - need to add loadObj() - fix latex output to use the same font """ @@ -26,7 +27,7 @@ from numpy import linspace __author__ = 'Kamil' -__version__ = '1.0.0~beta2' +__version__ = '1.0.0~beta3' __name__ = 'kaplot' @decorator @@ -82,6 +83,7 @@ def __init__(self,settings=None): self._LAYER_SETTINGS.append(deepcopy(self.LAYER_SETTINGS)) # Add settings self.load_settings(settings) + plt.clf() return def load_settings(self,settings): @@ -206,7 +208,6 @@ def set_legend(self,lbool,**kwargs): framealpha - alpha level for the frame ncol - number of legend columns title - legend title - fontsize - font size borderpad - padding inside the legend labelspacing - spacing between labels handletextpad - spacing between legend handle and text @@ -214,7 +215,6 @@ def set_legend(self,lbool,**kwargs): ** font prop kwargs ** family - font family , 'sans-serif' 'serif' 'monospace' 'fantasy' - style - 'normal' or 'oblique' weight - 'normal' 'regular' 'semibold' 'bold' 'black' size - font size , #points 'xx-small' 'medium' 'xx-large' """ @@ -237,7 +237,7 @@ def set_title(self,title,**kwargs): name - layer name family - font family , 'sans-serif' 'serif' 'monospace' 'fantasy' - style - 'normal' or 'oblique' + weight - 'normal' 'regular' 'semibold' 'bold' 'black' size - font size , #points 'xx-small' 'medium' 'xx-large' color - font color alpha - alpha level @@ -321,7 +321,6 @@ def set_xlabel(self,lab='',unit=None,**kwargs): name - layer name family - font family , 'sans-serif' 'serif' 'monospace' 'fantasy' - style - 'normal' or 'oblique' weight - 'normal' 'regular' 'semibold' 'bold' 'black' size - font size , #points 'xx-small' 'medium' 'xx-large' color - font color @@ -352,7 +351,6 @@ def set_ylabel(self,lab='',unit=None,**kwargs): name - layer name family - font family , 'sans-serif' 'serif' 'monospace' 'fantasy' - style - 'normal' or 'oblique' weight - 'normal' 'regular' 'semibold' 'bold' 'black' size - font size , #points 'xx-small' 'medium' 'xx-large' color - font color @@ -371,7 +369,7 @@ def set_ylabel(self,lab='',unit=None,**kwargs): return @check_name - def set_xticks(self,start,stop,incr,log=False,**kwargs): + def set_xticks(self,start=None,stop=None,incr=None,log=False,**kwargs): """ sets the values of the ticks , can be logarithmic or custom if `myList` is specified it will overwrite all other values @@ -385,9 +383,9 @@ def set_xticks(self,start,stop,incr,log=False,**kwargs): ** kwargs ** name - layer name mylist - custom list + mylabels - custom labels family - font family , 'sans-serif' 'serif' 'monospace' 'fantasy' - style - 'normal' or 'oblique' size - font size , #points 'xx-small' 'medium' 'xx-large' weight - 'normal' 'regular' 'semibold' 'bold' 'black' color - font color @@ -396,17 +394,31 @@ def set_xticks(self,start,stop,incr,log=False,**kwargs): ha - horizontal alignment , 'center' , 'right' , 'left' rotation - rotate text by some degree """ - fdict = update_default_kwargs(self._FONT_XTICK,kwargs) - k = self._LAYER_OBJECTS[kwargs['ind']] - if 'mylist' in kwargs: - tick_list = kwargs['mylist'] - else: - tick_list = srange(start,stop,incr,log) - k.set_xticks(tick_list,**fdict) + fdict = update_default_kwargs(self._FONT_XTICK,kwargs) + k = self._LAYER_OBJECTS[kwargs['ind']] + tick_list = [] + tick_labels = [] + if start is not None: + # start, stop, incr are specified + tick_list = srange(start,stop,incr,log) + tick_labels = srange(start,stop,incr,log) + # custom labels + if 'mylist' in kwargs and 'mylabels' in kwargs: + for i,val in enumerate(kwargs['mylist']): + try: + li = tick_labels.index(val) + tick_labels[li] = kwargs['mylabels'][i] + except ValueError: + print 'The Value ',val,' is not in ',tick_labels,'. Ignoring.' + # custom ticks but no labels + elif 'mylist' in kwargs and 'mylabels' not in kwargs: + tick_list = kwargs['mylist'] + tick_labels = kwargs['mylist'] + k.set_xticks(tick_list,tick_labels,**fdict) return @check_name - def set_yticks(self,start,stop,incr,log=False,**kwargs): + def set_yticks(self,start=None,stop=None,incr=None,log=False,**kwargs): """ sets the values of the ticks , can be logarithmic or custom if `myList` is specified it will overwrite all other values @@ -420,9 +432,9 @@ def set_yticks(self,start,stop,incr,log=False,**kwargs): ** kwargs ** name - layer name mylist - custom list + mylabels - custom labels family - font family , 'sans-serif' 'serif' 'monospace' 'fantasy' - style - 'normal' or 'oblique' size - font size , #points 'xx-small' 'medium' 'xx-large' weight - 'normal' 'regular' 'semibold' 'bold' 'black' color - font color @@ -431,13 +443,27 @@ def set_yticks(self,start,stop,incr,log=False,**kwargs): ha - horizontal alignment , 'center' , 'right' , 'left' rotation - rotate text by some degree """ - fdict = update_default_kwargs(self._FONT_YTICK,kwargs) - k = self._LAYER_OBJECTS[kwargs['ind']] - if 'mylist' in kwargs: - tick_list = kwargs['mylist'] - else: - tick_list = srange(start,stop,incr,log) - k.set_yticks(tick_list,**fdict) + fdict = update_default_kwargs(self._FONT_YTICK,kwargs) + k = self._LAYER_OBJECTS[kwargs['ind']] + tick_list = [] + tick_labels = [] + if start is not None: + # start, stop, incr are specified + tick_list = srange(start,stop,incr,log) + tick_labels = srange(start,stop,incr,log) + # custom labels + if 'mylist' in kwargs and 'mylabels' in kwargs: + for i,val in enumerate(kwargs['mylist']): + try: + li = tick_labels.index(val) + tick_labels[li] = kwargs['mylabels'][i] + except ValueError: + print 'The Value ',val,' is not in ',tick_labels,'. Ignoring.' + # custom ticks but no labels + elif 'mylist' in kwargs and 'mylabels' not in kwargs: + tick_list = kwargs['mylist'] + tick_labels = kwargs['mylist'] + k.set_yticks(tick_list,tick_labels,**fdict) return @check_name @@ -499,13 +525,13 @@ def set_tick_params(self,axis='both',**kwargs): labelsize - tick label font size labelcolor - tick label font color - * valid in both / x-axis * + * valid in x-axis * (both is experimental) labeltop - True/False labelbottom - True/False top - True/False , draw ticks bottom - True/False , draw ticks - * valid in both / y-axis * + * valid in y-axis * (both is experimental) labelleft - True/False labelright - True/False left - True/False , draw ticks @@ -652,7 +678,7 @@ def add_text(self,txt,x,y,**kwargs): ** kwargs ** name - layer name family - font family , 'sans-serif' 'serif' 'monospace' 'fantasy' - style - 'normal' or 'oblique' + weight - 'normal' 'regular' 'semibold' 'bold' 'black' size - font size , #points 'xx-small' 'medium' 'xx-large' color - font color alpha - alpha level @@ -693,7 +719,7 @@ def add_plotdata(self,x,y,**kwargs): alpha - alpha level ** line plot kwargs ** - m - marker + marker - marker mec - marker edge color ms - marker size markevery - marker every data points @@ -851,6 +877,68 @@ def color_marker_fill_index(cnt,clist,mlist,flist): # GRID if k.SETTINGS['grid_bool']: mpobj.grid(**k.SETTINGS['grid_prop']) + # ADD PLOTDATA + if len(k.DATA_LIST) is not 0: + # generate color,marker,fill list for the plot + inc_cnt = 0 + for pd in k.DATA_LIST: + if pd['increment']: + inc_cnt += 1 + cnt = 0 + for pd in k.DATA_LIST: + # line plots + if k.SETTINGS['plot_type'] is 'line': + if k.SETTINGS['uniq_cols']: + cols = unique_colors(inc_cnt+1,k.SETTINGS['color_map']) + col , mar , fill = cols[cnt] , None , None + else: + cind , mind , find = color_marker_fill_index(cnt,self._COLOR_LIST,self._MARKER_LIST,self._MARKER_FILL_LIST) + col , mar , fill = self._COLOR_LIST[cind] , self._MARKER_LIST[mind] , self._MARKER_FILL_LIST[find] + if pd['increment']: + cnt += 1 + if 'color' not in pd: + pd['color'] = col + if 'marker' not in pd: + pd['marker'] = mar + if 'mfc' not in pd: + pd['mfc'] = fill + # spline portion + sp_key = ['color','lw','ls'] + if pd['spline']: + x_spline = linspace(pd['x'][0],pd['x'][-1],pd['sp_points']) + y_spline = UnivariateSpline(pd['x'],pd['y'],k=pd['sp_order'],s=pd['sp_smooth'])(x_spline) + sp_dict = {} + for sp in sp_key: + if sp in pd: + sp_dict[sp] = pd[sp] + pd['lw'] = 0 + pd['ls'] = '' + mpobj.errorbar(x=x_spline,y=y_spline,**sp_dict) + pd.pop('spline') + pd.pop('sp_smooth') + pd.pop('sp_order') + pd.pop('sp_points') + pd.pop('increment') + mpobj.errorbar(**pd) + # bar plots + elif k.SETTINGS['plot_type'] is 'bar': + if k.SETTINGS['uniq_cols']: + cols = unique_colors(inc_cnt+1,k.SETTINGS['color_map']) + col , hat , fill = cols[cnt] , None , None + else: + cind , hind , find = color_marker_fill_index(cnt,self._COLOR_LIST,self._HATCH_LIST,self._HATCH_FILL_LIST) + col , hat , fill = self._COLOR_LIST[cind] , self._HATCH_LIST[hind] , self._HATCH_FILL_LIST[find] + if pd['increment']: + cnt += 1 + # do not overwrite user specified values + if 'color' not in pd: + pd['color'] = col + if 'hatch' not in pd: + pd['hatch'] = hat + if 'fill' not in pd: + pd['fill'] = fill + pd.pop('increment') + mpobj.bar(**pd) # AXES TYPE AND BASE SETTING if k.SETTINGS['axes_type'] in ['log-log','semilog-x','semilog-y']: if k.SETTINGS['axes_type'] is 'log-log': @@ -870,10 +958,16 @@ def color_marker_fill_index(cnt,clist,mlist,flist): mpobj.set_ylabel(k.SETTINGS['ylabel'],**k.SETTINGS['ylab_prop']) if k.SETTINGS['xticks'] is not None: mpobj.set_xticks(k.SETTINGS['xticks']) - mpobj.set_xticklabels(k.SETTINGS['xticks'],**k.SETTINGS['xtick_prop']) + mpobj.set_xticklabels(k.SETTINGS['xtick_labels'],**k.SETTINGS['xtick_prop']) + elif k.SETTINGS['xtick_prop'] is not None: + # change settings even if no ticks are specified + mpobj.set_xticklabels(mpobj.get_xticklabels(),**k.SETTINGS['xtick_prop']) if k.SETTINGS['yticks'] is not None: mpobj.set_yticks(k.SETTINGS['yticks']) - mpobj.set_yticklabels(k.SETTINGS['yticks'],**k.SETTINGS['ytick_prop']) + mpobj.set_yticklabels(k.SETTINGS['ytick_labels'],**k.SETTINGS['ytick_prop']) + elif k.SETTINGS['ytick_prop'] is not None: + # change settings eben if no ticks are specified + mpobj.set_yticklabels(mpobj.get_yticklabels(),**k.SETTINGS['ytick_prop']) if k.XTICK_FORMAT is not None: mpobj.xaxis.set_major_formatter(matplotlib.ticker.ScalarFormatter()) mpobj.ticklabel_format(axis='x',**k.XTICK_FORMAT) @@ -933,68 +1027,6 @@ def color_marker_fill_index(cnt,clist,mlist,flist): for txt in k.TEXT_LIST: txt['s'] = txt.pop('txt') mpobj.text(**txt) - # ADD PLOTDATA - if len(k.DATA_LIST) is not 0: - # generate color,marker,fill list for the plot - inc_cnt = 0 - for pd in k.DATA_LIST: - if pd['increment']: - inc_cnt += 1 - cnt = 0 - for pd in k.DATA_LIST: - # line plots - if k.SETTINGS['plot_type'] is 'line': - if k.SETTINGS['uniq_cols']: - cols = unique_colors(inc_cnt+1,k.SETTINGS['color_map']) - col , mar , fill = cols[cnt] , None , None - else: - cind , mind , find = color_marker_fill_index(cnt,self._COLOR_LIST,self._MARKER_LIST,self._MARKER_FILL_LIST) - col , mar , fill = self._COLOR_LIST[cind] , self._MARKER_LIST[mind] , self._MARKER_FILL_LIST[find] - if pd['increment']: - cnt += 1 - if 'color' not in pd: - pd['color'] = col - if 'marker' not in pd: - pd['marker'] = mar - if 'mfc' not in pd: - pd['mfc'] = fill - # spline portion - sp_key = ['color','lw','ls'] - if pd['spline']: - x_spline = linspace(pd['x'][0],pd['x'][-1],pd['sp_points']) - y_spline = UnivariateSpline(pd['x'],pd['y'],k=pd['sp_order'],s=pd['sp_smooth'])(x_spline) - sp_dict = {} - for sp in sp_key: - if sp in pd: - sp_dict[sp] = pd[sp] - pd['lw'] = 0 - pd['ls'] = '' - mpobj.errorbar(x=x_spline,y=y_spline,**sp_dict) - pd.pop('spline') - pd.pop('sp_smooth') - pd.pop('sp_order') - pd.pop('sp_points') - pd.pop('increment') - mpobj.errorbar(**pd) - # bar plots - elif k.SETTINGS['plot_type'] is 'bar': - if k.SETTINGS['uniq_cols']: - cols = unique_colors(inc_cnt+1,k.SETTINGS['color_map']) - col , hat , fill = cols[cnt] , None , None - else: - cind , hind , find = color_marker_fill_index(cnt,self._COLOR_LIST,self._HATCH_LIST,self._HATCH_FILL_LIST) - col , hat , fill = self._COLOR_LIST[cind] , self._HATCH_LIST[hind] , self._HATCH_FILL_LIST[find] - if pd['increment']: - cnt += 1 - # do not overwrite user specified values - if 'color' not in pd: - pd['color'] = col - if 'hatch' not in pd: - pd['hatch'] = hat - if 'fill' not in pd: - pd['fill'] = fill - pd.pop('increment') - mpobj.bar(**pd) # ADD RECTANGLE if len(k.RECT_LIST) is not 0: inc_cnt = 0 @@ -1034,7 +1066,9 @@ def color_marker_fill_index(cnt,clist,mlist,flist): if k.SETTINGS['leg_props'] is not None: if k.SETTINGS['leg_props']['bool']: k.SETTINGS['leg_props'].pop('bool') - mpobj.legend(prop=k.SETTINGS['leg_fprop'],**k.SETTINGS['leg_props']) + l = mpobj.legend(prop=k.SETTINGS['leg_fprop'],**k.SETTINGS['leg_props']) + # update the legend title also + plt.setp(l.get_title(),**k.SETTINGS['leg_fprop']) # make copy of the entire object self._LAYER_PLT_OBJECT.append(mpobj) return @@ -1098,34 +1132,36 @@ class kaxes(object): _LOCATION = ['upper left', 'upper right', 'lower left', 'lower right'] def __init__(self): - self.SETTINGS = { 'plot_type' : 'line' , \ - 'uniq_cols' : False , \ - 'color_map' : 'gist_rainbow' , \ - 'location' : None , \ - 'title' : None , \ - 'title_prop': None , \ - 'grid_bool' : False , \ - 'grid_prop' : None , \ - 'axes_type' : 'linear' , \ - 'x_base' : 1.0 , \ - 'y_base' : 1.0 , \ - 'xlabel' : None , \ - 'xlab_prop' : None , \ - 'ylabel' : None , \ - 'ylab_prop' : None , \ - 'xticks' : None , \ - 'xtick_prop': None , \ - 'yticks' : None , \ - 'ytick_prop': None , \ - 'x_limit' : None , \ - 'y_limit' : None , \ - 'leg_props' : None , \ - 'leg_fprop' : None} - - self.FRAMES = { 'top' : True , \ - 'bottom' : True , \ - 'left' : True , \ - 'right' : True} + self.SETTINGS = { 'plot_type' : 'line' , \ + 'uniq_cols' : False , \ + 'color_map' : 'gist_rainbow' , \ + 'location' : None , \ + 'title' : None , \ + 'title_prop' : None , \ + 'grid_bool' : False , \ + 'grid_prop' : None , \ + 'axes_type' : 'linear' , \ + 'x_base' : 1.0 , \ + 'y_base' : 1.0 , \ + 'xlabel' : None , \ + 'xlab_prop' : None , \ + 'ylabel' : None , \ + 'ylab_prop' : None , \ + 'xticks' : None , \ + 'xtick_labels' : None , \ + 'xtick_prop' : None , \ + 'yticks' : None , \ + 'ytick_labels' : None , \ + 'ytick_prop' : None , \ + 'x_limit' : None , \ + 'y_limit' : None , \ + 'leg_props' : None , \ + 'leg_fprop' : None} + + self.FRAMES = { 'top' : True , \ + 'bottom' : True , \ + 'left' : True , \ + 'right' : True} self.XTICK_PARAM = None self.YTICK_PARAM = None self.XTICK_FORMAT = None @@ -1186,14 +1222,18 @@ def set_ylabel(self,lab,**fdict): self.SETTINGS['ylab_prop'] = fdict return - def set_xticks(self,myList,**fdict): - self.SETTINGS['xticks'] = myList + def set_xticks(self,myList,myLabels,**fdict): self.SETTINGS['xtick_prop'] = fdict + if len(myList) != 0: + self.SETTINGS['xticks'] = myList + self.SETTINGS['xtick_labels'] = myLabels return - def set_yticks(self,myList,**fdict): - self.SETTINGS['yticks'] = myList + def set_yticks(self,myList,myLabels,**fdict): self.SETTINGS['ytick_prop'] = fdict + if len(myList) != 0: + self.SETTINGS['yticks'] = myList + self.SETTINGS['ytick_labels'] = myLabels return def set_xlim(self,xmin,xmax): @@ -1298,18 +1338,33 @@ def srange(start,end,incr,log=False): return [] if start == end: return [] + ## type checking + # if everything can be shown as an int then make all values int + if int(start) == float(start): + start = int(start) + else: + start = float(start) + if int(end) == float(end): + end = int(end) + else: + end = float(end) + if int(incr) == float(incr): + incr = int(incr) + else: + incr = float(incr) + ## /type checking if log == False: - cval = float(start) + cval = start retList =[] while cval <= end: retList.append(cval) - cval = float(cval)+float(incr) + cval = cval+incr else: cval = start retList = [] while cval <= end: retList.append(cval) - cval = float(cval)*float(incr) + cval = cval*incr return retList def convert_xy(ax,x,y): diff --git a/kaplot/defaults.py b/kaplot/defaults.py index 96761ad..3d96f81 100644 --- a/kaplot/defaults.py +++ b/kaplot/defaults.py @@ -20,7 +20,7 @@ 'SAVEFIG_SETTINGS' : { 'dpi' : 100 , \ 'transparent' : False , \ - 'width' : 8 , \ + 'width' : 8 , \ 'height' : 6}, '_LOCATION_TIGHT' : { 'upper left' : [0.18,0.595,0.25,0.25] , \ @@ -34,7 +34,6 @@ 'lower left' : [0.22,0.180,0.25,0.25]}, '_FONT_TITLE' : { 'family' : 'Auto' , \ - 'style' : 'Auto' , \ 'weight' : 'Auto' , \ 'size' : 'Auto' , \ 'color' : 'Auto' , \ @@ -44,7 +43,6 @@ 'rotation' : 'Auto'}, '_FONT_XLABEL' : { 'family' : 'Auto' , \ - 'style' : 'Auto' , \ 'weight' : 'Auto' , \ 'size' : 'Auto' , \ 'color' : 'Auto' , \ @@ -54,7 +52,6 @@ 'rotation' : 'Auto'}, '_FONT_YLABEL' : { 'family' : 'Auto' , \ - 'style' : 'Auto' , \ 'weight' : 'Auto' , \ 'size' : 'Auto' , \ 'color' : 'Auto' , \ @@ -69,7 +66,6 @@ 'lw' : 'Auto'}, '_FONT_XTICK' : { 'family' : 'Auto' , \ - 'style' : 'Auto' , \ 'weight' : 'Auto' , \ 'size' : 'Auto' , \ 'color' : 'Auto' , \ @@ -79,7 +75,6 @@ 'rotation' : 'Auto'}, '_FONT_YTICK' : { 'family' : 'Auto' , \ - 'style' : 'Auto' , \ 'weight' : 'Auto' , \ 'size' : 'Auto' , \ 'color' : 'Auto' , \ @@ -133,7 +128,6 @@ 'color' : 'Auto'}, '_TEXT_FONT' : { 'family' : 'Auto' , \ - 'style' : 'Auto' , \ 'weight' : 'Auto' , \ 'size' : 'Auto' , \ 'color' : 'Auto' , \ @@ -184,10 +178,10 @@ 'hatch' : 'Auto' , \ 'facecolor' : 'Auto' , \ 'bottom' : 'Auto' , \ - 'log' : 'Auto'}, + 'log' : 'Auto' , \ + 'alpha' : 'Auto'}, '_LEGEND_FONTPROPS' : { 'family' : 'sans-serif' , \ - 'style' : 'normal' , \ 'weight' : 'normal' , \ 'size' : 'medium'}, @@ -201,7 +195,6 @@ 'framealpha' : 1.0 , \ 'ncol' : 1 , \ 'title' : None , \ - 'fontsize' : 'medium' , \ 'borderpad' : 0.1 , \ 'labelspacing' : 0.1 , \ 'handletextpad' : 0.25 , \