Skip to content

Commit 6c6ce35

Browse files
authored
Merge pull request #253 from Sichao25/dependency
Upgrade matplotlib
2 parents 22220c5 + a38c430 commit 6c6ce35

File tree

10 files changed

+75
-81
lines changed

10 files changed

+75
-81
lines changed

requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ colorcet>=2.0.1
44
cvxopt>=1.2.3
55
csbdeep>=0.6.3
66
descartes
7-
dynamo-release>=1.3.0
7+
dynamo-release>=1.4.1
88
folium>=0.12.1
99
geopandas>=0.10.2
1010
gpytorch
@@ -14,7 +14,7 @@ kornia>=0.6.4
1414
leidenalg>=0.10.0
1515
loompy>=3.0.5
1616
mapclassify>=2.4.2
17-
matplotlib<=3.5.3
17+
matplotlib>=3.7.5
1818
nbconvert
1919
networkx>=2.6.3
2020
# ngs_tools>=1.6.0

spateo/configuration.py

Lines changed: 39 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from typing import List, Optional, Tuple, Union
88

99
import colorcet
10-
import matplotlib
10+
import matplotlib as mpl
1111
import matplotlib.pyplot as plt
1212
import numpy as np
1313
import 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:
249249
def 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

330324
with 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

754748
def 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)

spateo/digitization/contour.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def gen_cluster_image(
4141
prepared from the designated cmap.
4242
"""
4343

44+
import matplotlib as mpl
4445
import matplotlib.pyplot as plt
4546

4647
if bin_size is None:
@@ -49,7 +50,7 @@ def gen_cluster_image(
4950
lm.main_info(f"Set up the color for the clusters with the {cmap} colormap.")
5051

5152
# TODO: what if cluster number is larger than cmap.N?
52-
cmap = plt.cm.get_cmap(cmap)
53+
cmap = mpl.colormaps[cmap]
5354
colors = cmap(np.arange(cmap.N))
5455
color_ls = []
5556
for i in range(cmap.N):

spateo/plotting/static/interactions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ def plot_connections(
467467

468468
# Set label colors:
469469
if isinstance(colormap, str):
470-
cmap = mpl.cm.get_cmap(colormap)
470+
cmap = mpl.colormaps[colormap]
471471
else:
472472
cmap = colormap
473473

spateo/plotting/static/scatters.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ def scatters(
346346
then this will simply display inline.
347347
"""
348348

349+
import matplotlib as mpl
349350
import matplotlib.pyplot as plt
350351
from matplotlib import rcParams
351352
from matplotlib.colors import rgb2hex, to_hex
@@ -776,7 +777,7 @@ def _plot_basis_layer(cur_b, cur_l):
776777
if stack_colors:
777778
# main_debug("stack colors: changing cmap")
778779
_cmap = stack_colors_cmaps[ax_index % len(stack_colors_cmaps)]
779-
max_color = matplotlib.cm.get_cmap(_cmap)(float("inf"))
780+
max_color = mpl.colormaps[_cmap](float("inf"))
780781
legend_circle = Line2D(
781782
[0],
782783
[0],

spateo/plotting/static/three_d_plot/three_dims_plots.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1251,7 +1251,7 @@ def visualize_3D_increasing_direction_gradient(
12511251
1 - (1 - coords_norm) * (1 - new_center) / 0.5, # Compress the upper half
12521252
)
12531253

1254-
colors = mpl.cm.get_cmap(cmap)(coords_norm)
1254+
colors = mpl.colormaps[cmap](coords_norm)
12551255
# Convert colors to hex format:
12561256
colors = ["#" + "".join([f"{int(c * 255):02x}" for c in color[:3]]) for color in colors]
12571257

spateo/plotting/static/utils.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
warnings.simplefilter("ignore")
1111
import geopandas as gpd
1212

13-
import matplotlib
13+
import matplotlib as mpl
1414
import matplotlib.patheffects as PathEffects
1515
import matplotlib.pyplot as plt
1616
import mpl_toolkits
@@ -75,23 +75,21 @@ def _get_adata_color_vec(adata, layer, col):
7575

7676

7777
def map2color(val, min=None, max=None, cmap="viridis"):
78-
import matplotlib
7978
import matplotlib.cm as cm
80-
import matplotlib.pyplot as plt
8179

8280
minima = np.min(val) if min is None else min
8381
maxima = np.max(val) if max is None else max
8482

85-
norm = matplotlib.colors.Normalize(vmin=minima, vmax=maxima, clip=True)
86-
mapper = cm.ScalarMappable(norm=norm, cmap=plt.get_cmap(cmap))
83+
norm = mpl.colors.Normalize(vmin=minima, vmax=maxima, clip=True)
84+
mapper = cm.ScalarMappable(norm=norm, cmap=mpl.colormaps[cmap])
8785

8886
cols = [mapper.to_rgba(v) for v in val]
8987

9088
return cols
9189

9290

9391
def _to_hex(arr):
94-
return [matplotlib.colors.to_hex(c) for c in arr]
92+
return [mpl.colors.to_hex(c) for c in arr]
9593

9694

9795
# https://stackoverflow.com/questions/8468855/convert-a-rgb-colour-value-to-decimal
@@ -399,7 +397,7 @@ def _matplotlib_points(
399397
)
400398
if color_key is None:
401399
# main_debug("color_key is None")
402-
cmap = copy.copy(matplotlib.cm.get_cmap(color_key_cmap))
400+
cmap = copy.copy(mpl.colormaps[color_key_cmap])
403401
cmap.set_bad("lightgray")
404402
colors = None
405403

@@ -594,12 +592,12 @@ def _matplotlib_points(
594592
# Color by values
595593
elif values is not None:
596594
# main_debug("drawing points by values")
597-
cmap_ = copy.copy(matplotlib.cm.get_cmap(cmap))
595+
cmap_ = copy.copy(mpl.colormaps[cmap])
598596
cmap_.set_bad("lightgray")
599597

600598
with warnings.catch_warnings():
601599
warnings.simplefilter("ignore")
602-
matplotlib.cm.register_cmap(name=cmap_.name, cmap=cmap_, override_builtin=True)
600+
mpl.colormaps.register(name=cmap_.name, cmap=cmap_, force=True)
603601

604602
if values.shape[0] != points.shape[0]:
605603
raise ValueError(
@@ -780,9 +778,9 @@ def _matplotlib_points(
780778
if "norm" in kwargs:
781779
norm = kwargs["norm"]
782780
else:
783-
norm = matplotlib.colors.Normalize(vmin=_vmin, vmax=_vmax)
781+
norm = mpl.colors.Normalize(vmin=_vmin, vmax=_vmax)
784782

785-
mappable = matplotlib.cm.ScalarMappable(norm=norm, cmap=cmap)
783+
mappable = mpl.cm.ScalarMappable(norm=norm, cmap=cmap)
786784
mappable.set_array(values)
787785
if show_colorbar:
788786
cb = plt.colorbar(mappable, cax=set_colorbar(ax, inset_dict), ax=ax)
@@ -791,12 +789,12 @@ def _matplotlib_points(
791789
cb.locator = MaxNLocator(nbins=3, integer=True)
792790
cb.update_ticks()
793791

794-
cmap = matplotlib.cm.get_cmap(cmap)
792+
cmap = mpl.colormaps[cmap]
795793
colors = cmap(values)
796794
# No color (just pick the midpoint of the cmap)
797795
else:
798796
# main_debug("drawing points without color passed in args, using midpoint of the cmap")
799-
colors = plt.get_cmap(cmap)(0.5)
797+
colors = mpl.colormaps[cmap](0.5)
800798
if geo:
801799
_geo_projection(ax, points, color=colors, **kwargs)
802800

@@ -823,7 +821,7 @@ def _matplotlib_points(
823821
ax.legend(
824822
handles=legend_elements,
825823
bbox_to_anchor=(1.04, 1),
826-
loc=matplotlib.rcParams["legend.loc"],
824+
loc=mpl.rcParams["legend.loc"],
827825
ncol=len(unique_labels) // 20 + 1,
828826
prop=dict(size=8),
829827
)
@@ -1486,8 +1484,8 @@ def save_return_show_fig_utils(
14861484
prefix: str,
14871485
save_kwargs: Dict,
14881486
total_panels: int,
1489-
fig: matplotlib.figure.Figure,
1490-
axes: matplotlib.axes.Axes,
1487+
fig: mpl.figure.Figure,
1488+
axes: mpl.axes.Axes,
14911489
return_all: bool,
14921490
return_all_list: Union[List, Tuple, None],
14931491
) -> Optional[Tuple]:
@@ -1585,7 +1583,7 @@ def check_colornorm(
15851583
vmin: Union[None, float] = None,
15861584
vmax: Union[None, float] = None,
15871585
vcenter: Union[None, float] = None,
1588-
norm: Union[None, matplotlib.colors.Normalize] = None,
1586+
norm: Union[None, mpl.colors.Normalize] = None,
15891587
):
15901588
"""
15911589
When plotting continuous variables, configure a normalizer object for the purposes of mapping the data to varying
@@ -1824,7 +1822,7 @@ def dendrogram(
18241822

18251823

18261824
def plot_dendrogram(
1827-
dendro_ax: matplotlib.axes.Axes,
1825+
dendro_ax: mpl.axes.Axes,
18281826
adata: AnnData,
18291827
cat_key: str,
18301828
dendrogram_key: Union[None, str] = None,

spateo/tdr/models/utilities/label_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def add_model_labels(
5555
# Set raw hex.
5656
if isinstance(colormap, str):
5757
if colormap in list(mpl.colormaps()):
58-
lscmap = mpl.cm.get_cmap(colormap)
58+
lscmap = mpl.colormaps[colormap]
5959
raw_hex_list = [mpl.colors.to_hex(lscmap(i)) for i in np.linspace(0, 1, len(cu_arr))]
6060
for label, color in zip(cu_arr, raw_hex_list):
6161
raw_labels_hex[raw_labels_hex == label] = color

0 commit comments

Comments
 (0)