I intend to make a plot of Suspension Travel x Distance of a train. I have a dataset with these information and with information relating if the train is in a straight track or in a curve. I want to mimic the following image, but I have some additional information I have to add (bridge location and tunnel for example). The problem is that what I tried takes almost four minutes to run.
# Plot of the line I want to add
def transition_line(xmin, xmax):
for i in range((df_irv['Distance'] - xmin).abs().idxmin(), (df_irv['Distance'] - xmax).abs().idxmin()):
plt.plot([df_irv['Distance'][i], df_irv['Distance'][i+1]], [max(df_irv['SuspTravel']) + 0.5]*2, color='red' if df_irv['Element'][i] == 'CURVA' else 'blue', linewidth=10, alpha=0.5)
# Function to plot the data with adjustable x-axis limits
def plot_graph(xmin, xmax, sensors):
plt.figure(figsize=(10, 5))
plt.plot(df_irv['Distance'], df_irv[sensors], label='Suspension Sensor')
plt.xlim(xmin, xmax)
plt.xlabel('Distance (Km)')
plt.ylabel('Suspension Sensor')
plt.title('Suspension Sensor vs Distance')
plt.legend()
plt.grid(True)
transition_line(xmin, xmax)
plt.show()
# Create sliders for x-axis limits
xmin_slider = IntSlider(value=0, min=0, max=df_irv['Distance'].max(), step=1, description='X min')
xmax_slider = IntSlider(value=20, min=0, max=df_irv['Distance'].max(), step=1, description='X max')
# Interactive plot
interact(plot_graph, xmin=xmin_slider, xmax=xmax_slider, sensors = ['SuspTravel', 'Roll', 'Bounce'])
Image produced by my attempt
New contributor
Tiago Amorim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.