I wanted to test the code from this previous post because I have a trouble to visualize my dropdown menu.
I use pycharm, and add the code found in the answer of the post above in a Jupyter notebook. As you can see in the printscreen I have the dropbox with the first value displayed. I also have my plot that updates with the option I choose. The problem is that when I click on the option, I only know it is selected thanks to the blue rectangle that appears, then with the arrows of my keyboard I can select the value I want.
Question : I want to see displayed some options like a regular dropdown list. How can I do that?
Thank you
Edit: here is the code I used from the previous post
%matplotlib widget
# needs ipympl
import ipywidgets as widgets
from IPython.display import display
import matplotlib.pyplot as plt
dropdown = widgets.Dropdown(
#options=['2009', '2010', '2011', '2012', '2013', '2014', '2015', '2016', '2017', '2018', '2019'],
#value='2009',
options=list(range(2009, 2020)), # integers instead of strings
value=2009, # integer instead of string
description='Jahr:',
)
fig, ax = plt.subplots()
fig.dpi = 150
x = [2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019]
y = [130, 137, 104, 147, 401, 274, 234, 770, 857, 746, 704]
line, = ax.plot(x, y, label="Flugstunden pro Jahr", marker=".")
ax.legend()
ax.set_title("Flugstunden")
ax.set_xlabel("Jahr")
ax.set_ylabel("Flugstunden")
ax.set_facecolor((0.9,0.9,0.9))
#plt.show()
def on_change1(value=2009):
"""remove old line(s) and plot new line(s)"""
#print(type(value))
#value = int(value) # I don't have to convert string to integer
# get all value for `year >= value`
#pairs = [(a,b) for a,b in zip(x, y) if a >= value] # use `==` to get only one value
#selected_x, selected_y = zip(*pairs)
# select data
pos = x.index(value)
selected_x = x[pos] # create `selected_x` to keep original values in `x`
selected_y = y[pos] # create `selected_y` to keep original values in `y`
print('x:', selected_x)
print('y:', selected_y)
# remove old line(s)
for l in ax.lines:
l.remove()
# plot new line(s)
ax.plot(selected_x, selected_y, label="Flugstunden pro Jahr", marker=".")
def on_change2(value=2009):
"""keep line, remove all data from line and use new data with the same line"""
#print(type(value))
#value = int(value) # I don't have to convert string to integer
# get all value for `year >= value`
#pairs = [(a,b) for a,b in zip(x, y) if a >= value] # use `==` to get only one value
#selected_x, selected_y = zip(*pairs)
# select data
pos = x.index(value)
selected_x = x[pos] # create `selected_x` to keep original values in `x`
selected_y = y[pos] # create `selected_y` to keep original values in `y`
print('x:', selected_x)
print('y:', selected_y)
line.set_xdata(selected_x)
line.set_ydata(selected_y)
#fig.canvas.draw()
widgets.interact(on_change1, value=dropdown)
#widgets.interact(on_change2, value=dropdown)
Edit 2: The last code with iypmpl works on the 'launcher binder'. On my computer, when I open the notebook in jupyter lab, the code works well. Then, when I open the same notebook in PyCharm, the code generates the figure, but the dropdown menu does not open. So it must be the IDE.
Update: I just decided to update the IDE (last time I did, it was 6 months ago). Now it works like a charm...My new Pycharm version is 2025.1.3

furasin comment and Stackoveflow should inform me that you mentioned me :)