微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

辅助 x 轴标签

如何解决辅助 x 轴标签

我有两个 csv 文件,它们是在我的录制过程中按时间顺序生成的(它们都有一个基于一个时钟的时间戳列)。 我想在 matplotlib(或使用 python 的其他地方,如果你有更好的建议)中绘制我的数据。

在我的主 x 轴上,我想要一般的连续时间戳(来自 csv 文件 1)。

在我的 y 轴上,我需要我想要的变量的记录(来自 csv 文件 1)。

在我的第二个 x 轴上,我需要有我的实验事件或注释(来自 csv 文件 2),就在它们发生时的时间戳(滴答声)处。

我尝试以这种方式绘制所有这些:

ticks = annotations_pd_frame['timestamp']
labels = annotations_pd_frame['label']

fig,ax1 = plt.subplots()
ax2 = ax1.twiny()

fig.set_figheight(5)
fig.set_figwidth(25)
ax1.yaxis.grid()

plt.xticks(ticks,labels)

plt.plot(pupil_data_in_trial_eye0['pupil_timestamp'].loc[pupil_data_in_trial_eye0['trial'] == trial_label],pupil_data_in_trial_eye0['diameter_3d'].loc[pupil_data_in_trial_eye0['trial'] == trial_label])
plt.plot(pupil_data_in_trial_eye1['pupil_timestamp'].loc[pupil_data_in_trial_eye1['trial'] == trial_label],pupil_data_in_trial_eye1['diameter_3d'].loc[pupil_data_in_trial_eye1['trial'] == trial_label])

plt.legend(['eye0','eye1'])
ax1.set_xlabel('Timestamps [s]')
ax1.set_ylabel('Diameter [mm]')
plt.title('Pupil Diameter in ' + str(label) )
plt.grid(b=True)

这里有一个 csv 文件的例子: https://gist.github.com/Zahra-on-Github/aa67a3e309fa66582a118f5c08509f77

First figure is when I plot my main data using plt.plot and I get correct ticks and labels (ticks and labels correctly shown as they happened in this one trial of data),but incorrect timestamps on the primary x axis.

Second figure is when I plot my main data using ax1.plot and I get correct timestamps on primary x axis,but incorrect ticks and labels (the whole run’s ticks and labels are shown for this one trial of data).

知道我做错了什么吗?

解决方法

我是这样解决的:

for (t,l) in zip(ticks,labels):
    ax1.axvline(t,color='black',linestyle='--')
    trans = mtransforms.blended_transform_factory(ax1.transData,ax1.transAxes)
    ax1.text(t,1.1,l,ha='center',transform=trans,rotation = 30) 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。