本文介绍了Matplotlib时间轴连续小时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想以%H:%M"的方式格式化我的 x 轴,但要像本例中一样使用连续的小时数(例如 2 天 = 48:00):
I want to format my x-axis in the way "%H:%M" but with continuous hours (e.g. 2 days = 48:00) like in this example:
我能做的最接近的尝试是这个例子:
The closest attempt I could made is this example:
但时间不会继续.这是我的简单代码片段:
But the hours doesn't continue. Here is my simple Code snippet:
import matplotlib.pyplot as plt import matplotlib.dates as md import numpy as np dataY = np.array([0,1,2,3,4,5,6,7,8,9]) dataX = np.array([0.1,0.5,0.8,1.2,1.3,1.6,1.9,2.1,2.2,2.5]) #Time values like in Excel 1h = 1/24 dataX = dataX +1 #Otherwise it says Error, time values <1 fig, ax = plt.subplots() timeformat = md.DateFormatter('%H:%M') plt.Axes.format_xdata = timeformat ax.xaxis_date() ax.xaxis.set_major_formatter(timeformat) plt.xlim(1,4) plt.plot(dataX,dataY) plt.ylabel('Y-Values') plt.xlabel('Time [hh:mm]') plt.show()非常感谢
推荐答案您没有在此处绘制任何实际日期.因此,不要尝试将这些值格式化为日期是有意义的.相反,按原样绘制数字并使用您选择的自定义格式.
You are not plotting any actual dates here. It hence makes sense to not try to format those values as dates. Instead, plot the numbers as they are and use your custom format of choice.
import matplotlib.pyplot as plt import matplotlib.ticker as mticker import numpy as np dataY = np.array([0,1,2,3,4,5,6,7,8,9]) dataX = np.array([0.1,0.5,0.8,1.2,1.3,1.6,1.9,2.1,2.2,2.5]) #Time values 1h = 1/24 fig, ax = plt.subplots() def timeformat(x,pos=None): h = int(x*24.) m = int((x*24.-h)*60) return "{:02d}:{:02d}".format(h,m) ax.xaxis.set_major_formatter(mticker.FuncFormatter(timeformat)) plt.plot(dataX,dataY) plt.ylabel('Y-Values') plt.xlabel('Time [hh:mm]') plt.show()
更多推荐
Matplotlib时间轴连续小时
发布评论