Rendering a matplotlib figure in a client

First, would like to express that Anvil seems brilliant as I’ve been experimenting with the Individual plan. I’d like to be able to use plotting functionality that exists in the qiskit.org quantum computing library. For each type of plot, the value returned by default is a matplotlib figure. Optionally, and Axes object can be passed in to which the plot is rendered. Can the Anvil example I found be modified to accommodate either using a figure or Axes as input?
https://anvil.works/blog/plotting-in-matplotlib

Thanks,
Jim

Hello and welcome,

Just in case it is helpful, have you checked out the blog post about plotting in Anvil?

It includes a section about matplotlib including clone-able examples.

If you are getting a matplotlib object from somewhere else and bringing that into your app, I would assume that Anvil would be able handle it as it would normally handle that type of object.

Perhaps that blog post can help you to make progress. There is also more information about matplotlib on the forum and in the documentation.

I hope this is helpful in some way.

1 Like

Thanks Allan. Following the guidance in that post, the thing I’m trying to do works now. It’s pretty amazing, and certainly unexpected to me, that the following would just work:

  fig = plot_histogram(counts).savefig(os.devnull)
  return anvil.mpl_util.plot_image()

Thanks again,
Jim

1 Like

No problem. Yeah, sometimes it seem like certain things would be complicated, but in a lot of ways Anvil allows you to use whichever libraries you like and they work exactly as they would in a local Python environment, especially when you are on the server.