写两个或三个 x 刻度标签,使用 matplotlib 显示 UT 和 MLT

我正在尝试制作这个图,类似于所附的示例图。我还附上了示例数据。

示例数据

我想绘制一列(BLC 73.61、FCC 68.5、BSL 40.69)与时间的关系图,它们位于数据框的索引中,并与日期时间 xticks 一起绘制,并希望显示相应的 MLT(磁性本地时间)值,即位于数据库的第四列。

这是我正在尝试执行的代码:

图片展示了我想做的一个例子。x 刻度标签在一行中以日期时间格式显示 uT 小时的值,在第二行或第三行中显示 MLT 的对应值

http://img2.mukewang.com/610914820001660110760220.jpg

# read the data

data = pd.read_csv('dados.csv', index_col=[0])


#plotting 

fig = plt.figure(figsize=(10, 5)) #

ax1 = fig.add_subplot(111)

ax2 = ax1.twiny() # second x-axis

ax1.plot(data.index,data['BLC 73.61'])

ax2.xaxis.set_ticks_position("bottom")

ax2.xaxis.set_label_position("bottom")

ax2.spines["bottom"].set_position(("axes", -0.2))

ax2.spines['bottom'].set_color('none')


majorLocator   = MultipleLocator(1)

minorLocator   = MultipleLocator(1)

ax1.xaxis.set_major_locator(majorLocator)

ax1.xaxis.set_minor_locator(minorLocator)

ax1.xaxis.set_major_locator(majorLocator)

ax1.xaxis.set_minor_locator(minorLocator)

ax1.xaxis.set_minor_formatter(dates.DateFormatter('%H:%M'))

ax1.xaxis.set_minor_locator(mdates.MinuteLocator(interval=30))

ax1.xaxis.set_major_formatter(dates.DateFormatter('%H:%M\n%b %d, %Y'))

ax1.xaxis.set_major_locator(mdates.HourLocator(interval=30))


ax2.set_xticklabels(data['MLT'].values)


# ax2.xaxis.set_minor_formatter(dates.DateFormatter('%H:%M'))

# ax2.xaxis.set_minor_locator(mdates.MinuteLocator(interval=30))

# ax2.xaxis.set_major_formatter(dates.DateFormatter('%H:%M\n%b %d, %Y'))

# ax2.xaxis.set_major_locator(mdates.HourLocator(interval=30))

a=ax2.get_xticks().tolist()

这是我得到的结果:

http://img1.mukewang.com/61091490000139ef13070724.jpg

慕的地6264312
浏览 144回答 1
1回答

holdtom

我刚刚为此找到了一种解决方案。我创建了一个函数来定义我想要的刻度并使用了 FuncFormatter 模块。def format_func(value, tick_number):    hora = mdates.num2date(value)    teste = mlt['BLC 73.61'][hora]    return ('%d:%d \n %0.1f'  % (hora.hour, hora.minute,teste))def format_func2(value, tick_number):    # find the first value    if tick_number == 0:        return ('hh:mm     \n MLT     ')    else:        return (' ')首先将时间戳值转换为数字:xx = [mdates.date2num(i) for i in ada[ini:end].index]和情节fig = plt.figure(figsize=(10, 5)) #ax1 = fig.add_subplot(111)ax1.plot(xx,ada['BLC 73.61'][ini:end])# set the major locator ion the month valuesax1.xaxis.set_major_locator(mdates.MonthLocator(interval=5))# use the format difeined in format_func2ax1.xaxis.set_major_formatter(plt.FuncFormatter(format_func2))ax1.xaxis.set_minor_locator(mdates.HourLocator(interval=2)) ax1.xaxis.set_minor_formatter(plt.FuncFormatter(format_func))
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python