Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
cornish_pasdy
Manage
Activity
Members
Labels
Plan
Issues
18
Issue boards
Milestones
Wiki
Code
Merge requests
3
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
CRNS
cornish_pasdy
Commits
5edb3392
Commit
5edb3392
authored
1 year ago
by
Martin Schrön
Browse files
Options
Downloads
Patches
Plain Diff
Figure now accepts complex mosaic layouts
parent
dba163e2
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
corny/figures.py
+54
-8
54 additions, 8 deletions
corny/figures.py
with
54 additions
and
8 deletions
corny/figures.py
+
54
−
8
View file @
5edb3392
...
...
@@ -90,6 +90,7 @@ def label_abc(axes, text=[], fontsize=10):
class
Figure
:
"""
Usage:
# Map
with Figure(size=(10,8), projection=
'
flat
'
,
tiles=
'
satellite
'
, zoom=16) as ax:
...
...
@@ -99,6 +100,23 @@ class Figure:
add_colorbar(ax=ax, points=c, label=
'
Soil moisture (g/g)
'
,
bar_kw=dict(shrink=0.5, pad=0.02, aspect=20),
ticks=[0.07,0.09,0.12], ticklabels=[
"
1
"
,
"
2
"
,
"
3
"
])
# Layout regular grid
with Figure(layout=(2,2)) as axes:
axes[0][0].plot(x,y)
axes[0][1].plot(x,y)
axes[1][0].plot(x,y)
axes[1][1].plot(x,y)
# Layout with complex mosaic
with Figure(title=
"
My grid
"
,
layout = [[0,0,1],[2,
'
.
'
,1]],
gridspec_kw=dict(width_ratios=[1.4, 1, 1], height_ratios=[1, 2]),
) as axes:
axes[0].plot(x,y)
axes[1].plot(x,y)
axes[2].plot(x,y)
"""
buff
=
None
...
...
@@ -147,13 +165,41 @@ class Figure:
# Entering `with` statement
def
__enter__
(
self
):
self
.
fig
,
self
.
axes
=
plt
.
subplots
(
self
.
layout
[
0
],
self
.
layout
[
1
],
figsize
=
self
.
size
,
gridspec_kw
=
self
.
gridspec_kw
,
subplot_kw
=
dict
(
projection
=
self
.
projection
))
if
self
.
layout
[
0
]
==
1
and
self
.
layout
[
1
]
==
1
:
self
.
axesflat
=
[
self
.
axes
]
else
:
self
.
axesflat
=
self
.
axes
.
flatten
()
return_later
=
None
if
isinstance
(
self
.
layout
,
tuple
):
"""
Regular grids, like (2,4)
"""
self
.
fig
,
self
.
axes
=
plt
.
subplots
(
self
.
layout
[
0
],
self
.
layout
[
1
],
figsize
=
self
.
size
,
gridspec_kw
=
self
.
gridspec_kw
,
subplot_kw
=
dict
(
projection
=
self
.
projection
)
)
if
self
.
layout
[
0
]
==
1
and
self
.
layout
[
1
]
==
1
:
self
.
axesflat
=
[
self
.
axes
]
return_later
=
self
.
axes
else
:
self
.
axesflat
=
self
.
axes
.
flatten
()
return_later
=
self
.
axesflat
elif
isinstance
(
self
.
layout
,
list
):
"""
Complex mosaic, like [[0,0,1],[2,
'
.
'
,1]]
"""
self
.
fig
,
self
.
axes
=
plt
.
subplot_mosaic
(
self
.
layout
,
layout
=
"
constrained
"
,
gridspec_kw
=
self
.
gridspec_kw
,
figsize
=
self
.
size
,
subplot_kw
=
dict
(
projection
=
self
.
projection
)
)
# Convert labeled dict to list
self
.
axesflat
=
[
v
for
k
,
v
in
sorted
(
self
.
axes
.
items
(),
key
=
lambda
pair
:
pair
[
0
])]
return_later
=
self
.
axesflat
for
ax
in
self
.
axesflat
:
if
self
.
extent
:
ax
.
set_xlim
(
self
.
extent
[
0
],
self
.
extent
[
1
])
...
...
@@ -165,7 +211,7 @@ class Figure:
print
(
"
Figure instance returned. Usage: `with Figure(...) as F:`, `ax = F.ax`, `buff=F.buff`
"
)
return
(
self
)
else
:
return
(
self
.
axes
)
# makes possibe: with Fig() as ax: ax.change
return
(
return_later
)
# makes possibe: with Fig() as ax: ax.change
# Exiting `with` statement
def
__exit__
(
self
,
type
,
value
,
traceback
):
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment