在三个值之间插入曲线

时间:2018-07-21 15:11:17

标签: python matplotlib plot

我有以下脚本可以绘制图形:

x = np.array([0,1,2])
y = np.array([5, 4.31, 4.01])
plt.plot(x, y)
plt.show()

问题是,直线从一点到另一点都是笔直的,但是我想平滑点之间的直线。 enter image description here

如果我使用scipy.interpolate.spline平滑数据,则会得到以下结果:

 order = np.array([0,1,2])
 y = np.array([5, 4.31, 4.01])
 xnew = np.linspace(order.min(), order.max(), 300)
 smooth = spline(order, y, xnew)
 plt.plot(xnew, smooth)
 plt.show()

enter image description here

但是我希望得到与给定example相同的结果

1 个答案:

答案 0 :(得分:1)

如果使用的点数多于3,您将得到与链接的问题相同的结果。阶数为3的样条有很多方法可以通过3个点。

但是您当然可以将阶数减少到2。

public class CreateSpreadSheetTask extends AsyncTask<Void, Void, Void> {
    private com.google.api.services.sheets.v4.Sheets mService = null;
    private Exception mLastError = null;
    private String title = null;
    private CreateSpreadsheetListener listener = null;

    public CreateSpreadSheetTask(GoogleAccountCredential credential, String title, CreateSpreadsheetListener listener) {
        HttpTransport transport = AndroidHttp.newCompatibleTransport();
        JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
        mService = new Sheets.Builder(
                transport, jsonFactory, credential)
                .setApplicationName("Google Sheets API Android Quickstart")
                .build();
        this.title = title;
        this.listener = listener;
    }

    @Override
    protected Void doInBackground(Void... params) {
        try {
            this.listener.onRecvCreateSpreadsheet(getSpreadsheetFromApi());
        } catch (Exception e) {
            e.printStackTrace();
            mLastError = e;
            cancel(true);
        }
        return null;
    }

    private Spreadsheet getSpreadsheetFromApi() throws IOException {
        Spreadsheet spreadsheet = new Spreadsheet();
        SpreadsheetProperties properties = new SpreadsheetProperties();
        properties.setTitle(this.title);
        spreadsheet.setProperties(properties);
        Sheets.Spreadsheets.Create request = mService.spreadsheets().create(spreadsheet);
        return request.execute();
    }
}

enter image description here