【发布时间】:2017-05-20 03:16:38
【问题描述】:
我正在尝试为我在海上收集的一些速度/深度数据创建时间序列轮廓图。原始数据采用 .mat 格式,我能够将其读入 pandas 数据帧并生成非常接近我需要的东西,除了 x 轴。
此代码生成此图:
fig = plt.figure()
ax = fig.add_subplot(111)
time, depth = np.meshgrid(a2,b2)
cont = ax.contourf(time, depth, c2, 50, cmap=plt.cm.get_cmap('viridis'),vmin=-1, vmax=1)
ax.set_ylabel("Depth (m)",size=35)
ax.set_xlabel("Time (date YYY/MM/DD)", size=35)
ax.tick_params(labelsize=35)
plt.gca().invert_yaxis()
plt.title("ADCP North-South Velocity", size = 50)
cbar = fig.colorbar(cont)
cbar.set_label(label='Velocity (m/s)', size = 35)
cbar.ax.tick_params(labelsize=35)
mpl.rcParams['figure.figsize'] = 70,35
plt.savefig('adcpV.png')
plt.show()
第一个情节
因为它是一个 matlab 文件,所以给出的时间值是一个参考号(即 736456(从某个参考日期算起的天数)是 2016 年 5 月 6 日),我可以使用以下方法将其转换为 python 日期时间对象:
def matlab2datetime(matlab_datenum):
day = dt.datetime.fromordinal(int(matlab_datenum)) - dt.timedelta(days = 366)
return day
axisdate = []
for elem in a2:
axisdate.append(matlab2datetime(elem))
如果我将这一行添加到第一个代码块,它会产生这个情节:
ax.set_xticklabels(axisdate)
第二个情节
在第一个图中,x 轴的范围约为一年,但是当我将它们更改为转换后的日期时,每个刻度线都会显示相同的日期。参考编号均已正确转换为正确的日期,但有大约 64,000 个数据点包含同一日期的多个数据。因此,axisdate 列表中的前几千个元素是 2016-05-06,依此类推,直到 2017-04-19。
第一个问题:如何为 x 轴获取正确的日期标签?
第二个问题:如何从每个日期时间对象中删除小时、分钟和秒 (00:00:00),使其仅显示日期 (2016-05-06)?
这里分别是 a2、b2 和 c2 的一些数据:
Float64Index([736456.208333, 736456.213542, 736456.21875, 736456.223958,
736456.229167, 736456.234375, 736456.239583, 736456.244792,
736456.25, 736456.255208,
...
736804.703125, 736804.708333, 736804.713541, 736804.71875,
736804.723958, 736804.729166, 736804.734375, 736804.739583,
736804.744791, 736804.75],
dtype='float64', length=64142)
Int64Index([ 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36,
38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70,
72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96],
dtype='int64')
[[ 0.004 0.009 -0.012 ..., -0.072 -0.09 -0.098]
[-0.022 0.013 -0.067 ..., -0.319 -0.293 -0.253]
[-0.053 0.005 -0.017 ..., -0.335 -0.328 -0.343]
...,
[-0.065 0.059 0.05 ..., -0.036 0.018 0.009]
[-0.041 -0.03 -0.035 ..., -0.027 -0.07 -0.046]
[ 0.215 -0.287 0.033 ..., -0.049 0.002 -0.01 ]]
【问题讨论】:
-
你能分享你的a2、b2和c2吗?
-
截图就够了吗?
-
你可以给他们提供一些部分数据集,然后定位比较好
-
好的,我加了。
标签: python matlab matplotlib plot contourf