为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕的地6264312 2021-07-30 09:44:05
我正在尝试制作这个图,类似于所附的示例图。我还附上了示例数据。示例数据我想绘制一列(BLC 73.61、FCC 68.5、BSL 40.69)与时间的关系图,它们位于数据框的索引中,并与日期时间 xticks 一起绘制,并希望显示相应的 MLT(磁性本地时间)值,即位于数据库的第四列。这是我正在尝试执行的代码:图片展示了我想做的一个例子。x 刻度标签在一行中以日期时间格式显示 uT 小时的值,在第二行或第三行中显示 MLT 的对应值# read the datadata = 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-axisax1.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()这是我得到的结果:
查看完整描述

1 回答

?
holdtom

TA贡献1805条经验 获得超10个赞

我刚刚为此找到了一种解决方案。


我创建了一个函数来定义我想要的刻度并使用了 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 values

ax1.xaxis.set_major_locator(mdates.MonthLocator(interval=5))

# use the format difeined in format_func2

ax1.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))


查看完整回答
反对 回复 2021-08-03
  • 1 回答
  • 0 关注
  • 143 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号