简体   繁体   English

使用 Matplotlib,如何显示以 HH24:MI 格式排序的 Y 轴值

[英]Using Matplotlib, how to display Y-axis values ordered in HH24:MI format

I am unable to sort the job finish time (Y-Axis).我无法对作业完成时间(Y 轴)进行排序。 Currently it is displaying what each Y-axis has.目前它正在显示每个 Y 轴的内容。 I am going to have more Y-axis added in future.我将来会添加更多的 Y 轴。

Is there any way of sorting the Y-Axis values?有没有办法对 Y 轴值进行排序? My sample code is pasted below and a screenshot of the issue is attached.我的示例代码粘贴在下面,并附上了问题的屏幕截图。 Screenshot attached附截图

from matplotlib import pyplot as plt
from matplotlib.dates import DateFormatter
import datetime as DT

xaxis =[ \[][1]
            DT.datetime(2020, 3, 19, 0, 0),
            DT.datetime(2020, 3, 20, 0, 0),
            DT.datetime(2020, 3, 23, 0, 0),
            DT.datetime(2020, 3, 24, 0, 0)
        ]
jobrunt1 = [DT.datetime(2020, 3, 20, 6, 39, 0),
            DT.datetime(2020, 3, 23, 15, 59, 24),
            DT.datetime(2020, 3, 24, 6, 37, 4),
            DT.datetime(2020, 3, 25, 6, 38, 0)]

jobrunt2 = [DT.datetime(2020, 3, 20, 8, 40, 0),
            DT.datetime(2020, 3, 23, 18, 19, 24),
            DT.datetime(2020, 3, 24, 4, 39, 4),
            DT.datetime(2020, 3, 25, 1, 20, 8)]

yaxis1 = [DT.datetime.strftime(y, "%H:%M") for y in jobrunt1]
yaxis2 = [DT.datetime.strftime(z, "%H:%M") for z in jobrunt2]

f, ax = plt.subplots(figsize=(12, 4))
ax.xaxis.set_major_formatter(DateFormatter('%d/%m/%Y'))
ax.yaxis.set_minor_formatter(DateFormatter('%H:%M'))
ax.set_xlabel('Rundt')
ax.set_ylabel('Job Finish Time')
ax.plot(xaxis, yaxis1, marker='o', label='Job1')
ax.plot(xaxis, yaxis2, marker='^', label='Job2')
plt.show()

Thanks谢谢

Your problem is that you're converting to string, so the y-axis is presented in the given order by default, not sorted.您的问题是您正在转换为字符串,因此默认情况下 y 轴按给定顺序显示,而不是排序。 It's better to work with datetime objects, and let matplotlib figure out the y-axis order for you.最好使用 datetime 对象,并让 matplotlib 为您计算 y 轴顺序。 In order to do that, you need to convert the datetimes into matplotlib's datetime floats using date2num :为此,您需要使用date2num将日期时间转换为 matplotlib 的日期时间浮点数:

from matplotlib.dates import date2num

yaxis1 = date2num(jobrunt1)
yaxis2 = date2num(jobrunt2)

Which will give you this image:这会给你这个图像:

在此处输入图片说明

The y-axis goes up in chronological order, although it is difficult to see, since only time is displayed. y 轴按时间顺序上升,虽然很难看到,因为只显示时间。 Each 00:00 is midnight of a new day ,and the finish times are correctly placed between them.每个00:00是新一天的午夜,完成时间正确放置在它们之间。 However I suspect that what you want is the times modulo the date;但是我怀疑您想要的是以日期为模的时间; as if each time took place on the same day.仿佛每次都发生在同一天。 I think the easiest way to do this is just to replace the date component with a repeating day:我认为最简单的方法就是用重复的日期替换日期组件:

test_day = DT.date(2020, 3, 20)
yaxis1 = date2num([DT.datetime.combine(test_day, t.time()) for t in jobrunt1])
yaxis2 = date2num([DT.datetime.combine(test_day, t.time()) for t in jobrunt2])

All other code being the same, we get this:所有其他代码都相同,我们得到:

在此处输入图片说明

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

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