Search code examples
time-seriesplotlyplotly-dashplotly-pythontimeserieschart

How to plot line curve with one x axis and two y axis with plotly?


I have one dataframe with two columns Date and Amount. I'm able to plot those with matplotlib but unable to plot with plotly. The below is matplotlib example

result.Amount[:362].plot(figsize=(15,8), title= 'Credit Amount by date', fontsize=14)
result.Amount[362:].plot(figsize=(15,8), title= 'Credit Amount by date', fontsize=14)
plt.show()

And the output is :

enter image description here

But when I'm trying the same with plotly it's not working. The code I've written for plotly is

fig = px.line(result, x='Date', y=[Amount[:362], Amount[362:]], template = 'plotly_dark')

fig.show()

It's returning error like

NameError                                 Traceback (most recent call last)
<ipython-input-61-449ab60f9636> in <module>
      1 # line plot
      2 
----> 3 fig = px.line(result, x='Date', y=[Amount[:362], Amount[362:]], template = 'plotly_dark')
      4 
      5 # add a vertical rectange for test-set separation

NameError: name 'Amount' is not defined

https://drive.google.com/file/d/10TN_FSs27r5agLUv4aoopfaRpGsnIG5m/view?usp=sharing

The above is link for data

Can anyone help me with this?


Solution

  • Using plotly.graph_objects, you can draw multiple graphs within a single graph.

    import plotly.graph_objects as go
    
    fig = go.Figure()
    fig.add_trace(go.Scatter(x=df['Date'][:362], y=df['Amount'][:362], mode='lines', name='A'))
    fig.add_trace(go.Scatter(x=df['Date'][362:], y=df['Amount'][362:], mode='lines', name='B'))
    
    fig.show()
    

    enter image description here