I have an array of averages and an array of standard deviations in a pandas dataframe that I would like to plot. The averages correspond to timings (in seconds), and cannot be negative. How do I clip the standard errors in the plot to a minimum of zero?
import numpy as np
import pandas as pd
avg_timings = np.array([0.00039999, 0.00045002, 0.00114999, 0.00155001, 0.00170001,
0.00545 , 0.00550001, 0.0046 , 0.00545 , 0.00685 ,
0.0079 , 0.00979999, 0.0171 , 0.04305001, 0.0204 ,
0.02276359, 0.02916633, 0.06865 , 0.06749998, 0.10619999])
std_dev_timings = array([0.0005831 , 0.00049751, 0.00079214, 0.00135927, 0.00045823,
0.01185953, 0.0083934 , 0.00066328, 0.0007399 , 0.00079214,
0.00083071, 0.00107694, 0.01023177, 0.11911653, 0.00874871,
0.00299976, 0.01048584, 0.01463652, 0.00785808, 0.09579386])
time_df = pd.DataFrame({'avg_time':avg_timings, 'std_dev':std_dev_timings, 'x':np.arange(len(avg_timings))})
ax = time_df.plot(x='x', y='avg_time', yerr='std_dev', figsize=(16,8), legend=False);
ax.set_ylabel('average timings (s)')
ax.set_xlim(-1, 20)
I want to clip the error bars at zero (highlighted in red), so the timing can never be negative. Is there a way to achieve this?
Try using plt.errorbar
and pass in yerr=[y_low, y_high]
:
y_errors = time_df[['avg_time', 'std_dev']].min(axis=1)
fig, ax = plt.subplots(figsize=(16,8))
plt.errorbar(x=time_df['x'],
y=time_df['avg_time'],
yerr = [y_errors, time_df['std_dev']]
);
ax.set_ylabel('average timings (s)')
ax.set_xlim(-1, 20)
plt.show()
Output:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.