question: D3 Q2

This commit is contained in:
DBras 2024-06-10 13:49:08 +02:00
parent 3207c3f51f
commit b10d5f623e
1 changed files with 7 additions and 3 deletions

View File

@ -36,18 +36,22 @@ else:
################################################################################ ################################################################################
################## Question 1 ################################################## ################## Question 2 ##################################################
################################################################################ ################################################################################
# Construct a dataframe and pivot it to obtain a dataframe with a column per unit, and a row per timestamp. # Construct a dataframe and pivot it to obtain a dataframe with a column per unit, and a row per timestamp.
df = pd.DataFrame.from_records(data) df = pd.DataFrame.from_records(data)
df['time'] = pd.to_datetime(df['time'], unit='s')
df_pivot = df.pivot_table(values='value', columns='unit', index='time') df_pivot = df.pivot_table(values='value', columns='unit', index='time')
df_resampled = df_pivot.resample('s').mean()
df_resampled.interpolate(method='linear', inplace=True)
df_resampled = pd.DataFrame(df_resampled)
# Plot the data. Note, that the data will mostly not be plotted with lines. # Plot the data. Note, that the data will mostly not be plotted with lines.
plt.ion() # Turn interactive mode on plt.ion() # Turn interactive mode on
plt.figure() plt.figure()
ax1 = plt.subplot(211) # Make two separate figures ax1 = plt.subplot(211) # Make two separate figures
ax2 = plt.subplot(212) ax2 = plt.subplot(212)
df_pivot['pcc_p'].plot(marker='.', ax=ax1, linewidth=3) df_resampled[[c for c in df_resampled.columns if c.endswith('_p')]].plot(marker='.', ax=ax1, linewidth=3)
df_pivot['pcc_q'].plot(marker='.', ax=ax1, linewidth=3) df_resampled[[c for c in df_resampled.columns if c.endswith('_q')]].plot(marker='.', ax=ax2, linewidth=3)
plt.show(block=True) plt.show(block=True)