2021年3月25日星期四

matplotlib DateFormatter not showing correct dates with yyyy-mm-dd column

I have a python dataframe with two columns, a numeric column (total_outbounds) on the y-axis and a date column (month, pardon the bad name) for x-axis:

enter image description here

and when when I run this code to create a graph using this dataframe:

fig,ax = plt.subplots()    my_df.plot(x='month', y='total_outbounds', ax=ax, label = 'Total Email Outbounds on LE Change')  ax.xaxis.set_major_formatter(mdates.DateFormatter('%m/%y'))  plt.xlabel('')  plt.title('Total LE Changes and Outbounds by Month', pad = 10)  

I receive a graph where the X-axis is not what I was hoping for... Am I using mdates.DateFormatter wrong? Looking to receive mm/yy on the X-Axis, instead of the Apr, Jul, etc. that are currently appearing.

enter image description here

For reproducibility, here is the dataframe output with my_df.to_dict()

{'month': {0: Timestamp('2020-01-01 00:00:00'),    1: Timestamp('2020-02-01 00:00:00'),    2: Timestamp('2020-03-01 00:00:00'),    3: Timestamp('2020-04-01 00:00:00'),    4: Timestamp('2020-05-01 00:00:00'),    5: Timestamp('2020-06-01 00:00:00'),    6: Timestamp('2020-07-01 00:00:00'),    7: Timestamp('2020-08-01 00:00:00'),    8: Timestamp('2020-09-01 00:00:00'),    9: Timestamp('2020-10-01 00:00:00'),    10: Timestamp('2020-11-01 00:00:00'),    11: Timestamp('2020-12-01 00:00:00'),    12: Timestamp('2021-01-01 00:00:00'),    13: Timestamp('2021-02-01 00:00:00'),    14: Timestamp('2021-03-01 00:00:00')},   'total_outbounds': {0: 26364,    1: 33081,    2: 35517,    3: 34975,    4: 40794,    5: 51659,    6: 50948,    7: 65332,    8: 82839,    9: 96408,    10: 86923,    11: 99176,    12: 122199,    13: 116057,    14: 108439}}  

and I think you should be able to use pd.DataFrame.from_dict() to turn that back into a dataframe my_df from the dictionary. Please let me know if there's a more reproducible way to share the dataframe.

Edit: the solution in the comments works, however now I cannot rotate the minor ticks using plt.xaxis(rotation=50), this only rotates the two major ticks... also the X-axis values appearing are odd (showing 71 as the year?)

enter image description here

https://stackoverflow.com/questions/66809866/matplotlib-dateformatter-not-showing-correct-dates-with-yyyy-mm-dd-column March 26, 2021 at 09:35AM

没有评论:

发表评论