Home > Software design >  Plotly line graph to follow ordered time series on X-axis
Plotly line graph to follow ordered time series on X-axis

Time:12-19

I have a dataframe that looks like this :

country = ['Cambodia',
 'Cambodia',
 'Cambodia',
 'Cambodia',
 'Cambodia',
 'Lao PDR',
 'Lao PDR',
 'Lao PDR',
 'Lao PDR']

month_year = ['2020-Aug',
 '2020-Dec',
 '2020-May',
 '2020-Oct',
 '2021-Mar',
 '2020-Jul',
 '2021-Mar',
 '2021-May',
 '2021-Nov']

count = [5, 4, 4, 4, 2, 6, 4, 4, 6]

# Make dictionary, keys will become dataframe column names
intermediate_dictionary = {'country':country, 'month_year':month_year, 'count':count}

# Convert dictionary to Pandas dataframe
df3 = pandas_dataframe = pd.DataFrame(intermediate_dictionary)
df3

Using the dataframe I ordered the months correctly then plotted a line graph this way

ordered_months = ["2020-May", "2020-Jul", "2020-Aug", "2020-Oct", "2020-Dec", "2021-Mar",
      "2021-May", "2021-Nov"]

fig = px.line(df3, x="month_year", y="count", color='country', symbol='country')
fig.update_xaxes( categoryarray= ordered_months)

fig.show()

Now my problem is, the line chart for the country cambodia is not following the order of months as expected. Here is a photo of the graph

enter image description here

According to the graph , cambodia line chart starts from May-2020 then skips Aug-2020 and goes to oct-2020 then go to Dec-2020 before coming back to Aug-2020. Notice also that the line connecting to Mar-2021 doesnt start from Dec-2020 but Oct-2020.

My intention is the line for cambodia to follow the order of x-axis, from May-2020 go to Aug-2020 then Oct-2020 then Dec-2020 and finally Mar-2020.

How can I fix this ?

CodePudding user response:

I have been trying to solve your problem.

First of all, you create the DF and values, thats Ok. What I added is the function pd.to_datetime to make pandas recognise the dates and order them.

country = ['Cambodia',
 'Cambodia',
 'Cambodia',
 'Cambodia',
 'Cambodia',
 'Lao PDR',
 'Lao PDR',
 'Lao PDR',
 'Lao PDR']

month_year = ['2020-Aug',
 '2020-Dec',
 '2020-May',
 '2020-Oct',
 '2021-Mar',
 '2020-Jul',
 '2021-Mar',
 '2021-May',
 '2021-Nov']

month_year = pd.to_datetime(month_year)
count = [5, 4, 4, 4, 2, 6, 4, 4, 6]

Then you create the DF, in my case I sorted by 'month_year':

# Make dictionary, keys will become dataframe column names
intermediate_dictionary = {'country':country, 'month_year':month_year, 'count':count}

# Convert dictionary to Pandas dataframe
df3 = pd.DataFrame(intermediate_dictionary).sort_values(by='month_year')

Finally, plot the graph:

fig = px.line(df3, x="month_year", y="count", color='country',symbol='country')
fig.show()

The output would be:

output

  • Related