Create a nice looking plot of the sensitivity distribution for the
        given configuration nr. Configs start at 1!

        Parameters
        ----------
        config_nr : int, optional
            The configuration number (starting with 0) to compute the
            sensitivity for.
        sens_data : Nx2 numpy.ndarray, optional
            If provided, use this data as sensitivity data (do not compute
            anything)
        mag_only : bool, optional
            Plot only the magnitude sensitivities
        absv : bool, optional
            If true, plot absolute values of sensitivity

        Returns
        -------
        fig
        ax

        Examples
        --------

        .. plot::

            import crtomo.debug
            import crtomo
            grid = crtomo.debug.get_grid(key=20)
            td = crtomo.tdMan(grid=grid)
            td.configs.add_to_configs([1, 5, 9, 13])
            cid_mag, cid_pha = td.add_homogeneous_model(25, 0)
            td.register_forward_model(cid_mag, cid_pha)
            td.model(sensitivities=True)
            fig, axes = td.plot_sensitivity(0)
            fig.tight_layout()
            fig.savefig('sens_plot.pdf', bboch_inches='tight')

def plot_sensitivity(self, config_nr=None, sens_data=None,
                         mag_only=False, absv=False,
                         **kwargs):
        """Create a nice looking plot of the sensitivity distribution for the
        given configuration nr. Configs start at 1!

        Parameters
        ----------
        config_nr : int, optional
            The configuration number (starting with 0) to compute the
            sensitivity for.
        sens_data : Nx2 numpy.ndarray, optional
            If provided, use this data as sensitivity data (do not compute
            anything)
        mag_only : bool, optional
            Plot only the magnitude sensitivities
        absv : bool, optional
            If true, plot absolute values of sensitivity

        Returns
        -------
        fig
        ax

        Examples
        --------

        .. plot::

            import crtomo.debug
            import crtomo
            grid = crtomo.debug.get_grid(key=20)
            td = crtomo.tdMan(grid=grid)
            td.configs.add_to_configs([1, 5, 9, 13])
            cid_mag, cid_pha = td.add_homogeneous_model(25, 0)
            td.register_forward_model(cid_mag, cid_pha)
            td.model(sensitivities=True)
            fig, axes = td.plot_sensitivity(0)
            fig.tight_layout()
            fig.savefig('sens_plot.pdf', bboch_inches='tight')

        """

        def _rescale_sensitivity(sens_data):
            norm_value = np.abs(sens_data).max()
            sens_normed = sens_data / norm_value

            indices_gt_zero = sens_data > 1e-5
            indices_lt_zero = sens_data < -1e-5

            # map all values greater than zero to the range [0.5, 1]
            x = np.log10(sens_normed[indices_gt_zero])
            # log_norm_factor = np.abs(x).max()
            log_norm_factor = -5
            y1 = 1 - x / (2 * log_norm_factor)

            # map all values smaller than zero to the range [0, 0.5]
            x = np.log10(np.abs(sens_normed[indices_lt_zero]))
            y = x / (2 * log_norm_factor)

            y2 = np.abs(y)

            # reassign values
            sens_data[:] = 0.5
            sens_data[indices_gt_zero] = y1
            sens_data[indices_lt_zero] = y2
            return sens_data

        assert config_nr is not None or sens_data is not None
        assert not (config_nr is not None and sens_data is not None)

        if config_nr is not None:
            cids = self.assignments['sensitivities'][config_nr]

            sens_mag = self.parman.parsets[cids[0]]
            sens_pha = self.parman.parsets[cids[1]]
        else:
            sens_mag = sens_data[:, 0]
            sens_pha = sens_data[:, 1]

        if absv:
            sens_mag = np.log10(np.abs(sens_mag) / np.abs(sens_mag).max())
            sens_pha = np.log10(np.abs(sens_pha) / np.abs(sens_pha).max())
            cbmin = sens_mag.min()
            cbmax = sens_mag.max()
        else:
            _rescale_sensitivity(sens_mag)
            _rescale_sensitivity(sens_pha)
            cbmin = 0
            cbmax = 1

        cmap_jet = matplotlib.cm.get_cmap('jet')
        colors = [cmap_jet(i) for i in np.arange(0, 1.1, 0.1)]
        cmap = matplotlib.colors.LinearSegmentedColormap.from_list(
            'jetn9', colors, N=9)
        over = kwargs.get('over', 'orange')
        under = kwargs.get('under', 'cyan')
        bad = kwargs.get('bad', 'white')
        cmap.set_over(over)
        cmap.set_under(under)
        cmap.set_bad(bad)
        if mag_only:
            Nx = 1
        else:
            Nx = 2

        fig, axes = plt.subplots(1, Nx, figsize=(15 / 2.54, 12 / 2.54))
        axes = np.atleast_1d(axes)
        # magnitude
        ax = axes[0]
        cid = self.parman.add_data(sens_mag)
        fig, ax, cnorm, cmap, cb, sM = self.plot.plot_elements_to_ax(
            cid=cid,
            ax=ax,
            plot_colorbar=True,
            # cmap_name='seismic',
            cmap_name='jet_r',
            cbsegments=18,
            cbmin=cbmin,
            cbmax=cbmax,
            bad='white',
            # cbmin=-cblim,
            # cbmax=cblim,
            # converter=converter_pm_log10,
            # norm = colors.SymLogNorm(
            #     linthresh=0.03,
            #     linscale=0.03,
            #     vmin=-1.0,
            #     vmax=1.0
            # ),
            # xmin=-0.25,
            # xmax=10,
            # zmin=-2,
        )
        if not absv:
            cb.set_ticks([0, 0.25, 0.5, 0.75, 1])
            cb.set_ticklabels([
                '-1',
                r'$-10^{-2.5}$',
                '0',
                r'$10^{-2.5}$',
                '1',
            ])

        # self.plot.plot_elements_to_ax(
        #     cid=cids[0],
        #     ax=ax,
        # )

        if not mag_only:
            cid = self.parman.add_data(sens_pha)
            # plot phase
            ax = axes[1]
            fig, ax, cnorm, cmap, cb, sM = self.plot.plot_elements_to_ax(
                cid=cid,
                ax=ax,
                plot_colorbar=True,
                # cmap_name='seismic',
                cmap_name='jet_r',
                cbsegments=18,
                cbmin=cbmin,
                cbmax=cbmax,
                bad='white',
            )
            if not absv:
                cb.set_ticks([0, 0.25, 0.5, 0.75, 1])
                cb.set_ticklabels([
                    '-1',
                    r'$-10^{-2.5}$',
                    '0',
                    r'$10^{-2.5}$',
                    '1',
                ])

        fig.tight_layout()

        return fig, axes