Search code examples
pythonpandasdataframeplotplotly

How to make a plot with data? (Plotly preferably)


I have a dataset and a piece of it looks like that:

Metrics 01.08.21 01.09.21 01.10.21 01.11.21 01.12.21 01.13.21 01.14.21
0 Events NaN NaN NaN Sales NaN Table sales Pillow Sales
1 The price of pillows NaN NaN 1.9473 1.832 2.13 2.20 3
2 The price of tables 1.19 NaN NaN 1.9 2.0 2.0 2.5

I need to make a plot "Price of the pillows vs Date", but if there was an event on the date it must be on the plot. How can I add information about events and make a plot?

For simple plot I tried (with Plotly)

df = df.T
fig = px.line(df, x = df.index, y = df['The price of pillows'], width=900, height=800)
fig.update_layout(title="The price of pillows trend",xaxis_title="Date", yaxis_title="Price"
fig.show()

But something is wrong :(

Final result have to be like this pic: enter image description here

Thanks for the help!


Solution

  • The reason for error in plotting is due to the mismatch in column assignment after doing a transpose of the dataFrame with df.T

    This can be solved using df.set_index() method.

    original df:

    Metrics 01.08.21 1.09.21 01.10.21 01.11.21 01.12.21 01.13.21 01.14.21
    0 Events NaN NaN NaN Sales NaN Table sales Pillow Sales
    1 The price of pillows NaN NaN 1.9473 1.832 2.13 2.2 3
    2 The price of tables 1.19 NaN NaN 1.9 2.00 2 2.5

    df.T (notice that column names are now indices of df):

    0 1 2
    Metrics Events The price of pillows The price of tables
    01.08.21 NaN NaN 1.19
    1.09.21 NaN NaN NaN
    01.10.21 NaN 1.9473 NaN
    01.11.21 Sales 1.832 1.9
    01.12.21 NaN 2.13 2
    01.13.21 Table sales 2.2 2
    01.14.21 Pillow Sales 3 2.5

    Hence, we do:

    df.set_index('Metrics',inplace=True)
    df = df.T
    

    So now, df is:

    Metrics Events The price of pillows The price of tables
    01.08.21 NaN NaN 1.19
    1.09.21 NaN NaN NaN
    01.10.21 NaN 1.9473 NaN
    01.11.21 Sales 1.832 1.9
    01.12.21 NaN 2.13 2
    01.13.21 Table sales 2.2 2
    01.14.21 Pillow Sales 3 2.5

    And, finally we can proceed with plotting. the Events column can be annotated on the graph using the text keyword argument for px.line

    Full code for plotting:

    df.set_index('Metrics',inplace=True)
    df = df.T
    fig = px.line(df, x = df.index, y = "The price of pillows", text="Events", width=900, height=800)
    fig.update_traces(textposition='bottom right')
    fig.update_layout(title="The price of pillows trend",xaxis_title="Date", yaxis_title="Price")
    fig.show()
    

    Output figure:

    Required Figure