0

I have a dataset composed for 330 values where I would like to show the correlation between speed and error. If I show the plot as it is, it is difficult to check the correlation. Then, I would like to average the error based on a speed range. I mean, each bar representing the average of the speed between 0 and 1, 1 and 2, and so on. My current code is like this.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.pyplot import figure

figure(figsize=(24, 12), dpi=80)
# Make a random dataset:
height = summ['Error']
bars = summ['MeanSpeed']
y_pos = np.arange(len(bars))

# Create bars
plt.bar(y_pos, height)

# Create names on the x-axis
plt.xticks(y_pos, bars)

# Show graphic
plt.show()

How can I average the values for each speed range?

I would like something like this:

enter image description here

I create this by doing:

m1=np.mean(summ[(summ['MeanSpeed']>=0)&(summ['MeanSpeed']<=1)])[4]
m2=np.mean(summ[(summ['MeanSpeed']>=1)&(summ['MeanSpeed']<=2)])[4]
m3=np.mean(summ[(summ['MeanSpeed']>=2)&(summ['MeanSpeed']<=3)])[4]
m4=np.mean(summ[(summ['MeanSpeed']>=3)&(summ['MeanSpeed']<=4)])[4]
plt.bar([1,2,3,4], [m1,m2,m3,m4])
plt.show()
  • Could you explain better your problem or give more example code? – s.paszko Sep 23 '21 at 09:24
  • This question is not reproducible without **data**. This question needs a [SSCCE](http://sscce.org/). Please see [How to provide a reproducible dataframe](https://stackoverflow.com/q/52413246/7758804), then **[edit] your question**, and paste the clipboard into a code block. Always provide a [mre] **with code, data, errors, current output, and expected output, as [formatted text](https://stackoverflow.com/help/formatting)**. If relevant, plot images are okay. If you don't include an mre, it is likely the question will be downvoted, closed, and deleted. – Trenton McKinney Sep 23 '21 at 14:30

1 Answers1

0

Assuming that you have float mean speed data in MeanSpeed column (0.12, 2.25, 2.77, etc) and you are using dataframe, you can :

  • Add new column MeanSpeedInt with :
df['MeanSpeedInt'] = int(df['MeanSpeed'])
  • Group by MeanSpeedInt column with averaging
df.groupby(by=['MeanSpeedInt']).mean()

And it will results in mean error values in Error column grouped by MeanSpeed groups 0, 1, 2, 3 etc. Something like that. Code not tested, written from memory.

s.paszko
  • 633
  • 1
  • 7
  • 21
  • Should I add both lines? Where should I add the second one? After creating the new column, i cannot average and group the error based on the new column – Poyita de troya Sep 23 '21 at 09:50
  • Please give more detailed and working question description with sample data and columns. Describe better how bar plot should look (maybe picture will help). – s.paszko Sep 23 '21 at 09:58
  • Maybe you are looking also for this ploting function - https://matplotlib.org/stable/gallery/lines_bars_and_markers/errorbar_limits_simple.html#sphx-glr-gallery-lines-bars-and-markers-errorbar-limits-simple-py – s.paszko Sep 23 '21 at 10:00
  • I've updated the post – Poyita de troya Sep 23 '21 at 10:11