0

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

enter image description here

15
  • @Wayne maybe next time use @furas instead of furas in comment and Stackoveflow should inform me that you mentioned me :) Commented Jul 8 at 17:21
  • you could show code which you exactly run. Maybe it needs small modification. Commented Jul 8 at 17:25
  • @furas, I didn't think it really rated notifying you yet as no specific code was included and your code worked in a good environment. I was trying to learn more and put things on on solid footing for now. Commented Jul 8 at 18:56
  • 1
    @Wayne it is nice get any information when people use my old answer - which I don't even remeber :) Commented Jul 8 at 19:17
  • @wayne, thank you for your guidance. In the notebook created by clicking on the 'launch binder', when I run the code with ipympl, I get this error message "RuntimeError: 'widget is not a recognised GUI loop or backend name". This same code on my computer generates me the figure and the box with "Jahr: 2009". BUT : on my computer when I click on the arrow to open the dropdown menu, nothing happens. My only way to select another value is with the up and down arrows of my keyboard. Commented Jul 8 at 19:29

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.