[Python] Dropdown widget Python

Discussão em 'Python' iniciado por Stack, Julho 12, 2025.

  1. Stack

    Stack Membro Participativo

    Please bear with me. First post for me here. I am using Python, first programming language for me, for about two weeks. Now I am stuck..

    I want to create a single graph with a dropdown widget. On the x-axis I want the selected year and on the y-axis I want the respective number for that year. So the year 2009 on the x-axis and 130 on the y-axis and so on.

    I tried different things, basically playing around for three hours..

    Maybe someone can provide me a solution? Thanks!

    %matplotlib inline

    import ipywidgets as widgets
    from IPython.display import display
    import seaborn as sns
    import matplotlib.pyplot as plt
    sns.set_style("darkgrid")

    dropdown = widgets.Dropdown(
    options=['2009', '2010', '2011', "2012", "2013", "2014", "2015", "2016", "2017", "2018","2019"],
    value='2009',
    description='Jahr:',
    )

    def Dropdown_Menu(b):

    fig, ax = plt.subplots()
    fig.dpi = 500

    x = [2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019]
    y = [130, 137, 104, 147, 401, 274, 234, 770, 857, 746, 704]

    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()

    dropdown.observe(Dropdown_Menu, names="value")
    display(dropdown)

    Continue reading...

Compartilhe esta Página