Set Categorical xaxis range to be longer than there is data

Hello smart plotly community!

I am hoping to force my python plotly line graph’s X axis length to show all 24 months for the last 2 years. I have a python list of all these months for the x values:

Month_LIST = [
‘JAN 2020’, ‘EB 2020’, ‘MAR 2020’, ‘APR 2020’, ‘MAY 2020’, ‘JUN 2020’, ‘JUL 2020’, ‘AUG 2020’, ‘SEPT 2020’, ‘OCT 2020’, ‘NOV 2020’, ‘DEC 2020’, ‘JAN 2021’, ‘FEB 2021’, ‘MAR 2021’, ‘APR 2021’, ‘MAY 2021’, ‘JUN 2021’, ‘JUL 2021’, ‘AUG 2021’, ‘SEPT 2021’, ‘OCT 2021’, ‘NOV 2021’, ‘DEC 2021’]

(So, it’s a list of 24 strings)

However, there is no data yet for DEC 2021 from my dataframe of y values. SO, when I make a graph like this below, plotly forces the x values to stop at NOV 2021. But I do NOT want it to stop at NOV 2021. I want it to show DEC 2021 on the x axis even though there is no data for that month.

Of course, I could force plotly to show the final month by adding 0 values to the y axis data in the dataframe. BUT I don’t want to do that because then the line graphs will display the lines going downard to zero on the graph which is misleading. I just want to show all the x values even if there’s no data there. How can I do that???

I assume I can use update_xaxis somehow in plotly but I can’t figure out how!

Any insight here would be SO appreciated!!! Thank you!!!

Hello @alex3,
A workaround would be to use None instead of 0 values on the y axis data, see the below example :

import plotly.graph_objects as go

fig = go.Figure(go.Scatter(
    mode = "lines+markers",
    y = [4, 1, 3, None],
    x = ["December", "January", "February", "March"]))
fig.update_xaxes(type='category')
fig.show()

Great, I will try this, thank you!!

1 Like