使用下面的数据集,它代表随时间变化的深度记录的幅度。
data = {
'Time': [0, 2.5, 5, 7.5, 10],
'Depth_63.5': [161, 143, 134, 147, 163],
'Depth_64.5': [183, 190, 375, 255, 241],
'Depth_65.5': [711, 727, 914, 756, 747],
}
我可以绘制时间与幅度的图表,如下所示。
但是,我想要的是在父图上绘制的图表,X 轴为时间,Y 轴为深度,如下所示。
如何达到这个结果?我当前的代码如下所示...
import matplotlib.pyplot as plt
import pandas as pd
# Sample data..
data = {
'Time': [0, 2.5, 5, 7.5, 10],
'Depth_63.5': [161, 143, 134, 147, 163],
'Depth_64.5': [183, 190, 375, 255, 241],
'Depth_65.5': [711, 727, 914, 756, 747],
}
# Convert data to a DataFrame
df = pd.DataFrame(data)
# Extract depth levels from column names
depth_levels = [col.split('_')[1] for col in df.columns if 'Depth_' in col]
# Create the parent figure with subplots
fig, ax = plt.subplots(figsize=(10, 6))
# Loop through each depth level and plot Time vs Amplitude on the same axes
for depth_level in depth_levels:
ax.plot(df['Time'], df[f'Depth_{depth_level}'], label=f'Depth {depth_level}')
# Customize the plot appearance
ax.set_title('Depth vs Time vs Amplitude')
ax.set_xlabel('Time (us)')
ax.set_ylabel('Depth Levels')
ax.legend()
ax.grid(True)
# Show the parent figure
plt.show()
看起来您正在寻找子情节:
(
df.set_index("Time").iloc[:, ::-1]
.plot(subplots=True, figsize=(8, 6), # with optional legend=False
xlabel="Time", title="Depth vs Time vs Amplitude")
)
plt.subplots_adjust(hspace=0, top=0.94, left=0.1)
plt.gcf().supylabel("Depth")
plt.show();
输出: