Matplotlib时间轴连续小时

编程入门 行业动态 更新时间:2024-10-28 01:27:29
本文介绍了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时间轴连续小时

本文发布于:2023-11-22 09:35:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1616908.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:小时   时间   Matplotlib

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!