这个问题解释了如何在 x 轴上使用
datetime
对象:
使用 imshow 绘制 matplotlib 绘图的 x 轴中的日期
效果非常好。
但是我拥有的是与 1 分钟或 3 分钟的不同时间间隔相对应的测量数据,并且不同持续时间的数据中也存在间隙 - 10 分钟或 1 小时或之间的任何时间。 数据位于具有日期时间键和 numpy 数组值的字典中。有些按键间隔 1 分钟,有些按键间隔 3 分钟。数据中也存在> 3分钟的间隙,通常不是< 10 min.
如果我将其绘制为:
ax.imshow(arr, extent = [x1, x2, y1, y2], aspect='auto')
其中 x1 和 x2 是开始和结束时间,y1 和 y2 垂直维度,arr 是从上面提到的字典值生成的 2d numpy 数组,
数据将均匀分布在时间轴上,我希望间隙可见,并且我希望根据其时间分辨率和测量时间绘制数据。
我想将
nans
写入我的数组来填补空白,但随后我还必须复制 3 分钟时间步长的数据来填补“空白”,因为大多数数据每分钟都可用。
我正在寻找更好、更优雅的选择。当然如果有其他方法我就不必使用
imshow()
。
我没有找到使用 matplotlib 来解决所描述问题的解决方案。但plotly提供了一个解决方案,因为Heatmap对象也允许将时间作为参数:
import plotly.graph_objects as go
import numpy as np
# Assuming image is your image and times is your array of time values
image = np.random.rand(10, 10) # Replace with your image
times = np.array([1, 2, 4, 7, 11, 16, 22, 29, 37, 46]) # Replace with your times
# Create a 2D heatmap to represent the image
fig = go.Figure(data=go.Heatmap(
z=image,
x=times,
colorscale='Gray',
showscale=False
))
fig.update_layout(
xaxis_title="Time",
yaxis_title="Y",
autosize=False,
width=500,
height=500,
margin=dict(
l=50,
r=50,
b=100,
t=100,
pad=4
)
)
fig.show()