1

I want to suppress any text output when I run Jupyter Notebook cell. Specifically I output some figures and each is accompanied by something like:

<Figure size 432x288 with 0 Axes>

I have seen that if I put a ; at the end of a line, it should suppress the output, but it is not working in my case.

The code:

for i in tqdm_notebook(range(data.shape[0])):
    print('BIN:',i)
    fig = plt.figure(figsize=(15,4))
    plt.tight_layout()
    gs = gridspec.GridSpec(2,1)
    ax1 = fig.add_subplot(gs[0, 0])
    ax1.plot(match[window_begin:window_end],'k')
    plt.vlines(i,-np.max(match[window_begin:window_end])*0.05,np.max(match[window_begin:window_end])*1.05,'r',linewidth=4,alpha=0.2)
    ax1.set_xlim(0-1,post_bin_match_median[window_begin:window_end].shape[0])
    ax1.set_ylim(-np.max(match[window_begin:window_end])*0.05,np.max(match[window_begin:window_end])*1.05)
    plt.tick_params(axis='y', which='both', left=True, labelleft=False)
    ax1.tick_params(axis='x', which='both', bottom=False, labelbottom=False)
    plt.grid()

    ax2 = fig.add_subplot(gs[1, 0])
    fig.subplots_adjust(hspace=0.0)
    ax2.plot(gp_mjds[:],gp_data[i,:],'k')
    ax2.errorbar(remain, all[i,:], yerr=all_noise[i], fmt=".k", capsize=0);
    ax2.fill_between(gp[:], gp2[i,:] - np.sqrt(gp_var[i,:]), gp2[i,:] + np.sqrt(gp_var[i,:]),color="k", alpha=0.2)
    ax2.set_xlim(gp[0],gp[-1])
    plot_y_min = np.minimum(np.min(gp2[:,:] - np.sqrt(gp_var[:,:])),np.min(all_profile_residuals[:,:]-y_noise))
    plot_y_max = np.maximum(np.max(gp2[:,:] + np.sqrt(gp_var[:,:])), np.max(all[:,:]+y_noise))
    ax2.set_ylim(plot_y_min,plot_y_max)
    plt.grid()
    plt.show()
    plt.clf()
    plt.close(fig);
Wayne
  • 6,607
  • 8
  • 36
  • 93
user1551817
  • 6,693
  • 22
  • 72
  • 109

2 Answers2

2

The semi-colon would work if the typical output from the last line of the cell is what you are trying to suppress. As succinctly summarized by @kynan here, "The reason this works is because the notebook shows the return value of the last command. By adding ; the last command is "nothing" so there is no return value to show."

However, you have a loop inside a cell generating objects.
The culprit seems to be plt.clf(). Comment out that line or remove it from your code, and it should fix it.
Plus, I'd remove plt.show() as it isn't necessary when plt.clf() is removed, and I am seeing it being in the loop causing fig = plt.figure(figsize=(15,4)) to also show output text like you posted in your issue.
(I'll add for others looking at this later, that it is important have %matplotlib inline or %matplotlib notebook at the start of the cell (or at the start of a cell somewhere above this one.))

Wayne
  • 6,607
  • 8
  • 36
  • 93
  • Thank you. So is it not possible to suppress the `
    ' outputs?
    – user1551817 Jan 27 '20 at 18:35
  • 1
    So you have `%matplolib inline` or `matplotlib notebook` somewhere? It should be suppressible. However, you code isn't a case someone can debug directly. I am trying to make an equivalent toy example to test. – Wayne Jan 27 '20 at 18:40
  • 1
    My tests indicated `plt.clf()` is generating the spurious output. Are you able to remove it and get your plots still? – Wayne Jan 27 '20 at 19:15
  • For more general help suppressing output in a cell see, [this thread](https://stackoverflow.com/q/23692950/8508004). – Wayne Apr 15 '22 at 17:59
0

A complete guide on how to hide or remove content in Jupyter is available from the official documentation: https://jupyterbook.org/interactive/hiding.html#

For removing the single output line, you can tweak the command lines by adding a _ = [command ] assignment as suggested in this blog: https://www.tutorialguruji.com/python/suppress-output-in-matplotlib/.
The underscore there is a throwaway variable, actually an unidentified variable "when not in interactive mode". See the official Python documentation: https://docs.python.org/3.9/reference/lexical_analysis.html#reserved-classes-of-identifiers

XavierStuvw
  • 1,294
  • 2
  • 15
  • 30
  • Your first link, https://jupyterbook.org/interactive/hiding.html#, is to how to control content in Jupyter Book, which is not what the OP was asking about. However, I'm glad you brought this up since the title of this post is presently 'How to suppress unwanted output in Jupyter Notebook'. There's more general ways to suppress output from an entire cell in a Jupyter motebook, see [here](https://stackoverflow.com/a/23692951/8508004). I was waiting for the OP to replay if what was suggested helped in the specific case before suggesting that. I'm going to change title to be more specific. – Wayne Apr 15 '22 at 17:56