0

I am trying to plot some data but the tick marks show up in the incorrect order on the plot.

Yes, I know that xdata is just zeros, it just happened that the log I was using had only zeros.
Yes, I know that ydata appears to repeat itself, that is just how the data is in the log. I need to convince people that we need to add milliseconds to the log times.

fig = plt.figure()
fig.set_size_inches(18.5, 10.5)
fontSize = 9

ax = plt.subplot(1,7,7)
ax.set_title("My Data Plot", y=0, pad=-30)
ax.xaxis.set_ticks_position('top')
ax.yaxis.set_ticks_position('right')
ax.yaxis.set_label_position('right')

timeTicks = ['17:41:39', '17:41:30', '17:41:20', '17:41:10', '17:41:00', '17:40:50', '17:40:40', '17:40:30', '17:40:20']

xdata = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
d = xdata
d.reverse()

ydata = ['17:40:36', '17:40:36', '17:40:36', '17:40:37', '17:40:37', '17:40:37', '17:40:37', '17:40:37', '17:40:38', '17:40:38', '17:40:38', '17:40:38', '17:40:38', '17:40:39', '17:40:39', '17:40:39', '17:40:39', '17:40:39', '17:40:40', '17:40:40', '17:40:40', '17:40:40', '17:40:40', '17:40:41', '17:40:41', '17:40:41', '17:40:41', '17:40:41', '17:40:42', '17:40:42', '17:40:42', '17:40:42', '17:40:42', '17:40:43', '17:40:43', '17:40:43', '17:40:43', '17:40:43', '17:40:44', '17:40:44', '17:40:44', '17:40:44', '17:40:44', '17:40:45', '17:40:45', '17:40:45', '17:40:45', '17:40:45', '17:40:46', '17:40:46', '17:40:46', '17:40:46', '17:40:46', '17:40:47', '17:40:47', '17:40:47', '17:40:47', '17:40:47', '17:40:48', '17:40:48', '17:40:48', '17:40:48', '17:40:48', '17:40:49', '17:40:49', '17:40:56', '17:40:56', '17:40:56', '17:40:57', '17:40:57', '17:40:57', '17:40:57', '17:40:57', '17:40:58', '17:40:58', '17:40:58', '17:40:58', '17:40:58', '17:40:59', '17:40:59', '17:40:59', '17:40:59', '17:40:59', '17:41:00', '17:41:00', '17:41:00', '17:41:00', '17:41:00', '17:41:01', '17:41:01', '17:41:01', '17:41:01', '17:41:01', '17:41:02', '17:41:02', '17:41:02', '17:41:02', '17:41:02', '17:41:03', '17:41:03', '17:41:03', '17:41:03', '17:41:03', '17:41:04', '17:41:04', '17:41:04', '17:41:04', '17:41:04', '17:41:05', '17:41:05', '17:41:05', '17:41:05', '17:41:05', '17:41:06', '17:41:06', '17:41:06', '17:41:06', '17:41:06', '17:41:07', '17:41:07', '17:41:07', '17:41:07', '17:41:07', '17:41:08', '17:41:08', '17:41:08', '17:41:08', '17:41:08', '17:41:09', '17:41:09', '17:41:09', '17:41:09', '17:41:09', '17:41:10', '17:41:10', '17:41:10', '17:41:10', '17:41:10', '17:41:11', '17:41:11', '17:41:11', '17:41:11', '17:41:11', '17:41:12', '17:41:12', '17:41:12', '17:41:12', '17:41:12', '17:41:13', '17:41:13', '17:41:13', '17:41:13', '17:41:13', '17:41:14', '17:41:14', '17:41:14', '17:41:14', '17:41:14', '17:41:15', '17:41:15', '17:41:15', '17:41:15', '17:41:15', '17:41:16', '17:41:16', '17:41:16', '17:41:16', '17:41:16', '17:41:17', '17:41:17', '17:41:17', '17:41:17', '17:41:17', '17:41:18', '17:41:18', '17:41:18', '17:41:18', '17:41:18', '17:41:19', '17:41:19', '17:41:19', '17:41:19', '17:41:19', '17:41:20', '17:41:20', '17:41:20', '17:41:20', '17:41:20', '17:41:21', '17:41:21', '17:41:21', '17:41:21', '17:41:21', '17:41:22', '17:41:22', '17:41:22', '17:41:22', '17:41:22', '17:41:23', '17:41:23', '17:41:23', '17:41:23', '17:41:23', '17:41:24', '17:41:24', '17:41:24', '17:41:24', '17:41:24', '17:41:25', '17:41:25', '17:41:25', '17:41:25', '17:41:25', '17:41:26', '17:41:26', '17:41:26', '17:41:26', '17:41:26', '17:41:27', '17:41:27', '17:41:27', '17:41:27', '17:41:27', '17:41:28', '17:41:28', '17:41:28', '17:41:28', '17:41:28', '17:41:29', '17:41:29', '17:41:29', '17:41:29', '17:41:29', '17:41:30', '17:41:30', '17:41:30', '17:41:30', '17:41:30', '17:41:31', '17:41:31', '17:41:31', '17:41:31', '17:41:31', '17:41:32', '17:41:32', '17:41:32', '17:41:32', '17:41:32', '17:41:33', '17:41:33', '17:41:33', '17:41:33', '17:41:33', '17:41:34', '17:41:34', '17:41:34', '17:41:34', '17:41:34', '17:41:35', '17:41:35', '17:41:35', '17:41:35', '17:41:35', '17:41:36', '17:41:36', '17:41:36', '17:41:36', '17:41:36', '17:41:37', '17:41:37', '17:41:37', '17:41:37', '17:41:37', '17:41:38', '17:41:38', '17:41:38', '17:41:38', '17:41:38', '17:41:39', '17:41:39', '17:41:39', '17:41:39']
t = ydata
t.reverse()

ax.step(d, t, label="0xff1200")
ax.set_yticks(timeTicks, timeTicks)
ax.set_ylabel('Time (hh:mm:ss)')
ax.set_xticks([-100, -50, 0, 50, 100], ['-100', '-50', '0', '50', '100'], fontsize=fontSize)

plt.show()

The tick marks of the plot are not in the correct order, they show up like this:

17:40:20
17:40:30
17:40:50
17:40:40

17:41:00
17:41:10
17:41:20
17:41:30
17:41:39

I have seen this answer here, but this question is really about data that isn't in order.

According to matplotlib documentation that the step plot of matplotlib plots the data in the order that it was received and doesn't attempt to re-order the data.

So why do the tick marks show up in the wrong order?

Update

If I comment out the d.reverse() and t.reverse() lines then the tick labels are really messed up.

17:40:20
17:40:30
17:40:50
17:41:39
17:41:30
17:41:20
17:41:10
17:41:00
17:40:40

Shouldn't tick marks/labels stay in the order that they are set to in set_yticks()?

Fred
  • 1,054
  • 1
  • 12
  • 32
  • 2
    Your time data are strings, and matplotlib doesn't know the correct "order", it just plots them as categoricals. – Jody Klymak Jan 24 '22 at 15:04
  • @JodyKlymak So you are saying that matplotlib attempts to sort the data instead of plotting them in the order that they were received? – Fred Jan 24 '22 at 15:09
  • 3
    No it will plot them in the order it was received. Stop trying to change the tick marks, and you will see that it is working as intended. However, what you probably really want to do is convert to datetime and let matplotlib plot them as numbers rather than categories. – Jody Klymak Jan 24 '22 at 15:24
  • I understand now, matplotlib is confused by the strings. I converted them and things look good now. – Fred Jan 24 '22 at 15:28
  • 2
    If you look at the tick locations with `ax.get_yticks` you see that the strings provided by you are converted to float values in a very strange way. I could not how exactly they were converted, but it is definitively not what you want. You should convert the times to numbers (like e.g. number of seconds), or use datetime objects and the matplotlib datetime support (https://stackoverflow.com/questions/1574088/plotting-time-in-python-with-matplotlib/16428019) – Jakob Stark Jan 24 '22 at 15:33

0 Answers0