PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, August 14, 2022

[FIXED] How to suppress unwanted Plot figure object information in Jupyter Notebook

 August 14, 2022     jupyter-notebook, output     No comments   

Issue

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);

Solution

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.))



Answered By - Wayne
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing