Secondary Y axis Graph

Dear Experts,

i am unable to plot the secondary Y axis in this code… Kindly share if there is any link to study and get this code corrected or if you guys can help!!!

prepare data frame

from plotly.offline import iplot
from plotly.subplots import make_subplots

df2 = df.iloc[:100,:]

import graph objects as “go”

import plotly.graph_objs as go

Create figure with secondary y-axis

fig = make_subplots(specs=[[{“secondary_y”: True}]])

Creating trace1

trace1 = go.Scatter(
x = df2.DEP,
y = df2.IND,
mode = “lines”,
name = “DELTA”,
marker = dict(color = ‘rgba(16, 112, 2, 0.8)’),
text= df2.IND)

Creating trace2

trace2 = go.Scatter(
x = df2.DEP,
y = df2.SU,
mode =“lines+markers”,
name = “SOUTHWEST”,
marker = dict(color = ‘rgba(80, 26, 80, 0.8)’),
text= df2.SU)

Creating trace3

trace3 = go.Bar(
x = df2.DEP,
y = df2.PAX,
name = “Pax”,
text= df2.PAX)

fig.add_trace(trace1, secondary_y=False);
fig.add_trace(trace2, secondary_y=False);
fig.add_trace(trace3, secondary_y=True);

data = [trace1, trace2,trace3]
layout = dict(title = ‘DELTA vs SU Fare comparison’,
xaxis= dict(ticklen= 5,zeroline= False,
title= ‘Departure Dates’,secondary_y=False);
yaxis= dict(range=[0,1000],
title= ‘Pax’,secondary_y=False);
yaxis= dict(range=[0,20],
title= ‘fares’,secondary_y=True);

fig = dict(data = data, layout = layout)
iplot(fig)

Hi @lprabhu1107 ,

To get the secondary yaxis displayed you should reference at least one of the three traces to that yaxis, i.e. at least one of fig.add_trace() should set secondary_y=True.

Dear empet,

made few changes to the code based on your inputs, still the issue persists

prepare data frame

from plotly.offline import iplot
from plotly.subplots import make_subplots

df2 = df.iloc[:100,:]

import graph objects as “go”

import plotly.graph_objs as go

Create figure with secondary y-axis

fig = make_subplots(specs=[[{“secondary_y”: True}]])

Creating trace1

trace1 = go.Scatter(
x = df2.DEP,
y = df2.IND,
mode = “lines”,
name = “Delta”,
marker = dict(color = ‘rgba(16, 112, 2, 0.8)’),
text= df2.IND)

Creating trace2

trace2 = go.Scatter(
x = df2.DEP,
y = df2.DL,
mode =“lines+markers”,
name = “UNITED”,
marker = dict(color = ‘rgba(80, 26, 80, 0.8)’),
text= df2.DL)

Creating trace3

trace3 = go.Bar(
x = df2.DEP,
y = df2.PAX,
name = “Pax”,
text= df2.PAX)

fig.add_trace(trace1, secondary_y=False);
fig.add_trace(trace2, secondary_y=False);
fig.add_trace(trace3, secondary_y=True);

data = [trace1, trace2,trace3]
layout = dict(title = ‘DL vs UA Fare comparison’,
xaxis= dict(title= ‘Departure Dates’,ticklen= 5,zeroline= False)
)

fig = dict(data = data, layout = layout)
iplot(fig)

y for the third trace is text, y = df2.PAX?

this a fare in X axis vs number of pax in Y axis
Trace 1 and 2 is linked to Fare and Trace 3 is linked to Pax

I asked you not the names, FARE and PAX, but whether y is a numeric list or not Without data I cannot help you.

sorry…y is numeric…Details as below

DEP DL SU PAX
02/09/2018 23,275 37,715 74
03/09/2018 21,373 38,292 69
04/09/2018 19,875 35,142 199
05/09/2018 19,875 32,543 220
06/09/2018 21,985 30,464 271
07/09/2018 19,004 32,543 253

I noticed you are using decimal numbers in french fomat (with comma) Hence first you have to read the csv file into your dataframe as follows:

df = pd.read_csv('your_file.csv',  decimal=',')

Here is an example that illustrates how to add traces to figure, to get the secondary yaxis displayed:

import plotly.graph_objects as go
import pandas as pd
import numpy as np
from datetime import datetime
from plotly.subplots import make_subplots
np.random.seed(123)

fig = make_subplots(specs=[[{'secondary_y': True}]])

n=8

d = {'date': [datetime(2020, 3, k ) for k in range(2, n+2)],
     'A': 21+12*np.random.rand(n),
     'B': 16+12*np.random.rand(n),
     'C': 14+10*np.random.rand(n)}
df = pd.DataFrame(d)

fig.add_trace(go.Scatter(x=df['date'], y= df['A']), secondary_y=False)  
fig.add_trace(go.Scatter(x=df['date'], y= df['B']), secondary_y=False)  

fig.add_trace(go.Scatter(x=df['date'], y= df['C']), secondary_y=True) 

This is brilliant…thanks a lot…you have saved lot of time for me!!!