使用imshow绘制的时间序列

3
我尽力使标题尽可能清晰,但我不确定它是否完全清晰。我有三个数据系列(随时间变化的事件数量)。我想做一个子图,其中表示这三个时间序列。您将找到我能想到的最好的附件。最后一个时间序列明显较短,因此在此处不可见。
我还添加了相应的代码,以便您更好地理解我正在尝试做什么,并就正确/明智的方法向我提供建议。
import numpy as np
import matplotlib.pyplot as plt

x=np.genfromtxt('nbr_lig_bound1.dat')
x1=np.genfromtxt('nbr_lig_bound2.dat')
x2=np.genfromtxt('nbr_lig_bound3.dat')
# doing so because imshow requieres a 2D array
# best way I found and probably not the proper way to get it done
x=np.expand_dims(x, axis=0)
x=np.vstack((x,x))
x1=np.expand_dims(x1, axis=0)
x1=np.vstack((x1,x1))
x2=np.expand_dims(x2, axis=0)
x2=np.vstack((x2,x2))
# hoping that this would compensate for sharex shrinking my X range to 
# the shortest array 
ax[0].set_xlim(1,24)
ax[1].set_xlim(1,24)
ax[2].set_xlim(1,24)


fig, ax = plt.subplots(nrows=3, ncols=1, figsize=(6,6), sharex=True)
fig.subplots_adjust(hspace=0.001) # this seem to have no effect 

p1=ax[0].imshow(x1[:,::10000], cmap='autumn_r')
p2=ax[1].imshow(x2[:,::10000], cmap='autumn_r')
p3=ax[2].imshow(x[:,::10000], cmap='autumn')

这是我目前为止能够达到的结果: 实际结果 以下是我希望拥有的方案,因为我在网上找不到它。简而言之,我想删除两个上图中绘制数据周围的空白空间。作为一个更一般的问题,我想知道imshow是否是获得这样的绘图的最佳方法(参见下面的预期结果)。

enter image description here


请问您能否描述一下您对所得结果不喜欢的具体原因?“期望”的图表在很多方面都与现有结果不同,因此不清楚您真正想要什么。 - ImportanceOfBeingErnest
我可以做到。我想要移除上面两个图中绘制数据周围的空格。作为一个更一般的问题,我想知道imshow是否是获得这样的绘图(参见预期结果)的最佳方法。 - Nicolas Martin
事实上,我在绘图时增加数据点的数量越多,图中的白色空间就越大,显示数据的颜色线条/区域也就越细。这就是为什么我认为使用imshow可能不是正确的方向。 - Nicolas Martin
1个回答

2
使用fig.subplots_adjust(hspace=0)将子图之间的垂直(高度)间距设置为零,但不会调整每个子图内部的垂直间距。默认情况下,plt.imshow具有默认纵横比(rc image.aspect),通常设置为像素是正方形,以便您可以准确地重新创建图像。要更改此设置,请使用aspect='auto'并相应地调整您的轴的ylim

例如:
# you don't need all the `expand_dims` and `vstack`ing.  Use `reshape`
x0 = np.linspace(5, 0, 25).reshape(1, -1)
x1 = x0**6
x2 = x0**2

fig, axes = plt.subplots(3, 1, sharex=True)
fig.subplots_adjust(hspace=0)

for ax, x in zip(axes, (x0, x1, x2)):
    ax.imshow(x, cmap='autumn_r', aspect='auto') 
    ax.set_ylim(-0.5, 0.5) # alternatively pass extent=[0, 1, 0, 24] to imshow
    ax.set_xticks([]) # remove all xticks
    ax.set_yticks([]) # remove all yticks

plt.show()

收益率

enter image description here

要添加颜色条,建议查看此答案,该答案使用fig.add_axes()或查看AxesDivider文档(我个人更喜欢这种方法)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接