简体   繁体   中英

Attach matplotlib figure.Figure to pyplot

I have a function that returns a matplotlib.figure.Figure object given some parameters and I want to add it to the pyplot object (so that pyplot.show() shows the image).

Is there any way to do this?

Thanks!

Background:

My question may seem counterintuitive, this is why I need to do so. I'm developing a Python package that uses matplotlib to generate plots, so I'm using the OO matplotlib interface.

I'm writing tests (using matplotlib.testing , which lets me compare images with a decorator) to be sure that the functions return the correct plots. The problem is that such package uses the MATLAB-like interface and looks for plots in the pyplot object (using plt.get_fignums() ) and then compares with images in files.

I could modify the matplotlib.testing code to suit my needs but using the code as is seems a better option, even though attaching a figure to pyplot seems hacky.

I ended up using another approach to solve my testing problem. Instead of using the matplotlib.testing built-in decorator, I'm using the function that compares the images:

from matplotlib.testing.compare import compare_images

compare_images(expected='expected.png', actual='actual.png', tol=13, in_decorator=True)

That returns None if the test was successful or a Dictionary with some data in case the images differed more than the threshold. I'm going to build a wrapper around that function to make it more testing friendly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM