Forum Archive

OO style matplotlib?

bennettbrowniowa

First, WOW! I love Pythonista already! Thank you to creators and contributors!

I am using matplotlib and prefer to use the OO style of coding as described here. In other Python environments, I have no difficulty doing this, but in Pythonista the script

import matplotlib.pyplot as plt
fig, ax = plt.subplots(1, 1)
ax.plot(1, 1, 'ro')
fig.show()

produces the error "UserWarning: matplotlib is currently using a non-GUI backend, so cannot show the figure."

I tried adding plt.ioff() to the script, since Pythonista documentation does warn that matplotlib GUIs will not implement interactive behavior, but it still does not produce the plot. I am able to produce the expected plot using plt.plot(1, 1, 'ro'). The Pythonista mirror of matplotlib's documentation seems to suggest I can use the OO style to code with matplotlib.

Is it possible to use the OO interface to matplotlib with Pythonista? If so, how?

omz

Thanks for the feedback. You can work around this limitation by saving the plot to an image first, and showing that (the non-OO show() does pretty much the same internally):

import matplotlib.pyplot as plt
import Image

fig, ax = plt.subplots(1, 1)
ax.plot(1, 1, 'ro')

fig.savefig('mpl_out.png')
Image.open('mpl_out.png').show()