Пример #1
0
    def __init__(self,
                 title=None,
                 figsize=(5, 5),
                 columns=None,
                 tightlayout=True,
                 data=None,
                 plot_groupmean=None,
                 plot_samplemean=None,
                 plot_samplebase=None,
                 plot_groupbase=None,
                 plot_other=None,
                 base_alpha=None,
                 ignore_samples=None,
                 result_from_means=None,
                 **fig_props):
        """
        Container for visuals.

        Parameters
        ----------
           title: str
              title text written on top of figure

        """
        self.plt_props, self.txt_props, kwargs = Visual.separate_plt_props_from_kwargs(
            **fig_props)

        self.log = logging.getLogger(__name__)
        self.log.info('CREATING new figure')
        self.logged_implemented = False
        self.__log_implemented(ignore_once=False)
        # create dictionary for visuals {visual_name:visual_object}
        self._visuals = []
        self._n_visuals = 0
        self.columns = columns
        self.tightlayout = tightlayout
        self.xsize, self.ysize = figsize
        self._fig = None
        self.title = title

        self.data = RockPy3.core.utils.sort_input(data)

        mlist, mean_list = RockPy3.core.utils.mlist_from_input(data)

        self.calculation_parameter = []

        self.plot_groupmean, self.plot_samplemean = plot_groupmean, plot_samplemean
        self.plot_groupbase, self.plot_samplebase = plot_groupbase, plot_samplebase
        self.plot_other = plot_other

        self.result_from_means = result_from_means
        self.base_alpha = base_alpha
        self.ignore_samples = ignore_samples
Пример #2
0
    def __init__(self, title=None, figsize=(5, 5), columns=None, tightlayout=True,
                 data=None,
                 plot_groupmean=None, plot_samplemean=None, plot_samplebase=None, plot_groupbase=None,
                 plot_other=None,
                 base_alpha=None, ignore_samples=None, result_from_means=None,
                 **fig_props
                 ):
        """
        Container for visuals.

        Parameters
        ----------
           title: str
              title text written on top of figure

        """
        self.plt_props, self.txt_props, kwargs = Visual.separate_plt_props_from_kwargs(**fig_props)

        self.log = logging.getLogger(__name__)
        self.log.info('CREATING new figure')
        self.logged_implemented = False
        self.__log_implemented(ignore_once=False)
        # create dictionary for visuals {visual_name:visual_object}
        self._visuals = []
        self._n_visuals = 0
        self.columns = columns
        self.tightlayout = tightlayout
        self.xsize, self.ysize = figsize
        self._fig = None
        self.title = title

        self.data = RockPy3.core.utils.sort_input(data)

        mlist, mean_list = RockPy3.core.utils.mlist_from_input(data)

        self.calculation_parameter = []

        self.plot_groupmean, self.plot_samplemean = plot_groupmean, plot_samplemean
        self.plot_groupbase, self.plot_samplebase = plot_groupbase, plot_samplebase
        self.plot_other = plot_other

        self.result_from_means = result_from_means
        self.base_alpha = base_alpha
        self.ignore_samples = ignore_samples
Пример #3
0
    def show(
        self,
        xlim=None,
        ylim=None,
        equal_lims=False,
        center_lims=False,
        save_path=None,
        pad=0.4,
        w_pad=0.5,
        h_pad=1.0,
        file_name=None,
        format='pdf',
        legend=True,
        sort_labels=True,
        return_figure=False,
        append_to_filename='',
    ):
        """
        calls all visuals

        Raises
        ------
            TypeError if no visuals have been added
        """

        self._fig, self.axes = self._create_fig(xsize=self.xsize,
                                                ysize=self.ysize)

        if not self.visuals:
            self.log.error(
                'NO VISUALS ADDED! Please add any of the followig visuals:')
            for visual in sorted(Visual.implemented_visuals()):
                self.log.info('\t %s' % visual)
            raise TypeError('add a visual')

        # actual plotting of the visuals
        self.plt_all()

        for name, type, visual in self._visuals:
            if visual.xlim:
                visual.ax.set_xlim(visual.xlim)
            if visual.ylim:
                visual.ax.set_ylim(visual.ylim)

            if visual.xscale:
                visual.ax.set_xscale(visual.xscale)
            if visual.yscale:
                visual.ax.set_yscale(visual.yscale)

            # prevent scientific notation for x axis
            # if type in ('thermocurve', ):
            visual.ax.ticklabel_format(style='plain', axis='x')
            # else:
            #     xlim = visual.ax.get_xlim()
            #     ylim = visual.ax.get_ylim()

        if xlim == 'equal' or ylim == 'equal' or equal_lims:
            if equal_lims:
                xlim, ylim = self.get_xylims()

            if center_lims:
                xl = max(np.abs(xlim))
                yl = max(np.abs(ylim))
                xlim = [-xl, xl]
                ylim = [-yl, yl]

            # cycle through visuals to set
            for name, type, visual in self._visuals:
                if xlim == 'equal' or equal_lims:
                    visual.ax.set_xlim(xlim)
                if ylim == 'equal' or equal_lims:
                    visual.ax.set_ylim(ylim)

        # prepare legends for individual visuals
        for name, type, visual in self._visuals:
            # check if the legend should be drawn accoring to the visual.legend dictionary
            if not visual.show_legend():
                continue

            if not legend:
                break
            handles, labels = visual.ax.get_legend_handles_labels()
            if not all(i for i in (handles, labels)):
                continue

            # sorting of labels
            if sort_labels:
                labels, handles = zip(
                    *sorted(zip(labels, handles), key=lambda t: t[0]))
            visual.ax.legend(handles, labels, **visual.legend_options)

        # check if two entries and each is float or int
        if xlim:
            if len(xlim) == 2 and any(
                    isinstance(i, (float, int)) for i in xlim):
                for name, type, visual in self._visuals:
                    visual.ax.set_xlim(xlim)
        # check if two entries and each is float or int
        if ylim:
            if len(ylim) == 2 and any(
                    isinstance(i, (float, int)) for i in ylim):
                for name, type, visual in self._visuals:
                    visual.ax.set_ylim(ylim)

        self._fig.set_tight_layout(tight={
            'pad': pad,
            'w_pad': w_pad,
            'h_pad': h_pad
        })

        if self.title:
            self._fig.suptitle(self.title, fontsize=20)
            # (left, bottom, right, top) in the normalized figure coordinate that the whole subplots area
            # (including labels) will fit into
            self._fig.set_tight_layout(tight={'rect': (0, 0, 1, 0.95)})

        if return_figure:
            return self._fig

        if save_path:
            if save_path.lower() == 'desktop':
                if not file_name:
                    file_name = os.path.basename(inspect.stack()[-1][1])
                    file_name += append_to_filename
                save_path = os.path.join(os.path.expanduser('~'), 'Desktop',
                                         file_name)
            else:
                save_path = os.path.join(save_path, file_name)

            if not format in file_name:
                save_path += '.'
                save_path += format

            plt.savefig(save_path)
        else:
            with RockPy3.ignored(AttributeError):
                self._fig.canvas.manager.window.raise_()
            plt.show()
            plt.close('all')
Пример #4
0
    def show(self,
             xlim=None, ylim=None,
             equal_lims=False, center_lims=False,
             save_path=None,
             pad=0.4, w_pad=0.5, h_pad=1.0,
             file_name=None, format='pdf',
             legend=True, sort_labels = True,
             return_figure=False,
             append_to_filename = '',
             ):
        """
        calls all visuals

        Raises
        ------
            TypeError if no visuals have been added
        """

        self._fig, self.axes = self._create_fig(xsize=self.xsize, ysize=self.ysize)

        if not self.visuals:
            self.log.error('NO VISUALS ADDED! Please add any of the followig visuals:')
            for visual in sorted(Visual.implemented_visuals()):
                self.log.info('\t %s' % visual)
            raise TypeError('add a visual')

        # actual plotting of the visuals
        self.plt_all()

        for name, type, visual in self._visuals:
            if visual.xlim:
                visual.ax.set_xlim(visual.xlim)
            if visual.ylim:
                visual.ax.set_ylim(visual.ylim)

            if visual.xscale:
                visual.ax.set_xscale(visual.xscale)
            if visual.yscale:
                visual.ax.set_yscale(visual.yscale)

            # prevent scientific notation for x axis
            # if type in ('thermocurve', ):
            visual.ax.ticklabel_format(style='plain', axis='x')
            # else:
            #     xlim = visual.ax.get_xlim()
            #     ylim = visual.ax.get_ylim()

        if xlim == 'equal' or ylim == 'equal' or equal_lims:
            if equal_lims:
                xlim, ylim = self.get_xylims()

            if center_lims:
                xl = max(np.abs(xlim))
                yl = max(np.abs(ylim))
                xlim = [-xl, xl]
                ylim = [-yl, yl]

            # cycle through visuals to set
            for name, type, visual in self._visuals:
                if xlim == 'equal' or equal_lims:
                    visual.ax.set_xlim(xlim)
                if ylim == 'equal' or equal_lims:
                    visual.ax.set_ylim(ylim)

        # prepare legends for individual visuals
        for name, type, visual in self._visuals:
            # check if the legend should be drawn accoring to the visual.legend dictionary
            if not visual.show_legend():
                continue

            if not legend:
                break
            handles, labels = visual.ax.get_legend_handles_labels()
            if not all(i for i in (handles, labels)):
                continue

            # sorting of labels
            if sort_labels:
                labels, handles = zip(*sorted(zip(labels, handles), key=lambda t: t[0]))
            visual.ax.legend(handles, labels, **visual.legend_options)

        # check if two entries and each is float or int
        if xlim:
            if len(xlim) == 2 and any(isinstance(i, (float, int)) for i in xlim):
                for name, type, visual in self._visuals:
                    visual.ax.set_xlim(xlim)
        # check if two entries and each is float or int
        if ylim:
            if len(ylim) == 2 and any(isinstance(i, (float, int)) for i in ylim):
                for name, type, visual in self._visuals:
                    visual.ax.set_ylim(ylim)

        self._fig.set_tight_layout(tight={'pad': pad, 'w_pad': w_pad, 'h_pad': h_pad})

        if self.title:
            self._fig.suptitle(self.title, fontsize=20)
            # (left, bottom, right, top) in the normalized figure coordinate that the whole subplots area
            # (including labels) will fit into
            self._fig.set_tight_layout(tight={'rect': (0, 0, 1, 0.95)})

        if return_figure:
            return self._fig

        if save_path:
            if save_path.lower() == 'desktop':
                if not file_name:
                    file_name = os.path.basename(inspect.stack()[-1][1])
                    file_name += append_to_filename
                save_path = os.path.join(os.path.expanduser('~'), 'Desktop', file_name)
            else:
                save_path = os.path.join(save_path, file_name)

            if not format in file_name:
                save_path +='.'
                save_path += format

            plt.savefig(save_path)
        else:
            with RockPy3.ignored(AttributeError):
                self._fig.canvas.manager.window.raise_()
            plt.show()
            plt.close('all')