简体   繁体   English

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

[英]x-axis tick label for contourf plot using dates

I'm trying to create a time series contourf plot of some velocity/depth data I collected out at sea.我正在尝试创建我在海上收集的一些速度/深度数据的时间序列轮廓图。 The raw data is in a .mat format and I was able to read this into a pandas data frame and produce something really close to what I need, except for the x-axis.原始数据采用 .mat 格式,我能够将其读入 Pandas 数据框并生成一些非常接近我需要的东西,除了 x 轴。

This code produces this plot:此代码生成此图:

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()

1st plot第一个情节

Because it is a matlab file, the time values given are a reference number (ie 736456 (days since some reference date) is May 6th, 2016), which I was able to convert into python datetime objects with this:因为它是一个 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))

If I add this line to the first block of code, it produces this plot:如果我将此行添加到第一个代码块,它会生成以下图:

ax.set_xticklabels(axisdate)

2nd plot第二个情节

In the first plot, the range of the x-axis is about a year, but when I change them into the converted dates, the same date shows up for each tick mark.在第一个图中,x 轴的范围大约是一年,但是当我将它们更改为转换后的日期时,每个刻度线都会显示相同的日期。 The reference numbers were all correctly converted to the correct date, but there are some 64,000 data points with multiple data for the same date.参考数字都正确转换为正确的日期,但有大约 64,000 个数据点具有同一日期的多个数据。 So the first few thousand elements in the axisdate list are 2016-05-06, and so on up until 2017-04-19.所以axisdate 列表中的前几千个元素是2016-05-06,依此类推,直到2017-04-19。

First question: How do I get the right date labels for the x-axis?第一个问题:如何为 x 轴获取正确的日期标签?

Second question: How do I remove the hours, minutes, and seconds (00:00:00) from each of the date time objects so that it only shows the day (2016-05-06)?第二个问题:如何从每个日期时间对象中删除小时、分钟和秒 (00:00:00),使其仅显示日期 (2016-05-06)?

Here is some of the a2, b2, and c2 data, respectively:下面分别是 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 ]]

You can use datetime.strftime(format) to format the datetime, change your function matlab2datetime like this: 您可以使用datetime.strftime(format)格式化日期时间,如下更改函数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))

please post the solution.请发布解决方案。 thanks谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM