【问题标题】:Secondary x axis labels次要 x 轴标签
【发布时间】:2021-03-16 16:42:07
【问题描述】:

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

任何想法我做错了什么?

【问题讨论】:

  • loc[pupil_data_in_trial_eye0['trial'] == trial_label] 中,pupil_data_in_trial 数据框中没有名为trial 的列。在这里的意义是什么?
  • 有一个名为 trial 的列(最后一列),它的值是 durationTRI1、frequencyTRI3 等。数据框在 .gist 中以某种方式放错了位置。你现在能看到吗?
  • 我明白了,但是关于 gist 的最新版本看起来你上传不正确。
  • 我刚刚更新了要点,将我使用的整个 csv 文件以及我的完整脚本放在一起。
  • 我猜我用来在每次试验中放入数据的布尔掩码有问题?

标签: python csv matplotlib jupyter-lab xticks


【解决方案1】:

我是这样解决的:

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) 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-04
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    相关资源
    最近更新 更多