20

私のデータは次のとおりです。

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

そして、次の2つのグラフを取得できます。

ここに画像の説明を入力

ここに画像の説明を入力

ただし、私が欲しいのはこれです(途中のすべてのポイントの平均): ここに画像の説明を入力

matplotlib で可能ですか? または、リストを手動で変更して、何らかの形で作成する必要がありますか:

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

関連コード

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)))
4

2 に答える 2

31

これは、次のようにすることで最も簡単に実行できると思いますy_mean = [np.mean(y) for i in x]

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()

結果の図: ここに画像の説明を入力

于 2014-04-11T13:53:15.473 に答える
7

はい、自分で計算する必要があります。 plot与えたデータをプロットします。他のデータをプロットする場合は、そのデータを自分で計算してから、代わりにプロットする必要があります。

編集:計算を行う簡単な方法:

>>> 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)
于 2012-08-20T19:19:52.333 に答える