154

グラフをプロットする次の簡単なスクリプトがあります。

import matplotlib.pyplot as plt
import numpy as np

T = np.array([6, 7, 8, 9, 10, 11, 12])
power = np.array([1.53E+03, 5.92E+02, 2.04E+02, 7.24E+01, 2.72E+01, 1.10E+01, 4.70E+00])

plt.plot(T,power)
plt.show()

今のように、線はポイントからポイントへとまっすぐに進み、問題ないように見えますが、私の意見ではもっと良いかもしれません。私が欲しいのは、ポイント間の線を滑らかにすることです。gnuplotでは。でプロットしsmooth cplinesます。

PyPlotでこれを行う簡単な方法はありますか?いくつかのチュートリアルを見つけましたが、それらはすべてかなり複雑に見えます。

4

8 に答える 8

202

scipy.interpolate.spline自分でデータを滑らかにするために使用できます。

from scipy.interpolate import spline

# 300 represents number of points to make between T.min and T.max
xnew = np.linspace(T.min(), T.max(), 300)  

power_smooth = spline(T, power, xnew)

plt.plot(xnew,power_smooth)
plt.show()

spline は scipy 0.19.0 で廃止されました。代わりに BSpline クラスを使用してください。

splineからへの切り替えBSplineは単純なコピー/貼り付けではなく、少し調整が必要です。

from scipy.interpolate import make_interp_spline, BSpline

# 300 represents number of points to make between T.min and T.max
xnew = np.linspace(T.min(), T.max(), 300) 

spl = make_interp_spline(T, power, k=3)  # type: BSpline
power_smooth = spl(xnew)

plt.plot(xnew, power_smooth)
plt.show()

前: スクリーンショット 1

後: スクリーンショット 2

于 2011-03-12T17:09:24.390 に答える
9

あなたの質問の文脈から、アンチエイリアシングではなく、カーブフィッティングを意味していると思います。PyPlot にはこれに対するビルトイン サポートはありませんが、ここで見られるコードのように、基本的なカーブ フィッティングを自分で簡単に実装できます。または、GuiQwt を使用している場合は、カーブ フィッティングモジュールがあります。(おそらく、これを行うためにSciPyからコードを盗むこともできます)。

于 2011-03-12T17:04:23.550 に答える
1

私が見つけた最も簡単な実装の 1 つは、Tensorboard が使用する指数移動平均を使用することでした。

def smooth(scalars: List[float], weight: float) -> List[float]:  # Weight between 0 and 1
    last = scalars[0]  # First value in the plot (first timestep)
    smoothed = list()
    for point in scalars:
        smoothed_val = last * weight + (1 - weight) * point  # Calculate smoothed value
        smoothed.append(smoothed_val)                        # Save it
        last = smoothed_val                                  # Anchor the last smoothed value
        
    return smoothed


ax.plot(x_labels, smooth(train_data, .9), x_labels, train_data)

ここに画像の説明を入力

于 2021-07-24T14:02:33.387 に答える