3个参数(PosX,PosY)与时间的关系图。这是一个时间序列数据

问题描述 投票:0回答:1

我是这个模块的新手。我有时间序列数据,用于了解粒子随时间的运动。运动相对于时间T具有X和Y分量。我想在图形中绘制这三个参数。示例数据如下所示。第一个列代表时间,第二个X坐标,第三个Y坐标。

1.5193 618.3349 487.55951.5193 619.3349 487.55952.5193 619.8688 489.58692.5193 620.8688 489.58693.5193 622.9027 493.31563.5193 623.9027 493.3156

matplotlib matplotlib-basemap
1个回答
0
投票

如果要向2D曲线添加第三信息,一种可能是使用颜色映射来建立第三坐标的值和一组颜色之间的关系。

在Matplotlib中,我们没有直接的方法来绘制颜色变化的曲线,但是我们可以使用matplotlib.collections.LineCollection来伪造曲线。

以下,我使用了一些任意曲线,但毫无疑问,如果我的代码适合您的需求,您可以根据自己的特定用例调整我的代码。

matplotlib.collections.LineCollection

import numpy as np import matplotlib.pyplot as plt from matplotlib.collections import LineCollection # e.g., a Lissajous curve t = np.linspace(0, 2*np.pi, 6280) x, y = np.sin(4*t), np.sin(5*t) # to use LineCollection we need an array of segments # the canonical answer (to upvote...) is https://stackoverflow.com/a/58880037/2749397 points = np.array([x, y]).T.reshape(-1,1,2) segments = np.concatenate([points[:-1],points[1:]], axis=1) # instantiate the line collection with appropriate parameters, # the associated array controls the color mapping, we set it to time lc = LineCollection(segments, cmap='nipy_spectral', linewidth=6, alpha=0.85) lc.set_array(t) # usual stuff, just note ax.autoscale, not needed here because we # replot the same data but tipically needed with ax.add_collection fig, ax = plt.subplots() plt.xlabel('x/mm') ; plt.ylabel('y/mm') ax.add_collection(lc) ax.autoscale() cb = plt.colorbar(lc) cb.set_label('t/s') # we plot a thin line over the colormapped line collection, especially # useful when our colormap contains white... plt.plot(x, y, color='black', linewidth=0.5, zorder=3) plt.show()

© www.soinside.com 2019 - 2024. All rights reserved.