I was using matplotlib and found out that there is something as interactive plots in matplotlib via which we can rotate the graph via mouse cursor
%matplotlib qt is used to enable the interactive plots.Works fine in jupyter notebook but google colab shows error enter image description here
Is there any chance it works in google colab also?
No. There is no chance. %matplotlib qtworks only if you have notebook executed on same machine as you use it. Colab is executed on remote server. You may try %matplotlib notebook This create interactive plot inside notebook.
Related
I am trying to set up a jupyter lab on a linux machine and access it locally on my mac.
Following this post I was able to do exactly that. The notebook generally works well, except it cannot plot figures using any sort of interactive backend. If I use %matplotlib widget all figures in the notebook as just a blank plot. With %matplotlib inline the figures plot, but no ability to zoom or anything.
If I try to run
from matplotlib import use
use('TkAgg')
Which is required for one part of my notebook, I get:
ImportError: Cannot load backend 'TkAgg' which requires the 'tk' interactive framework, as 'headless' is currently running
I've ensured tkinter is installed, plotly is installed. I tried adding -X to the ssh command (the extent of my ssh knowledge).
I'm trying to set up my JupyterLab environment so that HoloViews loads automatically using the bokeh backend. I've put the following code in my start.py file in the .ipython/profile_default/startup/ directory:
import holoviews as hv
from holoviews import opts
hv.extension('bokeh')
opts.defaults(
opts.Curve(aspect='square'),
opts.Image(colorbar=True, toolbar='above', width=400, aspect='equal')
)
The startup script runs without errors and I can access HoloViews functions in my JupyterLab environment. However, when I try to plot anything (an xarray object for example) using hv.Image() or hv.Curve() I just get a blank output. If I then insert a cell in the JupyterLab notebook and execute hv.extension('bokeh') the plots start showing up when I rerun the plotting commands. They continue to work even if I restart the kernel.
This appears to be an issue specific to the bokeh backend, because if I change the backend to matplotlib (with appropriate modifications to the opts commands), all the plots show up from the start.
Anyone know what's going on here? It would be nice to be able to fully initialize HoloViews with bokeh from the JupyterLab start script.
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.
When I use Matplotlib to plot some graphs, it is usually fine for the default inline drawing. However, when I draw some 3D graphs, I'd like to have them in a separate window so that interactions like rotation can be enabled. Can I configure in Python code which figure to display inline and which one to display in a new window?
I know that in Spyder, click Tools, Preferences, Ipython Console, Graphics and under Graphics Backend select “automatic” instead of “inline”. However, this make all the figures to be in new windows. It can be messy when I have a lot of plots. So I want only those 3D plot to be in new windows, but all the other 2D plots remain inline. Is it possible at all?
Thanks!
type
%matplotlib qt
when you want graphs in a separate window and
%matplotlib inline
when you want an inline plot
Go to
Tools >> Preferences >> IPython console >> Graphics >> Backend:Inline, change "Inline" to "Automatic", click "OK"
Reset the kernel at the console, and the plot will appear in a separate window
Magic commands such as
%matplotlib qt
work in the iPython console and Notebook, but do not work within a script.
In that case, after importing:
from IPython import get_ipython
use:
get_ipython().run_line_magic('matplotlib', 'inline')
for inline plotting of the following code, and
get_ipython().run_line_magic('matplotlib', 'qt')
for plotting in an external window.
Edit: solution above does not always work, depending on your OS/Spyder version
Anaconda issue on GitHub. Setting the Graphics Backend to Automatic (as indicated in another answer: Tools >> Preferences >> IPython console >> Graphics --> Automatic) solves the problem for me.
Then, after a Console restart, one can switch between Inline and External plot windows using the get_ipython() command, without having to restart the console.
I have set the IPython console backend set to Automatic in the Spyder preferences.
In my scripts, I can now use switch_backend as either
plt.switch_backend('module://ipykernel.pylab.backend_inline') or plt.switch_backend('Qt5Agg') before each new plot, to make it either inline or separate/interactive.
(Tested with Spyder 4.2.2.)
If you want to look at just 1 or 2 charts you can also try manually undocking them in the plot window. So on the top right corner window
select the 'plots' tab
click the button with 3 horizontal bars
select 'undock'
It'll open the plot in a new window. When you close the window, it docks back.
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