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.

Implement support of Matplotlib figures

See original GitHub issue

Hello @lanpa,

First of all, thanks a lot for investing the time and developing this project. I find it super useful whenever I try (and usually fail 😉 hacking PyTorch.

Frequently, when I work there’s a need to create Matplotlib plot and add it to TensorBoard. There are known and well documented ways to do it by converting the figure to image. One of your examples in this repo actually demonstrates it and there’s a few other ways to achieve the same result.

I was wondering if it would make sense to implement SummaryWriter.add_figure(...) method that would accept matplotlib.Figure, convert it into a RGB image and then add it to TensorBoard. I think the implementation could look roughly like this:

import matplotlib.pyplot as plt
import matplotlib.backends.backend_agg as plt_backend_agg

class SummaryWriter:
    def add_figure(self, tag, figure, global_step=None, close_figure=True):
        canvas = plt_backend_agg.FigureCanvasAgg(figure)
        canvas.draw()
        data = np.frombuffer(canvas.buffer_rgba(), dtype=np.uint8)
        w, h = figure.canvas.get_width_height()
        image = data.reshape([h, w, 4])[:, :, 0:3]
        if close_figure:
            plt.close(figure)
        self.add_image(tag, image, global_step)

I’ll submit a PR if you think adding the method makes sense and it fits well into the API.

Have a good one!

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
lanpacommented, Jun 2, 2018

thanks!

1reaction
vojtamoldacommented, Jun 1, 2018

Agreed. Thanks. PR coming in a little bit.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Interactive figures and asynchronous programming - Matplotlib
Matplotlib supports rich interactive figures by embedding figures into a GUI window. The basic interactions of panning and zooming in an Axes to...
Read more >
How To Display A Plot In Python using Matplotlib - ActiveState
Pythonistas typically use the Matplotlib plotting library to display numeric data in plots, graphs and charts in Python.
Read more >
Python Plotting With Matplotlib (Guide)
Free Bonus: Click here to download 5 Python + Matplotlib examples with full source code that you can use as a basis for...
Read more >
Matplotlib plots in PyQt5, embedding charts in ... - Python GUIs
Many other Python libraries — such as seaborn and pandas— make use of the Matplotlib backend for plotting. These plots can be embedded...
Read more >
Visualization with Matplotlib | Python Data Science Handbook
Matplotlib supports dozens of backends and output types, which means you can ... use, but just how you view your Matplotlib plots depends...
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