0

I want to publish an HTML (preferably) or PDF from a Jupyter notebook, however when I export from Visual Studio Code it also outputs some annoying text that I do not want for certain cells, alongside the expected output. See for example:

Input:

fig, axs = plt.subplots(2,5, figsize=(20,10))

axs[0,0].hist(pay_data_trim['amount'], bins=15, color = 'b')
axs[0,0].set_ylabel('Frequency', fontsize=14)
axs[0,0].set_title('amount', fontsize=14)
axs[1,0].boxplot(pay_data_trim['amount'])
axs[1,0].set_ylabel('IQR + Outliers', fontsize=14)

axs[0,1].hist(pay_data_trim['oldbalanceOrg'], bins=15, color = 'g')
axs[0,1].set_title('oldbalanceOrg', fontsize=14)
axs[1,1].boxplot(pay_data_trim['oldbalanceOrg'])

(by the way I haven't finished this chunk, so the figure is incomplete)

Output:

{'whiskers': [<matplotlib.lines.Line2D at 0x7ff133493760>,
  <matplotlib.lines.Line2D at 0x7ff133493a30>],
 'caps': [<matplotlib.lines.Line2D at 0x7ff133493d00>,
  <matplotlib.lines.Line2D at 0x7ff133493fd0>],
 'boxes': [<matplotlib.lines.Line2D at 0x7ff133493370>],
 'medians': [<matplotlib.lines.Line2D at 0x7ff1334bc2e0>],
 'fliers': [<matplotlib.lines.Line2D at 0x7ff1334bc5b0>],
 'means': []}

As well as the figure (so no problems with that). However I don't want to publish the meaningless output text. Does anyone know how to mute it in Visual Studio Code? Alternatively, if you can recommend a software/interface/anything that I can use to publish this without the unwanted text, I'm happy to take recommendations.

Thanks for your help!

  • 1
    What happens when you place `plt.show()` after all your plotting code? – Michael S. Aug 26 '22 at 03:11
  • The 'Output' example you show *usually* comes from something on the last line being returned that has to do with the plots. (I qualify it with *usually* because just yesterday I was encountering a case where invoking another plot figure when working in another already was causing something similar, too.) If that is the case, usually you can just add a semicolon as the last character as the last line. The reason for this is the last line is special in a Jupyter notebook. It's why you can type the name of a previously assigned variable and the value of it will be shown without the need for ... – Wayne Aug 26 '22 at 17:15
  • a `print` statement. For a more technical explanation, see [kynan's comment here](https://stackoverflow.com/questions/14506583/suppress-output-of-object-when-plotting-in-ipython/31792131#comment54913617_31792131). So usually if you just add a semicolon at the end of all places you see that type of output, you can eliminate it at the source. There are other ways to suppress output from an entire cell and even suppress just select output from a running cell with some code, see April 15 comment, presently at the bottom, [here](https://stackoverflow.com/a/59936885/8508004). ... – Wayne Aug 26 '22 at 17:21
  • I link to that answer and not the comment directly because the answer deals with plots and some of that knowledge about handling plots may help you since in your post it is clear that you are working with plots as well. – Wayne Aug 26 '22 at 17:25
  • 1
    Thanks @Wayne for that, I'll keep it in mind. Also, thanks @MichaelS.!! Adding that solved the issue to be honest, even though the plot was still showing in the output so I thought it was not strictly necessary when working in VS Code. I'll keep this in mind. Most appreciated! – Sebastian Quezada Aug 27 '22 at 09:15
  • Yes, pure modern Jupyter usually no long requires the `plt.show()` to display the plot (I usually don't use it in conjunction with VSCode, yet it seems similar); however, explicit use of that in conjunction with some of the other suggestions I made can help you clear up some of the behind-the-scenes cruft that can sometimes cause your output to be a little messier than desired. At least that is the present situation. – Wayne Aug 27 '22 at 14:31

2 Answers2

0

Open the jupyter file with Chrome and convert it to PDF by printing.

Export your Jupyter Notebook

Note: For PDF export, you must have TeX installed. If you don't, you will be notified that you need to install it when you select the PDF option. Also, be aware that if you have SVG-only output in your Notebook, they will not be displayed in the PDF. To have SVG graphics in a PDF, either ensure that your output includes a non-SVG image format or else you can first export to HTML and then save as PDF using your browser.

JialeDu
  • 6,021
  • 2
  • 5
  • 24
0

Adding plt.show() at the end of the statement solved the problem, as suggested by @Michael S.

Additionally, comments from @Wayne and @JialeDu were very helpful.