Python 使用日期绘制轮廓图的x轴刻度标签

Python 使用日期绘制轮廓图的x轴刻度标签,python,matlab,matplotlib,plot,contourf,Python,Matlab,Matplotlib,Plot,Contourf,我正试图用我在海上收集的一些速度/深度数据创建一个时间序列轮廓图。原始数据是.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

我正试图用我在海上收集的一些速度/深度数据创建一个时间序列轮廓图。原始数据是.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()
ax.set_xticklabels(axisdate)
第一地块

因为它是一个matlab文件,所以给出的时间值是一个参考号(即736456(从某个参考日期算起的天数)是2016年5月6日),我可以通过以下方式将其转换为python datetime对象:

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))
如果我将这一行添加到第一个代码块,它将生成以下绘图:

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()
ax.set_xticklabels(axisdate)
第二地块

在第一个图中,x轴的范围约为一年,但当我将其更改为转换日期时,每个记号都显示相同的日期。参考号都正确地转换为正确的日期,但有大约64000个数据点包含同一日期的多个数据。因此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 ]]
您可以使用来格式化日期时间,更改函数
matlab2datetime
,如下所示:

def matlab2datetime(matlab_datenum):
    day = (dt.datetime.fromordinal(int(matlab_datenum)) - dt.timedelta(days = 366)).strftime("%Y-%m-%d")
    return day
axisdate = []
for elem in a2:
    axisdate.append(matlab2datetime(elem))

你能分享你的a2、b2和c2吗?一个屏幕截图就足够了吗?你可以为他们提供一些部分数据集,然后最好是targetokay,我添加了它。哦,等等,这对格式有效,谢谢!关于如何确定轴上的日期有什么建议吗?你能和我分享你的真实数据吗?我想帮你,怎么帮?把所有的都贴在这里?有很多数据你可以分享到某个我也可以像github一样得到的地方?好的,当然。我实际上是在jupyter笔记本上做的也许我可以把它和数据一起发送给你?