How to take a damn middle row for scatter / plot in MatPlotLib?

My data is as follows:

x = [3,4,5,6,7,8,9,9] y = [6,5,4,3,2,1,1,2] 

And I can get the following two graphs.

enter image description here

and

enter image description here

However, I want this (on average, all the points along the way): enter image description here

Is this possible in matplotlib? Or I need to manually change the list and somehow create:

 x = [3,4,5,6,7,8,9] y = [6,5,4,3,2,1,1.5] 

RELATED CODE

 ax.plot(x, y, 'o-', label='curPerform') x1,x2,y1,y2 = ax.axis() x1 = min(x) - 1 x2 = max(x) + 1 ax.axis((x1,x2,(y1-1),(y2+1))) 
+6
source share
2 answers

Yes, you must do the calculation yourself. plot displays the data you give it. If you want to build some other data, you need to calculate this data yourself, and then build it.

Edit: quick way to calculate:

 >>> x, y = zip(*sorted((xVal, np.mean([yVal for a, yVal in zip(x, y) if xVal==a])) for xVal in set(x))) >>> x (3, 4, 5, 6, 7, 8, 9) >>> y (6.0, 5.0, 4.0, 3.0, 2.0, 1.0, 1.5) 
+2
source

I think this can be done the easiest by doing y_mean = [np.mean(y) for i in x]

An example :

 import matplotlib.pyplot as plt import random import numpy as np # Create some random data x = np.arange(0,10,1) y = np.zeros_like(x) y = [random.random()*5 for i in x] # Calculate the simple average of the data y_mean = [np.mean(y)]*len(x) fig,ax = plt.subplots() # Plot the data data_line = ax.plot(x,y, label='Data', marker='o') # Plot the average line mean_line = ax.plot(x,y_mean, label='Mean', linestyle='--') # Make a legend legend = ax.legend(loc='upper right') plt.show() 

Resulting figure : enter image description here

+6
source

Source: https://habr.com/ru/post/923315/


All Articles