77from typing import List , Optional , Tuple , Union
88
99import colorcet
10- import matplotlib
10+ import matplotlib as mpl
1111import matplotlib .pyplot as plt
1212import numpy as np
1313import pandas as pd
@@ -247,12 +247,12 @@ def get_agg_bounds(adata: AnnData) -> Tuple[int, int, int, int]:
247247
248248# Means to shift the scale of colormaps:
249249def shiftedColorMap (
250- cmap : matplotlib .colors .ListedColormap ,
250+ cmap : mpl .colors .ListedColormap ,
251251 start : float = 0 ,
252252 midpoint : float = 0.5 ,
253253 stop : float = 1.0 ,
254254 name : str = "shiftedcmap" ,
255- ) -> matplotlib .colors .ListedColormap :
255+ ) -> mpl .colors .ListedColormap :
256256 """
257257 Function to offset the "center" of a colormap. Useful for
258258 data with a negative min and positive max, and you want the
@@ -279,7 +279,7 @@ def shiftedColorMap(
279279 newcmap: a new colormap that has the middle point of the colormap shifted.
280280 """
281281 # Check for existing shifted colormap:
282- matplotlib .cm .ColormapRegistry .unregister (plt .colormaps , name = "shiftedcmap" )
282+ mpl .cm .ColormapRegistry .unregister (plt .colormaps , name = "shiftedcmap" )
283283
284284 cdict = {"red" : [], "green" : [], "blue" : [], "alpha" : []}
285285
@@ -299,54 +299,48 @@ def shiftedColorMap(
299299 cdict ["blue" ].append ((si , b , b ))
300300 cdict ["alpha" ].append ((si , a , a ))
301301
302- newcmap = matplotlib .colors .LinearSegmentedColormap (name , cdict )
303- plt . register_cmap (cmap = newcmap )
302+ newcmap = mpl .colors .LinearSegmentedColormap (name , cdict )
303+ mpl . colormaps . register (cmap = newcmap )
304304
305305 return newcmap
306306
307307
308- fire_cmap = matplotlib .colors .LinearSegmentedColormap .from_list ("fire" , colorcet .fire )
309- darkblue_cmap = matplotlib .colors .LinearSegmentedColormap .from_list ("darkblue" , colorcet .kbc )
310- darkgreen_cmap = matplotlib .colors .LinearSegmentedColormap .from_list ("darkgreen" , colorcet .kgy )
311- darkred_cmap = matplotlib .colors .LinearSegmentedColormap .from_list (
312- "darkred" , colors = colorcet .linear_kry_5_95_c72 [:192 ], N = 256
313- )
314- darkpurple_cmap = matplotlib .colors .LinearSegmentedColormap .from_list ("darkpurple" , colorcet .linear_bmw_5_95_c89 )
308+ fire_cmap = mpl .colors .LinearSegmentedColormap .from_list ("fire" , colorcet .fire )
309+ darkblue_cmap = mpl .colors .LinearSegmentedColormap .from_list ("darkblue" , colorcet .kbc )
310+ darkgreen_cmap = mpl .colors .LinearSegmentedColormap .from_list ("darkgreen" , colorcet .kgy )
311+ darkred_cmap = mpl .colors .LinearSegmentedColormap .from_list ("darkred" , colors = colorcet .linear_kry_5_95_c72 [:192 ], N = 256 )
312+ darkpurple_cmap = mpl .colors .LinearSegmentedColormap .from_list ("darkpurple" , colorcet .linear_bmw_5_95_c89 )
315313# add gkr theme
316- div_blue_black_red_cmap = matplotlib .colors .LinearSegmentedColormap .from_list (
314+ div_blue_black_red_cmap = mpl .colors .LinearSegmentedColormap .from_list (
317315 "div_blue_black_red" , colorcet .diverging_gkr_60_10_c40
318316)
319317# add RdBu_r theme
320- div_blue_red_cmap = matplotlib .colors .LinearSegmentedColormap .from_list (
321- "div_blue_red" , colorcet .diverging_bwr_55_98_c37
322- )
318+ div_blue_red_cmap = mpl .colors .LinearSegmentedColormap .from_list ("div_blue_red" , colorcet .diverging_bwr_55_98_c37 )
323319# add glasbey_bw for cell annotation in white background
324- glasbey_white_cmap = matplotlib .colors .LinearSegmentedColormap .from_list ("glasbey_white" , colorcet .glasbey_bw_minc_20 )
320+ glasbey_white_cmap = mpl .colors .LinearSegmentedColormap .from_list ("glasbey_white" , colorcet .glasbey_bw_minc_20 )
325321# add glasbey_bw_minc_20_maxl_70 theme for cell annotation in dark background
326- glasbey_dark_cmap = matplotlib .colors .LinearSegmentedColormap .from_list (
327- "glasbey_dark" , colorcet .glasbey_bw_minc_20_maxl_70
328- )
322+ glasbey_dark_cmap = mpl .colors .LinearSegmentedColormap .from_list ("glasbey_dark" , colorcet .glasbey_bw_minc_20_maxl_70 )
329323
330324with warnings .catch_warnings ():
331325 warnings .simplefilter ("ignore" )
332- if "fire" not in matplotlib .colormaps ():
333- plt . register_cmap ( "fire" , fire_cmap )
334- if "darkblue" not in matplotlib .colormaps ():
335- plt . register_cmap ( "darkblue" , darkblue_cmap )
336- if "darkgreen" not in matplotlib .colormaps ():
337- plt . register_cmap ( "darkgreen" , darkgreen_cmap )
338- if "darkred" not in matplotlib .colormaps ():
339- plt . register_cmap ( "darkred" , darkred_cmap )
340- if "darkpurple" not in matplotlib .colormaps ():
341- plt . register_cmap ( "darkpurple" , darkpurple_cmap )
342- if "div_blue_black_red" not in matplotlib .colormaps ():
343- plt . register_cmap ( "div_blue_black_red" , div_blue_black_red_cmap )
344- if "div_blue_red" not in matplotlib .colormaps ():
345- plt . register_cmap ( "div_blue_red" , div_blue_red_cmap )
346- if "glasbey_white" not in matplotlib .colormaps ():
347- plt . register_cmap ( "glasbey_white" , glasbey_white_cmap )
348- if "glasbey_dark" not in matplotlib .colormaps ():
349- plt . register_cmap ( "glasbey_dark" , glasbey_dark_cmap )
326+ if "fire" not in mpl .colormaps ():
327+ mpl . colormaps . register ( cmap = fire_cmap , name = "fire" )
328+ if "darkblue" not in mpl .colormaps ():
329+ mpl . colormaps . register ( cmap = darkblue_cmap , name = "darkblue" )
330+ if "darkgreen" not in mpl .colormaps ():
331+ mpl . colormaps . register ( cmap = darkgreen_cmap , name = "darkgreen" )
332+ if "darkred" not in mpl .colormaps ():
333+ mpl . colormaps . register ( cmap = darkred_cmap , name = "darkred" )
334+ if "darkpurple" not in mpl .colormaps ():
335+ mpl . colormaps . register ( cmap = darkpurple_cmap , name = "darkpurple" )
336+ if "div_blue_black_red" not in mpl .colormaps ():
337+ mpl . colormaps . register ( cmap = div_blue_black_red_cmap , name = "div_blue_black_red" )
338+ if "div_blue_red" not in mpl .colormaps ():
339+ mpl . colormaps . register ( cmap = div_blue_red_cmap , name = "div_blue_red" )
340+ if "glasbey_white" not in mpl .colormaps ():
341+ mpl . colormaps . register ( cmap = glasbey_white_cmap , name = "glasbey_white" )
342+ if "glasbey_dark" not in mpl .colormaps ():
343+ mpl . colormaps . register ( cmap = glasbey_dark_cmap , name = "glasbey_dark" )
350344
351345_themes = {
352346 "fire" : {
@@ -512,7 +506,7 @@ def config_spateo_rcParams(
512506 background : str = "white" ,
513507 prop_cycle : List [str ] = zebrafish_256 ,
514508 fontsize : int = 8 ,
515- color_map : matplotlib .colors .ListedColormap = None ,
509+ color_map : mpl .colors .ListedColormap = None ,
516510 frameon : Optional [bool ] = None ,
517511) -> None :
518512 """Configure matplotlib.rcParams to spateo defaults (based on ggplot style and scanpy).
@@ -735,8 +729,8 @@ def set_pub_style(scaler: float = 1) -> None:
735729 """
736730
737731 set_figure_params ("spateo" , background = "white" )
738- matplotlib .use ("cairo" )
739- matplotlib .rcParams .update ({"font.size" : 6 * scaler })
732+ mpl .use ("cairo" )
733+ mpl .rcParams .update ({"font.size" : 6 * scaler })
740734 params = {
741735 "font.size" : 6 * scaler ,
742736 "legend.fontsize" : 6 * scaler ,
@@ -748,13 +742,13 @@ def set_pub_style(scaler: float = 1) -> None:
748742 "axes.titlepad" : 1 * scaler ,
749743 "axes.labelpad" : 1 * scaler ,
750744 }
751- matplotlib .rcParams .update (params )
745+ mpl .rcParams .update (params )
752746
753747
754748def set_pub_style_mpltex ():
755749 """formatting helper function based on mpltex package that can be used to save publishable figures"""
756750 set_figure_params ("spateo" , background = "white" )
757- matplotlib .use ("cairo" )
751+ mpl .use ("cairo" )
758752 # the following code is adapted from https://github.com/liuyxpp/mpltex
759753 # latex_preamble = r"\usepackage{siunitx}\sisetup{detect-all}\usepackage{helvet}\usepackage[eulergreek,EULERGREEK]{sansmath}\sansmath"
760754 params = {
@@ -811,4 +805,4 @@ def set_pub_style_mpltex():
811805 "axes.titlepad" : 1 ,
812806 "axes.labelpad" : 1 ,
813807 }
814- matplotlib .rcParams .update (params )
808+ mpl .rcParams .update (params )
0 commit comments