0

I have two csv files that have been generated on one chronological basis during my recording (they both have a timestamp column based on one clock). I want to plot my data in matplotlib (or elsewhere using python, if you have a better suggestion).

On my primary x axis, I want to have the general continuous timestamps (from csv file 1).

On my y axis I need the recordings of my desired variable (from csv file 1).

On my secondary x axis, I need to have my experiment events or annotations (from csv file 2), right at the timestamps (ticks) when they happened.

I try to plot all of these, this way:

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)

An example of the csv files is here : 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).

Any ideas what I'm doing wrong?

4

1 回答 1

0

I solved it like this:

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) 
于 2021-03-19T10:24:15.763 回答