Functions that plot should take an `ax` argument
See original GitHub issue🚀 Feature
Currently, functions like trainer.tuner.lr_find().plot()
will generate a new Matplotlib figure and axes. I believe best practice is for such functions to accept an ax
so that the user may pass in their own ax.
Motivation
- Consistency with other packages. E.g. Pandas plot, Statsmodels plot functions, etc.
- This allows a user to have a figure with multiple axes, and fill one of those axes with the output of, say,
lr_find().plot()
(not super compelling in the case of LR finder) - I use a Matplotlib wrapper that adds functionality, like reusing the same window over multiple runs, scroll-to-zoom, and tooltips-on-hover. But this doesn’t work if Lightning creates its own
figure
andaxes
each time.
Pitch
Just add ax
as an optional arg to any function that plots, which I believe is just the LR finder ATM.
Since the function returns a figure, you can pluck that from the ax if provided, so it wouldn’t be a breaking change. Something like:
if ax is None:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
else:
fig, ax = ax, ax.figure
Alternatives
Can’t think of any.
Additional context
There’s a page somewhere in the Matplotlib docs that describes general best practices for writing functions that produce charts but I can’t find it!
Issue Analytics
- State:
- Created a year ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
Creating custom plotting functions with matplotlib
Essentially, ax will be taking the axes object onto which you want to plot. This can be a subplot axes or a simple...
Read more >How should I pass a matplotlib object through a function
Set the figure size and adjust the padding between and around the subplots. In plot() method, plot x and y data points at...
Read more >matplotlib.axes.Axes.plot — Matplotlib 3.6.2 documentation
You can use Line2D properties as keyword arguments for more control on the appearance. ... If both x and y are 2D, they...
Read more >matplotlib - 2D and 3D plotting in Python
fig, axes = plt.subplots(nrows=1, ncols=2) for ax in axes: ax.plot(x, y, ... The legend function takes an optional keyword argument loc that can...
Read more >Advanced plotting — Python4Astronomers 2.0 documentation
This method is more convenient for advanced plots, and we will be adopting ... where the figsize argument takes a tuple of two...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Yes @bipinKrishnan, feel free to open a PR and we will be very happy to review it!
Oh, I just realised my initial snippet got the wrong order, good catch @bipinKrishnan . Actually it can just be:
And in case you didn’t know, this parameter can be typed as
ax: Axes
usingfrom matplotlib.axes import Axes