question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

  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

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
awaelchlicommented, Nov 9, 2022

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

1reaction
davidgilbertsoncommented, Nov 9, 2022

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found