Skip to content

Commit e7fca6e

Browse files
authored
rename files (#42)
* rename files * rename files * remove stray characters
1 parent 10d4744 commit e7fca6e

File tree

4 files changed

+70
-69
lines changed

4 files changed

+70
-69
lines changed

mplotutils/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# flake8: noqa
22

3-
from . import _colorbar, cartopy_utils, mpl_utils
3+
from . import _colorbar, cartopy_utils, colormaps
44
from ._colorbar import *
55
from .cartopy_utils import *
6-
from .mpl_utils import *
6+
from .colormaps import *
7+
from .map_layout import set_map_layout
78
from .xrcompat import *
89

910
try:

mplotutils/cartopy_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import shapely.geometry as sgeom
55
from cartopy.mpl.gridliner import LATITUDE_FORMATTER, LONGITUDE_FORMATTER
66

7-
from .mpl_utils import _get_label_attr
7+
from .colormaps import _get_label_attr
88

99
# =============================================================================
1010

Lines changed: 1 addition & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import matplotlib as mpl
2-
import matplotlib.pyplot as plt
32
import numpy as np
43
from matplotlib.colors import from_levels_and_colors
54

@@ -67,7 +66,7 @@ def _color_palette(cmap, n_colors):
6766
except ValueError:
6867
# ValueError happens when mpl doesn't like a colormap, try seaborn
6968
try:
70-
from seaborn.apionly import color_palette
69+
from seaborn import color_palette
7170

7271
pal = color_palette(cmap, n_colors=n_colors)
7372
except (ValueError, ImportError):
@@ -81,70 +80,6 @@ def _color_palette(cmap, n_colors):
8180
return pal
8281

8382

84-
def set_map_layout(axes, width=17.0, nrow=None, ncol=None):
85-
"""
86-
set figure height, given width
87-
88-
Needs to be called after all plotting is done.
89-
90-
Parameters
91-
----------
92-
axes : ndarray of (Geo)Axes
93-
Array with all axes of the figure.
94-
width : float, default: 17
95-
Width of the full figure in cm.
96-
nrow : integer, default: None
97-
manually set the number of rows of subplots. Good when using gridspec.
98-
However, subplots must span the same number of gridspec rows & columns.
99-
Either none or both of 'nrow' and 'ncol' must be set.
100-
ncol : integer, default: None
101-
As nrow but for the number of rows.
102-
103-
Notes
104-
-----
105-
Only works if all the axes have the same aspect ratio.
106-
"""
107-
108-
if (nrow is None and ncol is not None) or (nrow is not None and ncol is None):
109-
raise ValueError("Must set none or both of 'nrow' and 'ncol'")
110-
111-
if isinstance(axes, plt.Axes):
112-
ax = axes
113-
else:
114-
# assumes the first of the axes is representative for all
115-
ax = axes.flat[0]
116-
117-
# read figure data
118-
f = ax.get_figure()
119-
120-
bottom = f.subplotpars.bottom
121-
top = f.subplotpars.top
122-
left = f.subplotpars.left
123-
right = f.subplotpars.right
124-
hspace = f.subplotpars.hspace
125-
wspace = f.subplotpars.wspace
126-
127-
# data ratio is the aspect
128-
aspect = ax.get_data_ratio()
129-
130-
if nrow is None and ncol is None:
131-
# get geometry tells how many subplots there are
132-
nrow, ncol, __, __ = ax.get_subplotspec().get_geometry()
133-
134-
# width of one plot, taking into account
135-
# left * wf, (1-right) * wf, ncol * wp, (1-ncol) * wp * wspace
136-
wp = (width - width * (left + (1 - right))) / (ncol + (ncol - 1) * wspace)
137-
138-
# height of one plot
139-
hp = wp * aspect
140-
141-
# height of figure
142-
height = (hp * (nrow + ((nrow - 1) * hspace))) / (1.0 - (bottom + (1 - top)))
143-
144-
f.set_figwidth(width / 2.54)
145-
f.set_figheight(height / 2.54)
146-
147-
14883
def _get_label_attr(labelpad, size, weight):
14984

15085
if labelpad is None:

mplotutils/map_layout.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import matplotlib.pyplot as plt
2+
import numpy as np
3+
4+
5+
def set_map_layout(axes, width=17.0, nrow=None, ncol=None):
6+
"""set figure height, given width, taking axes' aspect ratio into account
7+
8+
Needs to be called after all plotting is done.
9+
10+
Parameters
11+
----------
12+
axes : (Geo)Axes | iterable of (Geo)Axes
13+
Array with all axes of the figure.
14+
width : float, default: 17
15+
Width of the full figure in cm.
16+
nrow : integer, default: None
17+
manually set the number of rows of subplots. Good when using gridspec.
18+
However, subplots must span the same number of gridspec rows & columns.
19+
Either none or both of 'nrow' and 'ncol' must be set.
20+
ncol : integer, default: None
21+
As nrow but for the number of rows.
22+
23+
Notes
24+
-----
25+
Only works if all the axes have the same aspect ratio.
26+
"""
27+
28+
if (nrow is None and ncol is not None) or (nrow is not None and ncol is None):
29+
raise ValueError("Must set none or both of 'nrow' and 'ncol'")
30+
31+
if isinstance(axes, plt.Axes):
32+
ax = axes
33+
else:
34+
# assumes the first of the axes is representative for all
35+
ax = np.asarray(axes).flat[0]
36+
37+
# read figure data
38+
f = ax.get_figure()
39+
40+
bottom = f.subplotpars.bottom
41+
top = f.subplotpars.top
42+
left = f.subplotpars.left
43+
right = f.subplotpars.right
44+
hspace = f.subplotpars.hspace
45+
wspace = f.subplotpars.wspace
46+
47+
# data ratio is the aspect
48+
aspect = ax.get_data_ratio()
49+
50+
if nrow is None and ncol is None:
51+
# get geometry tells how many subplots there are
52+
nrow, ncol, __, __ = ax.get_subplotspec().get_geometry()
53+
54+
# width of one plot, taking into account
55+
# left * wf, (1-right) * wf, ncol * wp, (1-ncol) * wp * wspace
56+
wp = (width - width * (left + (1 - right))) / (ncol + (ncol - 1) * wspace)
57+
58+
# height of one plot
59+
hp = wp * aspect
60+
61+
# height of figure
62+
height = (hp * (nrow + ((nrow - 1) * hspace))) / (1.0 - (bottom + (1 - top)))
63+
64+
f.set_figwidth(width / 2.54)
65+
f.set_figheight(height / 2.54)

0 commit comments

Comments
 (0)