Plotly interactive plot in jupyter notebook slideshow - python-3.x

I'm building a presentation using Jupyter Notebook in slideshow mode (and RISE).
Everything works fine till I add an interactive plot with Plotly. I only see the code, no plot.
Is it possible to have an interactive plot using Plotly in Slideshow?
Thank you!

Just add this line to the iplot cell:
plotly.offline.init_notebook_mode(connected=True)

Related

Is it possible to update inline plots in Python (Spyder)?

Setup: Anaconda 3 (Win10 64), Spyder 4 and Python 3.7. The IPython Graphics setting is default (Inline).I'm still a new to Python but I've looked around and have not found an answer that solves my problem so far. Thanks everyone in advance.
So in this setup, whenever I create a plot using matplotlib, it appears in the plot pane of Spyder. e.g.
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
df = pd.DataFrame(np.random.randint(0,100,size=(100, 1)), columns=list('A'))
bp = df.boxplot(column = 'A')
creates a boxplot. Now, if I want to add a title to the plot, the code would be
bp.set_title("This Title")
This is where I'm getting some problems. If I run the entire block together
df = pd.DataFrame(np.random.randint(0,100,size=(100, 1)), columns=list('A'))
bp = df.boxplot(column = 'A')
bp.set_title("This Title")
then I get a box plot with "This Title" as the title, showing up in the plot pane,
which is what I want.
However, if I run the above code line by line in the IPython console, the 2nd line will produce a boxplot as expected, but the 3rd line will not have an effect on the image in the plot pane, so the image in the plot pane still do not have a title
Now,if i go to Tools > Preference >IPython Console > Graphics and set the graphics backend to Automatic instead of the default Inline, then when I run the code in the Console line by line, I get an image that pops up in another window, and that it does update/refreshes based on new lines entered into the console. I understand that the inline plots are supposed to be static, but I thought I saw another post where someone said that it is possible to update inline plots? So now my questions are:
Do plots only update/refresh by line codes in the IPython console if the Graphics Backend is not static like inline?
Why do I get different result when I run code blocks vs line by line?
If it is possible to update the inline plots (preferably in the plot pane of Spyder), how do you do it? I've tried various methods to redraw the plots,for example
plt.show()
plt.draw()
bp.get_figure().canvas.draw()
but none of these updates the image in the plot pane. I figured that even if I can't update the image, I should at least be able to redraw it (i.e a 2nd image appears in the plot pane with the update characteristics). But nothing I've tried worked so far. Please advise and thanks again.
(Spyder maintainer here) About your questions:
Do plots only update/refresh by line codes in the IPython console if the Graphics Backend is not static like inline?
Correct.
Why do I get different result when I run code blocks vs line by line?
Because when you run code cells (which is what I think you mean by "code blocks") your plot is shown at the end of that code and hence it takes all modifications you've done to it in intermediate lines.
If it is possible to update the inline plots (preferably in the plot pane of Spyder), how do you do it?
No, it's not possible. As you correctly mentioned above, inline plots are static images, so they can't be modified.

How to download 3d plots with a specific camera angle in jupyter notebook?

I am using jupyter ipython notebook to generate 3d plots using matplotlib. I can get zoom and and pan options with the %matplotlib notebook command. I would like to download the plots with a specific camera angle that I choose with the pan/rotate option. Using savefig saves the default camera angle of the plot. How can I do this?
If you take the mouse to the up right-hand side of the plot, some options show up. If you press the first one (with the shape of a camera), it will save your plot with your desired angle to the download folder of your laptop.

Python - Spyder ignoring picker enabled plot

I am writing this script in Spyder (Python 3.5) and I want it to do this:
1) Plot something
2) Allow me to pick some values from the plot
3) Store those values into a variable
4) Do something with that variable
I have checked this thread: Store mouse click event coordinates with matplotlib and modified the function presented there for my own code. The problem I have is that spyder seems to ignore the interactive plot and runs the whole script at once, without waiting for me to pick any values from the plot. As I am using the values for further calculations, I obviously get an error from this. I have even tried to set an input('Press enter to continue...') after the plot, to see if it made it stop and wait for my pickings, but it does not work either.
When I run the script step by step, it works fine, I get the plot, pick my values, print the variable and find all of them in there and use them afterwards. So the question is: how can I make it work when I run the whole script?
Here is my code:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.pyplot import plot as plot
def onpick(event):
ymouse = event.ydata
print ('y of mouse: {:.2f}'.format(ymouse))
times.append(ymouse)
if len(times)==5:
f.canvas.mpl_disconnect(cid)
return times
#
t=np.arange(1000)
y=t**3
f=plt.figure(1)
ax=plt.gca()
ax.plot(t,y,picker=5)
times=[]
cid=f.canvas.mpl_connect('button_press_event',onpick)
plt.show()
#Now do something with times
mtimes=np.mean(times)
print(mtimes)
(Spyder maintainer here) I think to solve this problem you need to go to
Preferences > IPython console > Graphics
and turn off the option called Activate support. That will make your script to block the console when a plot is run, so you can capture the mouse clicks you need on it.
The only problem is you need to run
In [1]: %matplotlib qt5
before starting to run your code because Spyder doesn't that for you anymore.

Manipulate plots in jupyter notebook

I have an octave notebook and create a plot. That works fine, but I'd like to manipulate that plot by deleting and redrawing a line without redrawing the full plot:
delete(l3);
l3 = line(data(:,1), [ones(size(data,1),1) data(:,1)]*theta, 'color', 'red');
If I run this in an octave console this works fine, but when I try this in a notebook I get
error: delete: first argument must be a filename or graphics handle
So, is there a way to manipulate a plot in jupyter notebook? Or can I open the plot in an x11 window or something and then manipulate it?

PyQt Matplotlib colour control

Writing in Python 2.7 using pyQt 4.8.5:
How may I alter the background and graph-area (foreground?) of a Matplotlib widget? I would like to make the background of the graph widget 'light gray' (same as the background colour of the GUI), and I would like to make the graph-area (see below) black.
I'm new to GUI programming with pyQt and would like to achieve this:
my code:
self.ui.graph.axes.clear()
self.ui.graph.axes.hold(True)
self.ui.graph.axes.plot(self.Value,'r-')
self.ui.graph.axes.grid()
self.ui.graph.draw()
This should do it:
ax = self.ui.graph.axes
ax.set_axis_bgcolor('k')
self.ui.graph.set_facecolor('none')
This did only partly work for me. The two first lines worked fine but the last did not work. The error I got was:
AttributeError: 'MatplotlibWidget' object has no attribute 'set_facecolor'
The solution was to add figure to the code:
ax = self.ui.graph.axes
ax.set_axis_bgcolor('k')
self.ui.graph.figure.set_facecolor('none')
An interesting note is that I had been trying to solve this problem by using setPalette() but this didn't work until the facecolor was set to 'none' then suddenly all the changes I had made to the palette showed up.

Resources