Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functions that plot should take an ax argument #15416

Closed
davidgilbertson opened this issue Oct 30, 2022 · 5 comments · Fixed by #15652
Closed

Functions that plot should take an ax argument #15416

davidgilbertson opened this issue Oct 30, 2022 · 5 comments · Fixed by #15652
Assignees
Labels
feature Is an improvement or enhancement good first issue Good for newcomers tuner

Comments

@davidgilbertson
Copy link
Contributor

davidgilbertson commented Oct 30, 2022

🚀 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

  1. Consistency with other packages. E.g. Pandas plot, Statsmodels plot functions, etc.
  2. 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)
  3. 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 and axes 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!

cc @Borda @akihironitta @rohitgr7

@davidgilbertson davidgilbertson added the needs triage Waiting to be triaged by maintainers label Oct 30, 2022
@awaelchli awaelchli added tuner feature Is an improvement or enhancement and removed needs triage Waiting to be triaged by maintainers labels Oct 30, 2022
@awaelchli
Copy link
Contributor

@rohitgr7 What do you think, should we add this? @davidgilbertson are you interested in contributing this?

@davidgilbertson
Copy link
Contributor Author

I've got a full schedule at the moment, sorry.

Actually after I logged this I remembered that I've monkey-patched pyplot.subplots so that even if a package doesn't accept an axes object, it will use the one I want anyway. So for me there's no need, but I still think it's a good practice in general.

@awaelchli awaelchli added the good first issue Good for newcomers label Oct 31, 2022
@bipinKrishnan
Copy link
Contributor

bipinKrishnan commented Nov 9, 2022

@awaelchli I am new to the lightning codebase, can I work on this issue? Just to confirm, the solution would be to change this line:
https://github.com/Lightning-AI/lightning/blob/d5003b1c07fda783f651a732c86ad48656be42c1/src/pytorch_lightning/tuner/lr_finder.py#L150

to this(as suggested by @davidgilbertson):

if ax is None:
    fig, ax = plt.subplots()
else:
    fig, ax = ax.figure, ax

@davidgilbertson
Copy link
Contributor Author

Oh, I just realised my initial snippet got the wrong order, good catch @bipinKrishnan . Actually it can just be:

if ax is None:
    fig, ax = plt.subplots()
else:
    fig = ax.figure

And in case you didn't know, this parameter can be typed as ax: Axes using from matplotlib.axes import Axes

@awaelchli
Copy link
Contributor

Yes @bipinKrishnan, feel free to open a PR and we will be very happy to review it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Is an improvement or enhancement good first issue Good for newcomers tuner
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants