Matplotlib时间序列绘图去除时间间隔的实现(无pandas环境)
解决方法
核心思路是放弃使用原始时间序列作为x轴坐标值,改用连续整数索引定位每个数据点,再将x轴刻度替换为对应时间字符串,既保证点排布均匀无空白,又保留每个点的时间对应关系。
完整实现代码
基于你原有代码的修改版本
import matplotlib.pyplot as plt from datetime import datetime # 你原有的排序逻辑保持不变 time_sorted_list = sorted(unsorted_value_list, key=lambda x: x.time) elev = [i.elev for i in time_sorted_list] time = [i.time for i in time_sorted_list] # 生成连续整数作为x轴坐标,保证所有数据点间隔均匀 x_axis = list(range(len(time))) # 绘图逻辑 fig, elev_plot = plt.subplots(figsize=(12, 6)) elev_plot.plot(x_axis, elev) elev_plot.grid() # 配置x轴刻度,避免标签重叠,tick_step可根据数据量灵活调整 tick_step = 5 ticks = x_axis[::tick_step] tick_labels = [t.strftime("%m/%d %H:%M") for t in time[::tick_step]] elev_plot.set_xticks(ticks) elev_plot.set_xticklabels(tick_labels, rotation=45, ha='right') # 可选配置:鼠标hover时显示对应点的完整时间+海拔信息 def format_coord(x, y): idx = int(round(x)) if 0 <= idx < len(time): return f"时间: {time[idx].strftime('%m/%d %H:%M:%S')}, 海拔: {y:.2f}m" return f"x={x:.2f}, y={y:.2f}" elev_plot.format_coord = format_coord plt.tight_layout() plt.show()
可直接运行的测试版本(适配你提供的示例数据)
import matplotlib.pyplot as plt from datetime import datetime import numpy as np # 示例海拔数据 elev_data = [7.061637017210896, 8.62634035986128, 9.449231409579046, 9.449245213599722, 11.183401391828983, 11.183478912151985, 12.097695062804538, 14.032121063226736, 19.53103255309029, 20.132430448781705, 22.61562154333468, 23.892538058003574, 25.174568988146742, 25.81347252259264, 27.07766665010065, 28.301824218809962, 29.4560748154805, 30.51425894250495, 31.44003996067941, 32.19935454662037, 32.75797351858856, 33.09230892539046, 33.185638377860386, 32.64289077682021, 32.64282073446187, 32.03439364065985, 32.03432718356379, 31.235743890788736, 30.278072995085186, 29.198208966807904, 28.02762496428912, 25.534718034319297, 24.259335234095236, 22.987561974637945, 21.733969026630948, 20.50551578656278, 19.30698140187512, 18.145822000390414, 17.021157410685678, 14.89032761900031, 13.881534452146786, 12.910228441720443, 11.9735858799619, 10.20078824064575, 8.548230021876677, 7.7622314951825935, 7.002108526108933, 6.2652418436101245, 5.550265750342097, 4.180538242033181, 3.523953356314147, 10.468976986513358, 10.826799614265274, 11.548804997129018, 15.198784031309774, 15.913277577899912, 16.609161706884624, 18.52422058507705, 19.077032064883326, 19.57286148977654, 20.002244208317894, 20.91143576667658, 20.91127829131031, 20.911272488234292, 20.817089892791472, 20.630717861747748, 20.630698531303153, 20.35705893695184, 20.357030796826844, 20.001375885553323, 19.571700419702164, 19.075697249423904, 17.921110661414083, 15.911217262744842, 15.197579232135709, 14.472858366158526, 13.740784521720999, 13.007405649336956] # 示例时间字符串列表 time_str_list = ['08/26/2021, 08:28:28', '08/26/2021, 08:28:48', '08/26/2021, 08:28:58', '08/26/2021, 08:28:58', '08/26/2021, 08:29:18', '08/26/2021, 08:29:18', '08/26/2021, 08:29:28', '08/26/2021, 08:29:48', '08/26/2021, 08:30:38', '08/26/2021, 08:30:43', '08/26/2021, 08:31:03', '08/26/2021, 08:31:13', '08/26/2021, 08:31:23', '08/26/2021, 08:31:28', '08/26/2021, 08:31:38', '08/26/2021, 08:31:48', '08/26/2021, 08:31:58', '08/26/2021, 08:32:08', '08/26/2021, 08:32:18', '08/26/2021, 08:32:28', '08/26/2021, 08:32:38', '08/26/2021, 08:32:48', '08/26/2021, 08:32:58', '08/26/2021, 08:33:18', '08/26/2021, 08:33:18', '08/26/2021, 08:33:28', '08/26/2021, 08:33:28', '08/26/2021, 08:33:38', '08/26/2021, 08:33:48', '08/26/2021, 08:33:58', '08/26/2021, 08:34:08', '08/26/2021, 08:34:28', '08/26/2021, 08:34:38', '08/26/2021, 08:34:48', '08/26/2021, 08:34:58', '08/26/2021, 08:35:08', '08/26/2021, 08:35:18', '08/26/2021, 08:35:28', '08/26/2021, 08:35:38', '08/26/2021, 08:35:58', '08/26/2021, 08:36:08', '08/26/2021, 08:36:18', '08/26/2021, 08:36:28', '08/26/2021, 08:36:48', '08/26/2021, 08:37:08', '08/26/2021, 08:37:18', '08/26/2021, 08:37:28', '08/26/2021, 08:37:38', '08/26/2021, 08:37:48', '08/26/2021, 08:38:08', '08/26/2021, 08:38:18', '08/26/2021, 10:11:00', '08/26/2021, 10:11:05', '08/26/2021, 10:11:15', '08/26/2021, 10:12:05', '08/26/2021, 10:12:15', '08/26/2021, 10:12:25', '08/26/2021, 10:12:55', '08/26/2021, 10:13:05', '08/26/2021, 10:13:15', '08/26/2021, 10:13:25', '08/26/2021, 10:14:05', '08/26/2021, 10:14:15', '08/26/2021, 10:14:15', '08/26/2021, 10:14:25', '08/26/2021, 10:14:35', '08/26/2021, 10:14:35', '08/26/2021, 10:14:45', '08/26/2021, 10:14:45', '08/26/2021, 10:14:55', '08/26/2021, 10:15:05', '08/26/2021, 10:15:15', '08/26/2021, 10:15:35', '08/26/2021, 10:16:05', '08/26/2021, 10:16:15', '08/26/2021, 10:16:25', '08/26/2021, 10:16:35', '08/26/2021, 10:16:45'] # 转换时间格式 time_data = [datetime.strptime(t, "%m/%d/%Y, %H:%M:%S") for t in time_str_list] x_axis = list(range(len(time_data))) # 绘图 fig, ax = plt.subplots(figsize=(12, 6)) ax.plot(x_axis, elev_data) ax.grid(True) # 配置刻度 tick_step = 6 ax.set_xticks(x_axis[::tick_step]) ax.set_xticklabels([t.strftime("%m/%d %H:%M") for t in time_data[::tick_step]], rotation=45, ha='right') # hover显示完整信息 def format_coord(x, y): idx = int(round(x)) if 0 <= idx < len(time_data): return f"时间: {time_data[idx].strftime('%m/%d %H:%M:%S')}, 海拔: {y:.2f}m" return f"x={x:.2f}, y={y:.2f}" ax.format_coord = format_coord ax.set_ylabel("海拔(m)") ax.set_title("海拔随采样点变化趋势") plt.tight_layout() plt.show()
可选优化:标记时间断档位置
如果需要明确区分不连续的采样段,可以计算相邻点的时间差,超过设定阈值的位置添加断档标记:
# 计算相邻时间差,单位为秒 time_diff = np.diff([t.timestamp() for t in time_data]) # 超过10分钟判定为采样断档,阈值可自行
相关产品推荐
相关产品推荐

