简体   繁体   English

绘制折线图以遵循 X 轴上的有序时间序列

[英]Plotly line graph to follow ordered time series on X-axis

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这是图表的照片

在此处输入图像描述

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 .根据图表,柬埔寨折线图从May-2020开始,然后跳过Aug-2020 ,然后转到oct-2020 ,然后转到Dec-2020 ,然后返回到Aug-2020 Notice also that the line connecting to Mar-2021 doesnt start from Dec-2020 but Oct-2020 .另请注意,连接到Mar-2021的线不是从Dec-2020开始,而是从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 .我的意图是让柬埔寨遵循 x 轴的顺序,从May-2020Aug-2020 ,然后是Oct-2020 ,然后是 Dec-2020 ,最后是 Mar-2020

How can I fix this?我怎样才能解决这个问题?

I have been trying to solve your problem.我一直在努力解决你的问题。

First of all, you create the DF and values, thats Ok.首先,您创建 DF 和值,没关系。 What I added is the function pd.to_datetime to make pandas recognise the dates and order them.我添加的是函数 pd.to_datetime使 pandas 识别日期并对其进行排序。

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' :然后创建 DF,在我的例子中,我按 '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:输出将是:

输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM