Search code examples
pythonpandasplotly

How can i make this time series graph interactive?


I am new to Python and Pandas so any help is much appreciated.

I am trying to make the graph below interactive, it would also be good to be able to choose which attributes show rather than them all.

Here is what I have so far

df.set_index('Current Year').plot(rot=45)
plt.xlabel("Year",size=16)
plt.ylabel("",size=16)
plt.title("Current year time series plot", size=18)

I know that i need to import the following import plotly.graph_objects as go but no idea how to implement this with the above time series graph. Thanks

EDIT

I am getting this error when trying to enter my plotted data.

Error message


Solution

  • All you need is:

    df.plot()
    

    As long as you import the correct libraries and set plotly as the plotting backend for pandas like this:

    import pandas as pd
    pd.options.plotting.backend = "plotly"
    
    df = pd.DataFrame({'year':['2020','2021','2022'], 'value':[1,3,2]}).set_index('year')
    fig = df.plot(title = "Current year time series plot")
    fig.show()
    

    Plot:

    enter image description here

    Complete code:

    import pandas as pd
    pd.options.plotting.backend = "plotly"
    
    df = pd.DataFrame({'year':['2020','2021','2022'], 'value':[1,3,2]}).set_index('year')
    fig = df.plot(title = "Current year time series plot")
    fig.show()