Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring of colorbar logic #349

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/spatialdata_plot/pl/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from datatree import DataTree
from matplotlib.cm import ScalarMappable
from matplotlib.colors import ListedColormap, Normalize
from mpl_toolkits.axes_grid1.axes_divider import make_axes_locatable
from scanpy._settings import settings as sc_settings
from spatialdata import get_extent
from spatialdata.models import PointsModel, get_table_keys
Expand Down Expand Up @@ -624,8 +625,10 @@ def _render_images(
_ax_show_and_transform(layer, trans_data, ax, cmap=cmap, zorder=render_params.zorder)

if legend_params.colorbar:
ax_divider = make_axes_locatable(ax)
sm = plt.cm.ScalarMappable(cmap=cmap, norm=render_params.cmap_params.norm)
fig_params.fig.colorbar(sm, ax=ax)
cax = ax_divider.append_axes("right", size="7%", pad="2%")
fig_params.fig.colorbar(sm, ax=ax, cax=cax)

# 2) Image has any number of channels but 1
else:
Expand Down
5 changes: 4 additions & 1 deletion src/spatialdata_plot/pl/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
from matplotlib.gridspec import GridSpec
from matplotlib.transforms import CompositeGenericTransform
from matplotlib_scalebar.scalebar import ScaleBar
from mpl_toolkits.axes_grid1.axes_divider import make_axes_locatable
from numpy.ma.core import MaskedArray
from numpy.random import default_rng
from pandas.api.types import CategoricalDtype
Expand Down Expand Up @@ -1016,7 +1017,9 @@ def _decorate_axs(
)
elif colorbar:
# TODO: na_in_legend should have some effect here
cb = plt.colorbar(cax, ax=ax, pad=0.01, fraction=0.08, aspect=30)
ax_divider = make_axes_locatable(ax)
cax2 = ax_divider.append_axes("right", size="5%", pad="2%")
cb = fig_params.fig.colorbar(cax, ax=ax, cax=cax2)
cb.solids.set_alpha(alpha)

if isinstance(scalebar_dx, list) and isinstance(scalebar_units, list):
Expand Down
Loading