# -*- coding: utf-8 -*-"""This file set ups matplotlib plot functions for the whole package.Import all necessary Matplotlib modules and set default optionsTo use this module, call the setup() function.Examples-------- >>> import crtomo.mpl >>> crtomo.mpl.setup()"""
[docs]defsetup():"""import the matplotlib modules and set the style """importsysalready_loaded='matplotlib'insys.modules# just make sure we can access matplotlib as mplimportmatplotlibasmplifnotalready_loaded:mpl.use('Agg')importmatplotlib.pyplotaspltmpl_version=[int(x)forxinmpl.__version__.split('.')]ifmpl_version[0]>=3andmpl_version[1]>6:plt.style.use("seaborn-v0_8")else:# old style for older matplotlib versions (<= 3.6)plt.style.use("seaborn")general_settings()importmpl_toolkits.axes_grid1asaxes_grid1axes_grid1returnplt,mpl
[docs]defmpl_get_cb_bound_next_to_plot(ax):""" Return the coordinates for a colorbar axes next to the provided axes object. Take into account the changes of the axes due to aspect ratio settings. Parts of this code are taken from the transforms.py file from matplotlib Important: Use only AFTER fig.subplots_adjust(...) Examples -------- >>> cb_pos = mpl_get_cb_bound_next_to_plot(fig.axes[15]) ax1 = fig.add_axes(cb_pos, frame_on=True) cmap = mpl.cm.jet_r norm = mpl.colors.Normalize(vmin=float(23), vmax=float(33)) # cmap = pm.cmap # norm = pm.norm cb1 = mpl.colorbar.ColorbarBase( ax1, cmap=cmap, norm=norm, orientation='vertical' ) cb1.locator = mpl.ticker.FixedLocator([23, 28, 33]) cb1.update_ticks() cb1.ax.artists.remove(cb1.outline) # remove framei """position=ax.get_position()figW,figH=ax.get_figure().get_size_inches()fig_aspect=figH/figWbox_aspect=ax.get_data_ratio()pb=position.frozen()pb1=pb.shrunk_to_aspect(box_aspect,pb,fig_aspect).boundsax_size=ax.get_position().boundsxdiff=(ax_size[2]-pb1[2])/2ydiff=(ax_size[3]-pb1[3])/2# the colorbar is set to 0.01 widthsizes=[ax_size[0]+xdiff+ax_size[2]+0.01,ax_size[1]+ydiff,0.01,pb1[3]]returnsizes
[docs]defmpl_get_cb_bound_below_plot(ax):""" Return the coordinates for a colorbar axes below the provided axes object. Take into account the changes of the axes due to aspect ratio settings. Important: Use only AFTER fig.subplots_adjust(...) """position=ax.get_position()figW,figH=ax.get_figure().get_size_inches()fig_aspect=figH/figWbox_aspect=ax.get_data_ratio()pb=position.frozen()pb1=pb.shrunk_to_aspect(box_aspect,pb,fig_aspect).boundsax_size=ax.get_position().bounds# xdiff = (ax_size[2] - pb1[2]) / 2# ydiff = (ax_size[3] - pb1[3]) / 2# the colorbar is set to 0.01 widthsizes=[ax_size[0],ax_size[1],pb1[2],0.03]returnsizes