def save_plot(self, fig, axes, width=None, height=None, bottom=None, top=None, left=None, right=None, sep=()): # Attributes or parameters # w = width if width != None else self.width h = height if height != None else self.height b = bottom if bottom != None else self.bottom t = top if top != None else self.top l = left if left != None else self.left r = right if right != None else self.right # Adjust # fig.set_figwidth(w) fig.set_figheight(h) fig.subplots_adjust(hspace=0.0, bottom=b, top=t, left=l, right=r) # Data and source # if hasattr(self, 'dev_mode'): fig.text(0.99, 0.98, time.asctime(), horizontalalignment='right') job_name = os.environ.get('SLURM_JOB_NAME', 'Unnamed') user_msg = 'user: %s, job: %s' % (getpass.getuser(), job_name) fig.text(0.01, 0.98, user_msg, horizontalalignment='left') # Nice digit grouping # if 'x' in sep: seperate = lambda x,pos: split_thousands(x) axes.xaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(seperate)) if 'y' in sep: seperate = lambda y,pos: split_thousands(y) axes.yaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(seperate)) # Save it as different formats # for ext in self.formats: fig.savefig(self.path.replace_extension(ext))
def save_plot(self, fig, axes, **kwargs): # Parameters # self.params = {} for key in self.default_params: if key in kwargs: self.params[key] = kwargs[key] elif hasattr(self, key): self.params[key] = getattr(self, key) elif self.default_params[key] is not None: self.params[key] = self.default_params[key] # Backwards compatibility # if kwargs.get('x_log', False): self.params['x_scale'] = 'symlog' if kwargs.get('y_log', False): self.params['y_scale'] = 'symlog' # Log # if 'x_scale' in self.params: axes.set_xscale(self.params['x_scale']) if 'y_scale' in self.params: axes.set_yscale(self.params['y_scale']) # Axis limits # if 'x_min' in self.params: axes.set_xlim(self.params['x_min'], axes.get_xlim()[1]) if 'x_max' in self.params: axes.set_xlim(axes.get_xlim()[0], self.params['x_max']) if 'y_min' in self.params: axes.set_xlim(self.params['y_min'], axes.get_ylim()[1]) if 'y_max' in self.params: axes.set_xlim(axes.get_ylim()[0], self.params['y_max']) # Title # title = self.params.get('title', False) if title: axes.set_title(title) # Axes labels # if self.params.get('x_label'): axes.set_xlabel(self.params['x_label']) if self.params.get('y_label'): axes.set_ylabel(self.params['y_label']) # Adjust # fig.set_figwidth(self.params['width']) fig.set_figheight(self.params['height']) fig.subplots_adjust(hspace=0.0, bottom = self.params['bottom'], top = self.params['top'], left = self.params['left'], right = self.params['right']) # Grid # axes.xaxis.grid(self.params['x_grid']) axes.yaxis.grid(self.params['y_grid']) # Data and source extra text # if hasattr(self, 'dev_mode') and self.dev_mode is True: fig.text(0.99, 0.98, time.asctime(), horizontalalignment='right') job_name = os.environ.get('SLURM_JOB_NAME', 'Unnamed') user_msg = 'user: %s, job: %s' % (getpass.getuser(), job_name) fig.text(0.01, 0.98, user_msg, horizontalalignment='left') # Nice digit grouping # if 'x' in self.params['sep']: separate = lambda x,pos: split_thousands(x) axes.xaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(separate)) if 'y' in self.params['sep']: separate = lambda y,pos: split_thousands(y) axes.yaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(separate)) # Add custom labels # if 'x_labels' in self.params: axes.set_xticklabels(self.params['x_labels']) if 'x_labels_rot' in self.params: pyplot.setp(axes.xaxis.get_majorticklabels(), rotation=self.params['x_labels_rot']) # Possibility to overwrite path # if 'path' in self.params: path = FilePath(self.params['path']) elif hasattr(self, 'path'): path = FilePath(self.path) else: path = FilePath(self.short_name + '.pdf') # Save it as different formats # for ext in self.params['formats']: fig.savefig(path.replace_extension(ext))