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.
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'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.
I've made a simple GUI game with matplotlib, I can get it running well on PyCharm if I check "Run with Python Console" in config options.
The problem comes when I try to run it from terminal by doing
python MyGame.py
Indeed, when doing this, the matplotlib window only displays the plots, but not the widget buttons, and the latters only appear in a second window, when I close the first one...
Is there a way to get all the plots and the buttons in the same window, as it does in PyCharm with my config?
Here is an example of code having the unexpected behaviour
import matplotlib.pyplot as plt
import matplotlib.widgets as widgets
ax = plt.subplot()
ax.plot(.5,.6, marker='o', markersize=15, color='k')
plt.show()
buttonShape = plt.axes([.4, .2, .2, .1])
button = widgets.Button(buttonShape, "I am a button")
plt.show()
Thanks,
Vic
You got two plt.show() in your code.
The first one shows the figure you have constructed so far. Then you close it with the mouse. And then you start constructing a new one.
Just remove the first plt.show()
I am new to the world of mac. In python we I want to visualize a data, I use matplotlib's pyplot to generate a plot but when I do pyplot.show() it creates a new window. This behavior also happens inside my ipython notebook too - see image below. I wanted it to embed the image inside the notebook.
How can I correct this ?
Try ipython notebook --pylab inline when you launch ipython.
Input in a cell, the cell magic %pylab inline or %matplotlib inline will make the plots appear inline/interactive (instead of a new window).
Not that if you use %matplotlib cell magic, you still need import matplotlib.pyplot as plt and import numpy as np etc. With %pylab cell magic you don't.