I'm trying to create a simple interactive plot with sliders and matplotlib in Jupyter, however, the icons are simply showing a blank square instead? Figure below:
Blank icons using notebook backend
Am I missing a dependency? I believe I'm using default settings for everything.
A simplified version of my code is shown below:
%matplotlib notebook
import matplotlib.pyplot as plt
from ipywidgets import FloatSlider, interact
fig = plt.figure(figsize=(10,3))
ax = fig.add_subplot(1, 1, 1)
ax.set_xlabel('time [s]')
ax.set_ylabel('displacement [cm]')
x1, = ax.plot(t,x)
def update(P_l = FloatSlider(min=0,max=4000,step=50,value=1500)):
x1.set_ydata(x)
fig.canvas.draw()
interact(update)
Thanks in advance!
Jupyter Notebook tends to minimize heavy graphs and tables when it exceeds a specific size. Not sure about the accurate answer, But did you try double clicking the left edge of graph? It does the job in my windows PC.
Related
I am making some small tests in Jupyter. The user should see two plots, one on the left and one on the right. Than after clicking somewhere on the left plot something on the right plot should happen. For example here a click on the left plot will produce a red dot on the right plot in the same place:
%matplotlib notebook
def onclick(event):
ax.clear()
ax2.clear()
ax.set_xlim(0,10)
ax.set_ylim(0,10)
ax2.set_xlim(0,10)
ax2.set_ylim(0,10)
ax2.plot([event.xdata],[event.ydata],'o',color='red')
ax.figure.canvas.draw_idle()
ax2.figure.canvas.draw_idle()
fig = plt.figure(figsize=(20,10))
ax = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
ax.set_xlim(0,10)
ax.set_ylim(0,10)
ax2.set_xlim(0,10)
ax2.set_ylim(0,10)
cid = fig.canvas.mpl_connect('button_press_event',onclick)
This works, but the visualization in Jupyter is not the best. Is there a way to have this kind of interactive plots working outside of a Jupyter notebook ? Maybe in a window with varying dimensions ? As an example, how would one proceed with the example above ?
You can use plotly and export your plots to html file:
https://plotly.com/python/interactive-html-export/
I think I found a possible simple answer to my needs. Simply put the code in the question in a python script, remove the 'magic' command and insert at the end:
plt.show(block=True)
This way matplotlib will just use a different backend than notebook or inline in Jupiter and plot the figure in a separate window. This window is compatible with the interactive commands from matplotlib. I am not sure about widget compatibility though I will try and update the answer.
Update: I do not think ipywidgets can be used with plt.show() since these are not plotted in an axes, but there are some widgets within matplotlib (matplotlib.widgets) that can be used. Though, the resulting speed in my case is not satisfactory so I would avoid combining matplotlib widgets with matplotlib event catching.
I am using python to plot in VS code Jupiter on my Mac. The plot is shown in the Interactive View, while I cannot do more thing like zoom or save in the view. I refer to Python Interactive window(see Plot Viewer)and it says:
Within the Python Interactive window, double-click any plot to open it in the viewer, or select the expand button on the upper left corner of the plot.
Expand Button
Plot Viewer
The expand button is supposed to be shown and open the Plot Viewer, while I don't have the button and there is nothing happen after I double-click the plot.
No Button Shows
My test code:
import seaborn as sns
import numpy as np
x = np.random.randn(100)
ax = sns.distplot(x)
Could you please help with my issue? I wonder whether I miss any extension. Thank you very much!
If still does not work please check the version of the Python and Jupyter extensions.
Default the renderer used in Interactive Python windows uses the PNG renderer.
You can change it to use the TkAgg renderer (external window)
Add %matplotlib to the script
%matplotlib
import seaborn as sns
import numpy as np
x = np.random.randn(100)
ax = sns.distplot(x)
You can use other plot renderers
I am getting what seems to be a very annoying bug on Matplotlib when working on Jupyter Notebook. First consider this simple code:
import numpy as np
import matplotlib.pyplot as plt
%config InlineBackend.figure_format='retina'
plt.style.use('ggplot')
x = np.linspace(0, 1, 100)
y = np.cos(x)
z = np.sin(x)
Let's say I want to plots: (X, Y) and (X, Z) in the same figure. Apparently, the resolution at which the plot is displayed on Jupyter Notebook depends on whether I use two columns or two rows.
This plot is not "retina" at all. Now, If I try another configuration, I have the following:
This latter image is displayed with much more resolution (not sure if one can see that here on the website but I can clearly see on the screen of my laptop).
When I try to save the figure, no wonder both have the same resolution. This makes me wonder if there is something fishy in the connection between Matplotlib and the Jupyter notebook. Am I missing something? Can someone reproduce the error?
I could reproduce the error on a Google Colab notebook by the way.
I am on matplotlib version 3.2.2, with Jupyter notebook on version 6.4.6.
I am trying to plot multiple bars on the same plot on my jupyter notebook. However, due to some reason, it does not work on the notebook,but it works on a normal python editor. Here is the code that I have used.
import matplotlib.pyplot as plt
plt.bar(['A','B','C'],[72,73,88])
plt.bar(['A','B','C'],[98,77,98])
plt.show()
Any help will be highly appreciated.
(Edit)
I am looking for somthing like this on my jupyter notebook.
I presume you want to have both bar plots side by side. So here you go:
import matplotlib.pyplot as plt
fig,axs = plt.subplots(1,2)
axs[0].bar(['A','B','C'],[72,73,88])
axs[1].bar(['A','B','C'],[98,77,98])
plt.show()
Running the following code in a jupyter notebook (python3)
%matplotlib notebook
import matplotlib.pyplot as plt
f, (ax1, ax2) = plt.subplots(1, 2, figsize=(9,2.5))
The resulting figure is embedded in a window with huge borders that are wasting space. Up to now, I only found a function to set the window title of a figure "canvas.set_window_title". How can the window layout be changed? In particular, how can the window header be removed, the area where the window title and the "Stop Interaction" Button resides?
EDIT: I want to keep the interactive mode of matplotlib.
Screenshot of the output.
replace:
%matplotlib notebook
with
%matplotlib inline
Edit:
I see you want to keep interactive... I don't know if it is possible; inline is not interactive. You may have to settle for a floating window.