1

I have this pandas dataframe


index   cath_date
0   2015    65
1   2016    88
2   2017    106
3   2018    97
0   2019Q2  120
6   2019Q1  101
17  2019Q3  48

I tried to plot a line graph using plotly trying this code

x= df['index'].values.tolist()
y= df['cath_date'].values.tolist()
fig = go.Figure(data=go.Scatter(x= df['index'].astype(str).tolist(), y= y,
                                text= y,textposition= 'top center', mode='lines+markers',
                                hovertemplate='Avg: %{y} <br> Year: %{x}<extra></extra>'))
fig.update_layout(title_text='PPCI Primary Average 2015-2018',
                  xaxis= dict(dtick = 1, title=dict(text="Year"), showgrid = True), 
                  yaxis = dict(rangemode="tozero",autorange=True, title=dict(text="Average"), showgrid= True))
fig.show()

this the figure

as you can see the values 2019Q1, 2019Q2, 2019Q3 so, what is the problem

1 Answer 1

1

An x-axis of string doesn't make any sense so plotly is treating them like numbers. Of course having a Q1/2/3 doesn't make any sense as a number so we need to convert them to numbers:

import pandas as pd
import plotly.graph_objects as go

index = [2015,2016,2017,2018,2019,2019.25,2019.5]
cath_date = [65,88,106,97,120,101,48]

x = index
y = cath_date

fig = go.Figure(data=go.Scatter(x= x, y= y,
                                text= y,textposition= 'top center', mode='lines+markers',
                                hovertemplate='Avg: %{y} <br> Year: %{x}<extra></extra>'))
fig.update_layout(title_text='PPCI Primary Average 2015-2018',
                  xaxis= dict(dtick = 1, title=dict(text="Year"), showgrid = True), 
                  yaxis = dict(rangemode="tozero",autorange=True, title=dict(text="Average"), showgrid= True))
fig.show()

Now your last three data points will actually show.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.