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?

  • In `loc[pupil_data_in_trial_eye0['trial'] == trial_label]`, there is no column named `trial` in the `pupil_data_in_trial` dataframe. What is meant to be here? – Kris Mar 16 '21 at 16:57
  • There is a column named trial (the last column) and it's values are durationTRI1, frequencyTRI3, etc. The dataframe is somehow misplaced in the .gist. Can you see that now? – ZahraonStack Mar 16 '21 at 21:01
  • I see, but the most recent version on the gist looks like you've uploaded it incorrectly. – Kris Mar 16 '21 at 22:03
  • I just updated the gist, put the whole csv files that I use, along with my full script. – ZahraonStack Mar 17 '21 at 12:59
  • I guess something is wrong with my boolean masks that I use to put data in each trial? – ZahraonStack Mar 17 '21 at 13:58
  • @Kris are the files on the gist now meaningful? – ZahraonStack Mar 18 '21 at 18:02

1 Answers1

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)