Search code examples
pythonpandasplotly

How to plot a variable dataframe


I have a dataframe with a variable number of stock prices. In other words, I have to be able to plot the entire Dataframe, because I may encounter 1 to 10 stocks prices. The x axis are dates, the Y axis are Stock prices. Here is a sample of my Df:

 df = pd.DataFrame(all_Assets)
 df2 = df.transpose()
 print(df2)
                                Close       Close       Close
Date                                                         
2018-12-12 00:00:00-05:00   40.802803   24.440001  104.500526
2018-12-13 00:00:00-05:00   41.249191   25.119333  104.854965
2018-12-14 00:00:00-05:00   39.929325   24.380667  101.578560
2018-12-17 00:00:00-05:00   39.557732   23.228001   98.570381
2018-12-18 00:00:00-05:00   40.071678   22.468666   99.605057

This is not working

fig = go.Figure(data=go.Scatter(df2, mode='lines'),)

I need to plot this entire dataframe on a single chart, with 3 different lines. But the code has to adapt automatically if there is a fourth stock, fifth stock e.g. By the way , I want it to be a Logarithmic plot.


Solution

  • There is a sample in the reference, so let's try to graph it in wide and long format with express and in wide and long format with the graph object. You can choose from these four types to do what you need.

    • express wide format
    df.head()
    date    GOOG    AAPL    AMZN    FB  NFLX    MSFT
    0   2018-01-01  1.000000    1.000000    1.000000    1.000000    1.000000    1.000000
    1   2018-01-08  1.018172    1.011943    1.061881    0.959968    1.053526    1.015988
    2   2018-01-15  1.032008    1.019771    1.053240    0.970243    1.049860    1.020524
    3   2018-01-22  1.066783    0.980057    1.140676    1.016858    1.307681    1.066561
    4   2018-01-29  1.008773    0.917143    1.163374    1.018357    1.273537    1.040708
    
    import plotly.express as px
    
    df = px.data.stocks()
    fig = px.line(df, x='date', y=df.columns[1:])
    fig.show()
    

    enter image description here

    • express long format
    df_long = df.melt(id_vars='date', value_vars=df.columns[1:],var_name='ticker')
    px.line(df_long, x='date', y='value', color='ticker')
    
    • graph_objects wide format
    import plotly.graph_objects as go
    
    fig = go.Figure()
    
    for ticker in df.columns[1:]:
        fig.add_trace(go.Scatter(x=df['date'], y=df[ticker], name=ticker))
    
    fig.show()
    
    • graph_objects long format
    fig = go.Figure()
    
    for ticker in df_long.ticker.unique():
        dff = df_long.query('ticker == @ticker')
        fig.add_trace(go.Scatter(x=dff['date'], y=dff['value'], name=ticker))
    
    fig.show()