matplotlib_renderer.py 文件源码

python
阅读 31 收藏 0 点赞 0 评论 0

项目:MDT 作者: cbclab 项目源码 文件源码
def _add_colorbar(self, axis, map_name, image_figure, colorbar_label, colorbar_position):
        """Add a colorbar to the axis

        Returns:
            axis: the image axis
            colorbar_position (str): one of 'left', 'right', 'top', 'bottom'
        """
        divider = make_axes_locatable(axis)

        show_colorbar = self._get_map_attr(map_name, 'show_colorbar', self._plot_config.show_colorbars)
        show_colorbar_global = self._plot_config.show_colorbars

        if show_colorbar_global:
            axis_kwargs = dict(size="5%", pad=0.1)

            if self._plot_config.show_axis and colorbar_position in ['bottom', 'left']:
                axis_kwargs['pad'] = 0.3

            if show_colorbar and colorbar_position in ('left', 'right'):
                colorbar_axis = divider.append_axes(colorbar_position, **axis_kwargs)
            else:
                fake_axis = divider.append_axes('right', **axis_kwargs)
                fake_axis.axis('off')

            if show_colorbar and colorbar_position in ('top', 'bottom'):
                colorbar_axis = divider.append_axes(colorbar_position, **axis_kwargs)
            else:
                fake_axis = divider.append_axes('bottom', **axis_kwargs)
                fake_axis.axis('off')

        if show_colorbar:
            kwargs = dict(cax=colorbar_axis, ticks=self._get_tick_locator(map_name))
            if colorbar_label:
                kwargs.update(dict(label=colorbar_label))

            if colorbar_position in ['top', 'bottom']:
                kwargs['orientation'] = 'horizontal'

            cbar = plt.colorbar(image_figure, **kwargs)
            cbar.formatter.set_powerlimits((-3, 4))
            colorbar_axis.yaxis.set_offset_position('left')

            if colorbar_position == 'left':
                colorbar_axis.yaxis.set_ticks_position('left')
                colorbar_axis.yaxis.set_label_position('left')

            if colorbar_position == 'top':
                colorbar_axis.xaxis.set_ticks_position('top')
                colorbar_axis.xaxis.set_label_position('top')

            cbar.update_ticks()

            if cbar.ax.get_yticklabels():
                cbar.ax.get_yticklabels()[-1].set_verticalalignment('top')

            self._apply_font_colorbar_axis(colorbar_axis)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号