@@ -16,8 +16,9 @@ def occupancy_histogram(
16
16
keypoint : int | str = 0 ,
17
17
individual : int | str = 0 ,
18
18
title : str | None = None ,
19
+ ax : plt .Axes | None = None ,
19
20
** kwargs : Any ,
20
- ) -> tuple [plt .Figure , dict [HistInfoKeys , np .ndarray ]]:
21
+ ) -> tuple [plt .Figure , plt . Axes , dict [HistInfoKeys , np .ndarray ]]:
21
22
"""Create a 2D histogram of the occupancy data given.
22
23
23
24
Time-points whose corresponding spatial coordinates have NaN values
@@ -35,13 +36,20 @@ def occupancy_histogram(
35
36
title : str, optional
36
37
Title to give to the plot. Default will be generated from the
37
38
``keypoint`` and ``individual``
39
+ ax : matplotlib.axes.Axes, optional
40
+ Axes object on which to draw the histogram. If not provided, a new
41
+ figure and axes are created and returned.
38
42
kwargs : Any
39
43
Keyword arguments passed to ``matplotlib.pyplot.hist2d``
40
44
41
45
Returns
42
46
-------
43
47
matplotlib.pyplot.Figure
44
- Plot handle containing the rendered 2D histogram.
48
+ Plot handle containing the rendered 2D histogram. If ``ax`` is
49
+ supplied, this will be the figure that ``ax`` belongs to.
50
+ matplotlib.axes.Axes
51
+ Axes on which the histogram was drawn. If ``ax`` was supplied,
52
+ the input will be directly modified and returned in this value.
45
53
dict[str, numpy.ndarray]
46
54
Information about the created histogram (see Notes).
47
55
@@ -99,7 +107,10 @@ def occupancy_histogram(
99
107
if key not in kwargs :
100
108
kwargs [key ] = value
101
109
# Now it should just be a case of creating the histogram
102
- fig , ax = plt .subplots ()
110
+ if ax is not None :
111
+ fig = ax .get_figure ()
112
+ else :
113
+ fig , ax = plt .subplots ()
103
114
counts , xedges , yedges , hist_image = ax .hist2d (
104
115
data .sel (space = x_coord ), data .sel (space = y_coord ), ** kwargs
105
116
)
@@ -114,4 +125,4 @@ def occupancy_histogram(
114
125
ax .set_xlabel (x_coord )
115
126
ax .set_ylabel (y_coord )
116
127
117
- return fig , {"counts" : counts , "xedges" : xedges , "yedges" : yedges }
128
+ return fig , ax , {"counts" : counts , "xedges" : xedges , "yedges" : yedges }
0 commit comments