Reputation: 7852
I have 2 buttons and a plot, below each other. The code:
from IPython.display import display
import matplotlib.pyplot as plt
button1 = widgets.Button(description="Yes")
display(button1)
button2 = widgets.Button(description="No")
display(button2)
def logyes(b):
print("yes")
def logno(b):
print("no")
button1.on_click(logyes)
button2.on_click(logno)
plt.figure(figsize=(3,3))
plt.plot([1,2],[3,4])
The result in Jupyter Notebook:
I would like the plot to be placed next to the buttons, not below the buttons. I tried using this example, and changed the lines beginning with axprev
and axnext
to:
axprev = plt.axes([-0.2, 0.75, 0.2, 0.1])
axnext = plt.axes([-0.2, 0.65, 0.2, 0.1])
Getting the result:
This is the right orientation (plot next to buttons, not below buttons), but these buttons (Previous
, Next
) are not working in Jupyter Notebook, as they are not IPython widgets.
How can I create working buttons next to a plot in Jupyter Notebook?
Upvotes: 1
Views: 3726
Reputation: 5565
Matplotlib and ipywidgets seems to come up a lot on SO. A common approach is to use an Output
widget rather than plotting straight to stdout, and use the standard %matplotlib inline
magic. You can manipulate widget layout using the VBox
and HBox
container widgets.
I'm not clear what you want your buttons to do, but this is an example of how you can layout widgets in the desired arrangement,
%matplotlib inline
import ipywidgets as widgets
import matplotlib.pyplot as plt
button1 = widgets.Button(description="Yes")
button2 = widgets.Button(description="No")
out = widgets.Output()
buttons = widgets.VBox(children=[button1, button2])
all_widgets = widgets.HBox(children=[buttons, out])
display(all_widgets)
def logyes(b):
print("yes")
def logno(b):
print("no")
button1.on_click(logyes)
button2.on_click(logno)
with out:
plt.figure(figsize=(3,3))
plt.plot([1,2],[3,4])
plt.show()
Upvotes: 1