首页 > 解决方案 > 如何对熊猫数据框的日期索引进行排序,以便在图表上绘制时所有较新的年份日期都位于 X 轴标签的一侧

问题描述

我有一个以日期为索引(索引)的熊猫数据框。当我绘制值时,索引(日期,即 X 轴标签)不会以正确的顺序显示在绘制图形的 X 轴上。例如,代替所有 2018 年的日期(例如 2018/02/15、2018/03/10、2018/10/12 ... 2019/01/07、2019/01/10、2019/03/16 .. .),我会让这些日期以不匹配的顺序显示在 X 轴上。例如 2019/01/07、2019/01/10、2018/02/15、2018/03/10、2019/03/16 ...即使我已对索引(即日期)应用排序。我该如何处理这个问题?先感谢您。

我试图对索引进行排序,但这不起作用。

DTT_data = miniBid_data.groupby(['Mini_Bid_Date_2'])['New_Cost_Per_Load','Volume'].aggregate([np.mean])

# sort the data
DTT_data.sort_index(inplace=True, ascending=True)

fig, ax = plt.subplots()

color1 = 'tab:red'
DTT_data.plot(kind='line', figsize=(12,8), legend=False, ax=ax, logy=True, marker='*')
ax.set_title('Trends of Selected Variables')
ax.set_ylabel('Log10 of Values', color=color1)
ax.legend(loc='upper left')
ax.set_xlabel('Event Dates')
ax.tick_params(axis='y', labelcolor=color1)
#ax.legend(loc='upper left')

ax1 = ax.twinx()

color2 = 'tab:blue'
DTT_data2 = miniBid_data.groupby(['Mini_Bid_Date_2'])['Carrier_Code'].count()
DTT_data2.plot(kind='bar', figsize=(12,8), legend=False, ax=ax1, color=color2)
DTT_data2.sort_index(inplace=True, ascending=False)
ax1.set_ylabel('Log10 of Values', color=color2)
ax1.set_yscale('log')
ax1.tick_params(axis='y', labelcolor=color2)
ax1.legend(loc='upper right')
fig.autofmt_xdate()
fig.tight_layout()
plt.show()

Sample Data:
a) DTT_data = 
Mini_Bid_Date_2  New_Cost_Per_Load      Volume
01/07/2019  1604.3570393487105  1.6431478968792401
02/25/2018  1816.1534797297306  2.831081081081081
10/22/2018  1865.5403827160494  2.074074074074074
10/29/2018  1945.3011032028478  1.9023576512455516
01/08/2019  1947.7562972972971  1.162162162162162
02/11/2019  2062.7133737931017  2.3424827586206916
11/05/2018  2095.531836956521   1.7753623188405796
12/08/2018  2155.48935907859    1.437825203252031
02/04/2019  2169.209245791246   2.2669696969696966
02/04/2018  2189.3693333333335  5.0
01/14/2019  2313.3854211711728  1.1587162162162181
01/20/2019  2380.9063928571427  1.0
01/21/2019  2631.0407864661634  1.3657894736842129
12/03/2018  2684.0808513089005  4.402827225130894
02/25/2019  2844.047048492792   1.89397116644823
11/12/2018  3011.510282722513   2.147905759162304
10/08/2018  3042.3035776536312  1.8130726256983247
11/19/2018  3063.736631460676   1.7407865168539327
02/18/2019  3148.531689480355   6.798162230671736
10/01/2018  3248.0486851851842  2.1951388888888905
01/19/2019  3291.1334154589376  1.4626086956521749
10/15/2018  11881.90527833753   1.779911838790932
01/28/2019  13786.149445804196  1.6329195804195813
03/04/2019  14313.741501103752  1.5459455481972018
12/10/2018  100686.89588865546  3.051260504201676


b) DTT_data = 
Mini_Bid_Date_2 Carrier_Code
12/08/2018  1476
03/04/2019  1359
02/04/2019  1188
10/29/2018  1124
12/03/2018  955
10/08/2018  895
11/19/2018  890
10/15/2018  794
02/18/2019  789
02/25/2019  763
01/07/2019  737
02/11/2019  725
01/21/2019  665
10/01/2018  648
02/25/2018  592
01/28/2019  572
12/10/2018  476
01/14/2019  444
11/12/2018  382
10/22/2018  324
11/05/2018  276
01/19/2019  207
01/20/2019  56
01/08/2019  37
02/04/2018  30



My expectation is to have dates (indexes) in this case show up in sequential order, for example, 2019/01/07, 2019/01/10, 2018/02/15, 2018/03/10, 2019/03/16 ... on as labels on the X-axis.

标签: pandas

解决方案


推荐阅读