Hello is there any way to make images (generated by matploblib) show off in jupyter notebook only after a plt.show()
request?
Tried to plt.ioff()
and didn’ t works also tried plt.ion()
. Tried %matplotlib
and %matploblib inline
.
I’m using RISE to build a slideshow and is annoying that all views share a portion of code in slide.
If there is a way to hide the source code in RISE is valid too.
1 Like
There is a discussion here about using an extension to hide the code in a way that is compatible with RISE.
Supposing that is overkill for what you need, some suggestions:
- You can make the images show up with markdown on the slide.
- You can save an image file with matplotlib’s savefig()
in a cell where
%%captureis the first line of the cell to suppress output of the plot generation from being displayed and then display the image of it when and where you want with
IPython.display` like at the bottom of here. Or use markdown to display the image.
- You can use the method here to not display and capture the plot for display later, as illustrated next.
Expanded example of that last suggestion:
cell ‘one’:
%%capture out
# of course you can show figures
def polynom(x):
return 2 * x**2 - 20 * x + 2
X = np.linspace(-10, 10)
Y = polynom(X)
plt.plot(X, Y);
In cell ‘two’:
out.show() #currently `out()` alone works also.
As a followup to that last method, because you objected to code showing, you might find the plot’s returned object value details, text like [<matplotlib.lines.Line2D at 0x7ff1855077f0>]
showing up to be unwanted as well. Trick to fix that so you just get only the plot from cell ‘one’ code:
In second cell:
# trick based on https://ipython.readthedocs.io/en/stable/api/generated/IPython.utils.capture.html#IPython.utils.capture.CapturedIO.outputs
# to get plot to print with out the object description / return value above
# like `out.show()` results in.
from IPython.display import display
display(out.outputs[1])