辅助x轴标签

2024-06-26 00:20:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个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).

知道我做错了什么吗


Tags: and文件csvindatalabelsplotplt
1条回答
网友
1楼 · 发布于 2024-06-26 00:20:01

我是这样解决的:

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) 

相关问题 更多 >