Can %matplotlib notebook only be bused in Jupyter? - python

I have this and it works in jupyter in browser:
%matplotlib notebook
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import scipy as sp
However, after I copied it to a python file in the PyDev IDE editor, it always complains the first line:
%matplotlib notebook
Error message:
Encountered "%" at line 1, column 1. Was expecting one of: <EOF> <NEWLINE> ... "(" ... "{" ...
Does this mean the magic method can only be used in jupyter notebook?

Yes, It's a jupyter notebook command.

Well, you can't use it in a file (as that's not valid Python syntax), but you can use it in the PyDev interactive console: http://www.pydev.org/manual_adv_interactive_console.html if you have IPython properly installed.

The command in question, %matplotlib notebook, can be used to specify how to display graphs in a Jupyter notebook. If you use %matplotlib inline, the graphs will be displayed as a part of the Jupyter notebook. Using the form of the command that you asked about will display the graph in an interactive form within the Jupyter notebook. Or using %matplotlib gtk will open another program to display each individual graph, which is how Python would display the graphs in any other situation. So the command does not have any use or meaning outside of a Jupyter notebook.

Related

tqdm fails when detecting Jupyter notebook

I'm using tqdm to display a progress bar for my code. Sometimes I use the code in the terminal and sometimes I use the code in a Jupyter Notebook.
Unless I'm mistaken (probably am or I wouldn't be writing this question), tqdm is supposed to automatically determine if I'm in the Jupyter Notebook or in the terminal by using this code:
from tqdm.autonotebook import tqdm
def extend(index):
# some code here
# X is a pandas DataFrame defined elsewhere
with mp.Pool(N) as pool:
results = list(
tqdm(pool.imap(extend, X.index), total=len(X)))
The progress bar is shown nicely in the terminal like this:
100%|█████████████████████████████████████████| 15035/15035 [35:02<00:00, 7.15it/s]
But when the same code is called in a Jupyter Notebook, this is displayed instead:
HBox(children=(HTML(value=''), FloatProgress(value=0.0, max=15035.0), HTML(value='')))
Am I doing something wrong? What do I need to do to get tqdm to display nicely in Jupyter Notebook and in the terminal?
If you are editing the notebook in the JupyterLab, you should install jupyterlab_widgets.

How to export an jupyter interactive output within vsc as pdf

I am using visual studio code and I am using the python extension to run jupyter notebooks. A "python interactive" window is opened within vsc and displays the jupyter Notebook. I am wondering how do easily export this as pdf?
It is possible to export an ipython notebook, from there I can create an pdf … but these very unhandy ….
Code:
%%
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
%%
x = np.linspace(0,20, 100)
plt.plot(x, np.sin(x))
#plt.show()
%%
Screenshot:
You could log a suggestion on our github issues list:
https://github.com/Microsoft/vscode-python/issues/new.
Otherwise we don't currently support exporting to PDF. Just ipynb.

Cannot get matplotlib graphics to show up inline

I would like to load automatically the command lines with Jupyter in Ubuntu 16.10 :
%matplotlib inline
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
With Ubuntu, we could normally use configuration files as .vimrc, .bashrc and so on to automate some command lines. I know we could create the config file jupyter_notebook_config.py with jupyter notebook --generate-config. How could we implement that in python in that .py file?
Thanks!
You can't have graphics inline in a regular terminal because regular terminals don't have graphics capabilities. However, if you would be willing to use an alternative console, Jupyter Qtconsole, may be suitable for you.
EDIT: okay, I've had a look and you need to look at this, specifically the parts about running files/lines of code at startup. (like c.InteractiveShellApp.exec_lines or c.InteractiveShellApp.exec_files

Jupyter shows plot without plt.show()

I am using the Jupyter notebook with Python 2.7. Importing matplotlib like this:
%matplotlib inline
import matplotlib.pyplot as plt
But I have observed one thing. When I use Python in Spyder I always have to use the plt.show() command at the end of the python script in order to see the plots.
In Jupyter I do not need this command in order to see a plot. I do get this error message:
[<matplotlib.lines.Line2D at 0x91615d0>]
but it still makes a plot. Why is that?
You turn on the immediate display with %matplotlib inline.
The line:
[<matplotlib.lines.Line2D at 0x91615d0>]
is no error message. It is the return value of the last command. Try adding a ; at the end of the last line to suppress this.
The requirement of adding %matplotlin inline is no longer needed in the latest jupyter notebooks. It's a by default addition now.
You can change settings in ipython_kernel_config.py for different behaviour

Plot inside Ipython Notebook

I'm using IPython notebooks to share code and (hopefully) graphics with m collaborators. sadly, I cannot get matplotlib to plot inside the notebook, it always gives me a pop-up window, which I obviously cannot include in notebook pastes or similar.
Here's two minimal examples of how I go about plotting things.
Either with invoking plt.show() , or without it.
this either gives me an external pop-up, or plots nothing.
You need to be using the matplotlib inline backend.
%matplotlib inline

Categories

Resources