77
88
99@contextlib .contextmanager
10- def plotting_context (
11- ax = None , pause = True , title = "" , filename = "" , render = True , axis_index = None , ** kwargs
12- ):
10+ def plotting_context (ax = None , pause = True , title = "" , filename = "" , render = True , axis_index = None , ** kwargs ):
1311 """Executes code required to set up a matplotlib figure.
1412
1513 :param ax: Axes object on which to plot
@@ -49,9 +47,7 @@ def plotting_context(
4947 except (AttributeError , TypeError ):
5048 pass # only 1 axis, not an array
5149 except IndexError as exc :
52- raise ValueError (
53- f"axis_index={ axis_index } is not compatible with arguments to subplots: { kwargs } "
54- ) from exc
50+ raise ValueError (f"axis_index={ axis_index } is not compatible with arguments to subplots: { kwargs } " ) from exc
5551 else :
5652 fig = ax .get_figure ()
5753 ax_supplied = True
@@ -60,16 +56,16 @@ def plotting_context(
6056
6157 yield fig , ax
6258
59+ # if no axes was supplied, finish the plot and return the figure and axes
60+ plt .tight_layout ()
61+
6362 ax .set_aspect ("equal" , anchor = "C" )
6463 ax .set_title (title )
6564
6665 if ax_supplied :
6766 # if an axis was supplied, don't continue with displaying or configuring the plot
6867 return
6968
70- # if no axes was supplied, finish the plot and return the figure and axes
71- plt .tight_layout ()
72-
7369 if filename :
7470 fig .savefig (filename , dpi = fig .dpi )
7571 plt .close (fig ) # close the figure to free the memory
0 commit comments