Home > Enterprise >  plotting dates seems to give the wrong labels
plotting dates seems to give the wrong labels

Time:12-11

I am trying to plot dates on the x-axis. As I have 100,000 points I can't plot them all so I tried the following (I have made a toy example so it is reproducible.)

xdata = ['2020-02-01 00:00', '2020-02-01 01:00', '2020-02-01 02:00', '2020-02-01 03:00', '2020-02-01 05:00', '2020-02-01 06:00', '2020-02-01 07:00', '2020-02-01 08:00', '2020-02-01 09:00', '2020-02-01 10:00', '2020-02-01 11:00', '2020-02-01 12:00', '2020-02-01 13:00', '2020-02-01 14:00', '2020-02-01 15:00', '2020-02-01 16:00', '2020-02-01 17:00', '2020-02-01 18:00', '2020-02-01 19:00', '2020-02-01 20:00', '2020-02-01 21:00', '2020-02-01 22:00', '2020-02-01 23:00', '2020-02-02 00:00', '2020-02-02 01:00', '2020-02-02 02:00', '2020-02-02 03:00', '2020-02-02 04:00', '2020-02-02 05:00', '2020-02-02 06:00', '2020-02-02 07:00', '2020-02-02 08:00', '2020-02-02 09:00', '2020-02-02 10:00', '2020-02-02 11:00', '2020-02-02 12:00', '2020-02-02 13:00', '2020-02-02 14:00', '2020-02-02 15:00', '2020-02-02 16:00', '2020-02-02 17:00', '2020-02-02 18:00', '2020-02-02 19:00', '2020-02-02 20:00', '2020-02-02 21:00', '2020-02-02 22:00', '2020-02-02 23:00', '2020-02-03 00:00', '2020-02-03 01:00', '2020-02-03 02:00', '2020-02-03 04:00', '2020-02-03 05:00', '2020-02-03 06:00', '2020-02-03 07:00', '2020-02-03 08:00', '2020-02-03 09:00', '2020-02-03 10:00', '2020-02-03 11:00', '2020-02-03 12:00', '2020-02-03 13:00', '2020-02-03 14:00', '2020-02-03 15:00', '2020-02-03 16:00', '2020-02-03 17:00', '2020-02-03 18:00', '2020-02-03 19:00', '2020-02-03 20:00', '2020-02-03 21:00', '2020-02-03 22:00', '2020-02-03 23:00', '2020-02-04 00:00', '2020-02-04 01:00', '2020-02-04 03:00', '2020-02-04 05:00', '2020-02-04 06:00', '2020-02-04 07:00', '2020-02-04 08:00', '2020-02-04 09:00', '2020-02-04 10:00', '2020-02-04 11:00', '2020-02-04 12:00', '2020-02-04 13:00', '2020-02-04 14:00', '2020-02-04 15:00', '2020-02-04 16:00', '2020-02-04 17:00', '2020-02-04 18:00', '2020-02-04 19:00', '2020-02-04 20:00', '2020-02-04 21:00', '2020-02-04 22:00', '2020-02-04 23:00', '2020-02-05 00:00', '2020-02-05 01:00', '2020-02-05 02:00', '2020-02-05 03:00', '2020-02-05 04:00', '2020-02-05 05:00', '2020-02-05 06:00', '2020-02-05 07:00']
ydata = [-0.05384615384615384, -0.11458333333333334, 0.2333333333333333, 0.43333333333333335, 0.0, -0.027976190476190488, -0.09283216783216784, 0.10741758241758241, 0.034378599924818405, -0.05627705627705627, -0.04098800505050504, -0.029667207792207793, 0.002295008912655976, -0.013920454545454528, 0.050894938394938416, 0.05308441558441558, 0.054337121212121205, 0.036538461538461534, 0.029629629629629624, -0.023611111111111114, -0.0835978835978836, -0.05612874779541446, 0.010963804713804712, -0.04666666666666667, -0.31499999999999995, -0.009375000000000001, 0.25, 0.0, 0.0, 0.0, 0.09292929292929292, 0.09819444444444443, -0.009590322871572871, 0.1078968253968254, 0.036809269162210334, 0.017669513457556935, 0.0014980158730158676, -0.0010416065416065398, 0.076002886002886, -0.03283955627705629, 0.04109848484848485, -0.0011904761904761908, -0.04500231910946199, -0.02335843169176503, 0.0008887577069395247, 0.044137806637806636, 0.042956349206349206, 0.08068181818181819, 0.4666666666666666, 0.17500000000000002, 0.04375, 0.14285714285714285, -0.02023809523809522, 0.061805555555555565, 0.09047619047619049, 0.0870439084724799, -0.0167194264069264, 0.0011111111111111126, 0.01898989898989899, -0.08500309214594928, 0.05, 0.06493783993783996, 0.009729437229437234, 0.07349834689120403, 0.07711834733893556, -0.03058080808080808, 0.05114128295946478, -0.023092642007115693, -0.010771103896103886, -0.05156249999999997, 0.10714285714285714, 0.014000000000000012, 0.13636363636363635, 0.0, 0.05879629629629629, 0.03732123531230674, 0.020092592592592592, -0.04015151515151515, -0.029629629629629624, 0.042157544836116265, -0.045000000000000005, -0.010294117647058827, -0.04, 0.010238095238095235, 0.04522727272727273, -0.10012626262626262, -0.19333333333333333, 0.0494949494949495, -0.03166666666666668, -0.06487603305785124, 0.11452020202020202, -0.18636363636363634, 0.0, 0.057499999999999996, 0.05000000000000001, 0.0, 0.0, 0.0, 0.0, 0.11620879120879121]
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
# plot the data
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
# different formats:
locator = mdates.AutoDateLocator()
formatter = mdates.ConciseDateFormatter(locator)
#ax.set_title('ConciseFormatter', loc='left', y=0.85, x=0.02, fontsize='medium')
ax.xaxis.set_major_locator(locator)
ax.xaxis.set_major_formatter(formatter)
plt.plot(xdata, ydata)
plt.show()

This gives:

enter image description here

As you can see, the dates are all wrong.

Ideally I would like as many dates as fit on the x-axis and for them to be sloped so you can fit them in.

CodePudding user response:

try converting the string data to a datetime using strptime and transform

xdata = ['2020-02-01 00:00', '2020-02-01 01:00', '2020-02-01 02:00', '2020-02-01 03:00', '2020-02-01 05:00', '2020-02-01 06:00', '2020-02-01 07:00', '2020-02-01 08:00', '2020-02-01 09:00', '2020-02-01 10:00', '2020-02-01 11:00', '2020-02-01 12:00', '2020-02-01 13:00', '2020-02-01 14:00', '2020-02-01 15:00', '2020-02-01 16:00', '2020-02-01 17:00', '2020-02-01 18:00', '2020-02-01 19:00', '2020-02-01 20:00', '2020-02-01 21:00', '2020-02-01 22:00', '2020-02-01 23:00', '2020-02-02 00:00', '2020-02-02 01:00', '2020-02-02 02:00', '2020-02-02 03:00', '2020-02-02 04:00', '2020-02-02 05:00', '2020-02-02 06:00', '2020-02-02 07:00', '2020-02-02 08:00', '2020-02-02 09:00', '2020-02-02 10:00', '2020-02-02 11:00', '2020-02-02 12:00', '2020-02-02 13:00', '2020-02-02 14:00', '2020-02-02 15:00', '2020-02-02 16:00', '2020-02-02 17:00', '2020-02-02 18:00', '2020-02-02 19:00', '2020-02-02 20:00', '2020-02-02 21:00', '2020-02-02 22:00', '2020-02-02 23:00', '2020-02-03 00:00', '2020-02-03 01:00', '2020-02-03 02:00', '2020-02-03 04:00', '2020-02-03 05:00', '2020-02-03 06:00', '2020-02-03 07:00', '2020-02-03 08:00', '2020-02-03 09:00', '2020-02-03 10:00', '2020-02-03 11:00', '2020-02-03 12:00', '2020-02-03 13:00', '2020-02-03 14:00', '2020-02-03 15:00', '2020-02-03 16:00', '2020-02-03 17:00', '2020-02-03 18:00', '2020-02-03 19:00', '2020-02-03 20:00', '2020-02-03 21:00', '2020-02-03 22:00', '2020-02-03 23:00', '2020-02-04 00:00', '2020-02-04 01:00', '2020-02-04 03:00', '2020-02-04 05:00', '2020-02-04 06:00', '2020-02-04 07:00', '2020-02-04 08:00', '2020-02-04 09:00', '2020-02-04 10:00', '2020-02-04 11:00', '2020-02-04 12:00', '2020-02-04 13:00', '2020-02-04 14:00', '2020-02-04 15:00', '2020-02-04 16:00', '2020-02-04 17:00', '2020-02-04 18:00', '2020-02-04 19:00', '2020-02-04 20:00', '2020-02-04 21:00', '2020-02-04 22:00', '2020-02-04 23:00', '2020-02-05 00:00', '2020-02-05 01:00', '2020-02-05 02:00', '2020-02-05 03:00', '2020-02-05 04:00', '2020-02-05 05:00', '2020-02-05 06:00', '2020-02-05 07:00']
ydata = [-0.05384615384615384, -0.11458333333333334, 0.2333333333333333, 0.43333333333333335, 0.0, -0.027976190476190488, -0.09283216783216784, 0.10741758241758241, 0.034378599924818405, -0.05627705627705627, -0.04098800505050504, -0.029667207792207793, 0.002295008912655976, -0.013920454545454528, 0.050894938394938416, 0.05308441558441558, 0.054337121212121205, 0.036538461538461534, 0.029629629629629624, -0.023611111111111114, -0.0835978835978836, -0.05612874779541446, 0.010963804713804712, -0.04666666666666667, -0.31499999999999995, -0.009375000000000001, 0.25, 0.0, 0.0, 0.0, 0.09292929292929292, 0.09819444444444443, -0.009590322871572871, 0.1078968253968254, 0.036809269162210334, 0.017669513457556935, 0.0014980158730158676, -0.0010416065416065398, 0.076002886002886, -0.03283955627705629, 0.04109848484848485, -0.0011904761904761908, -0.04500231910946199, -0.02335843169176503, 0.0008887577069395247, 0.044137806637806636, 0.042956349206349206, 0.08068181818181819, 0.4666666666666666, 0.17500000000000002, 0.04375, 0.14285714285714285, -0.02023809523809522, 0.061805555555555565, 0.09047619047619049, 0.0870439084724799, -0.0167194264069264, 0.0011111111111111126, 0.01898989898989899, -0.08500309214594928, 0.05, 0.06493783993783996, 0.009729437229437234, 0.07349834689120403, 0.07711834733893556, -0.03058080808080808, 0.05114128295946478, -0.023092642007115693, -0.010771103896103886, -0.05156249999999997, 0.10714285714285714, 0.014000000000000012, 0.13636363636363635, 0.0, 0.05879629629629629, 0.03732123531230674, 0.020092592592592592, -0.04015151515151515, -0.029629629629629624, 0.042157544836116265, -0.045000000000000005, -0.010294117647058827, -0.04, 0.010238095238095235, 0.04522727272727273, -0.10012626262626262, -0.19333333333333333, 0.0494949494949495, -0.03166666666666668, -0.06487603305785124, 0.11452020202020202, -0.18636363636363634, 0.0, 0.057499999999999996, 0.05000000000000001, 0.0, 0.0, 0.0, 0.0, 0.11620879120879121]

xdata=[datetime.strptime(x,'%Y-%m-%d %H:%M') for x in xdata]
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
# different formats:
#ax.set_title('ConciseFormatter', loc='left', y=0.85, x=0.02, fontsize='medium')
plt.plot(xdata, ydata)
plt.xticks(rotation=90)
plt.show()

CodePudding user response:

Convert dates to datetime objects and use xaxis.set_major_formatter with mdates.DateFormatter().

import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from datetime import datetime

xdata = ['2020-02-01 00:00', '2020-02-01 01:00', '2020-02-01 02:00', '2020-02-01 03:00', '2020-02-01 05:00', '2020-02-01 06:00', '2020-02-01 07:00', '2020-02-01 08:00', '2020-02-01 09:00', '2020-02-01 10:00', '2020-02-01 11:00', '2020-02-01 12:00', '2020-02-01 13:00', '2020-02-01 14:00', '2020-02-01 15:00', '2020-02-01 16:00', '2020-02-01 17:00', '2020-02-01 18:00', '2020-02-01 19:00', '2020-02-01 20:00', '2020-02-01 21:00', '2020-02-01 22:00', '2020-02-01 23:00', '2020-02-02 00:00', '2020-02-02 01:00', '2020-02-02 02:00', '2020-02-02 03:00', '2020-02-02 04:00', '2020-02-02 05:00', '2020-02-02 06:00', '2020-02-02 07:00', '2020-02-02 08:00', '2020-02-02 09:00', '2020-02-02 10:00', '2020-02-02 11:00', '2020-02-02 12:00', '2020-02-02 13:00', '2020-02-02 14:00', '2020-02-02 15:00', '2020-02-02 16:00', '2020-02-02 17:00', '2020-02-02 18:00', '2020-02-02 19:00', '2020-02-02 20:00', '2020-02-02 21:00', '2020-02-02 22:00', '2020-02-02 23:00', '2020-02-03 00:00', '2020-02-03 01:00', '2020-02-03 02:00', '2020-02-03 04:00', '2020-02-03 05:00', '2020-02-03 06:00', '2020-02-03 07:00', '2020-02-03 08:00', '2020-02-03 09:00', '2020-02-03 10:00', '2020-02-03 11:00', '2020-02-03 12:00', '2020-02-03 13:00', '2020-02-03 14:00', '2020-02-03 15:00', '2020-02-03 16:00', '2020-02-03 17:00', '2020-02-03 18:00', '2020-02-03 19:00', '2020-02-03 20:00', '2020-02-03 21:00', '2020-02-03 22:00', '2020-02-03 23:00', '2020-02-04 00:00', '2020-02-04 01:00', '2020-02-04 03:00', '2020-02-04 05:00', '2020-02-04 06:00', '2020-02-04 07:00', '2020-02-04 08:00', '2020-02-04 09:00', '2020-02-04 10:00', '2020-02-04 11:00', '2020-02-04 12:00', '2020-02-04 13:00', '2020-02-04 14:00', '2020-02-04 15:00', '2020-02-04 16:00', '2020-02-04 17:00', '2020-02-04 18:00', '2020-02-04 19:00', '2020-02-04 20:00', '2020-02-04 21:00', '2020-02-04 22:00', '2020-02-04 23:00', '2020-02-05 00:00', '2020-02-05 01:00', '2020-02-05 02:00', '2020-02-05 03:00', '2020-02-05 04:00', '2020-02-05 05:00', '2020-02-05 06:00', '2020-02-05 07:00']
ydata = [-0.05384615384615384, -0.11458333333333334, 0.2333333333333333, 0.43333333333333335, 0.0, -0.027976190476190488, -0.09283216783216784, 0.10741758241758241, 0.034378599924818405, -0.05627705627705627, -0.04098800505050504, -0.029667207792207793, 0.002295008912655976, -0.013920454545454528, 0.050894938394938416, 0.05308441558441558, 0.054337121212121205, 0.036538461538461534, 0.029629629629629624, -0.023611111111111114, -0.0835978835978836, -0.05612874779541446, 0.010963804713804712, -0.04666666666666667, -0.31499999999999995, -0.009375000000000001, 0.25, 0.0, 0.0, 0.0, 0.09292929292929292, 0.09819444444444443, -0.009590322871572871, 0.1078968253968254, 0.036809269162210334, 0.017669513457556935, 0.0014980158730158676, -0.0010416065416065398, 0.076002886002886, -0.03283955627705629, 0.04109848484848485, -0.0011904761904761908, -0.04500231910946199, -0.02335843169176503, 0.0008887577069395247, 0.044137806637806636, 0.042956349206349206, 0.08068181818181819, 0.4666666666666666, 0.17500000000000002, 0.04375, 0.14285714285714285, -0.02023809523809522, 0.061805555555555565, 0.09047619047619049, 0.0870439084724799, -0.0167194264069264, 0.0011111111111111126, 0.01898989898989899, -0.08500309214594928, 0.05, 0.06493783993783996, 0.009729437229437234, 0.07349834689120403, 0.07711834733893556, -0.03058080808080808, 0.05114128295946478, -0.023092642007115693, -0.010771103896103886, -0.05156249999999997, 0.10714285714285714, 0.014000000000000012, 0.13636363636363635, 0.0, 0.05879629629629629, 0.03732123531230674, 0.020092592592592592, -0.04015151515151515, -0.029629629629629624, 0.042157544836116265, -0.045000000000000005, -0.010294117647058827, -0.04, 0.010238095238095235, 0.04522727272727273, -0.10012626262626262, -0.19333333333333333, 0.0494949494949495, -0.03166666666666668, -0.06487603305785124, 0.11452020202020202, -0.18636363636363634, 0.0, 0.057499999999999996, 0.05000000000000001, 0.0, 0.0, 0.0, 0.0, 0.11620879120879121]

xdata = [datetime.strptime(x, '%Y-%m-%d %H:%M') for x in xdata]

# plot the data
fig = plt.figure()
ax = fig.add_subplot()

plt.plot(xdata, ydata)

# setup x-axis formatter
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%m/%d\n%H:%M'))
#plt.gcf().autofmt_xdate()
plt.show()

Output:

matplotlib figure

  • Related