matplotlib中的平滑贝塞尔等效绘图

时间:2017-10-19 05:34:05

标签: python-3.x matplotlib gnuplot smoothing

我需要帮助在matplotlib中使用plt.steps函数绘制光谱。由于数据有噪音,在gnuplot中我使用了平滑bezier选项来平滑。 matplotlib中有类似的选项吗?样条曲线无法使所需级别的数据平滑。以下是一个示例数据集https://drive.google.com/open?id=0B4shfFfM7MOqV2h3ZDA0RXlOa2M

我用plt.steps(data [:,0],data [:,1])绘制了log x值。 情节已附上。matplotlib image 。如何用matplotlib平滑整个数据。(我对python绘图很新)

1 个答案:

答案 0 :(得分:1)

一个简单的低通滤波器似乎很有效。根据需要调整alpha。

smoothed

#! /usr/bin/env python3


import matplotlib.pyplot as plt


def smooth(y, alpha=0.1):
    ret = []
    sm = y[0]  # smoothed value, a moving average
    for val in y:
        ret.append(sm)
        sm = alpha * val + (1 - alpha) * sm
    return ret


def plot(points):
    x = [a for a, b in points]
    y = [b for a, b in points]

    plt.subplot(211)
    plt.semilogx(x, y)

    plt.subplot(212)
    plt.semilogx(x, smooth(y))

    plt.savefig('/tmp/smoothed.png')


if __name__ == '__main__':
    plot([
        (0.25, 0.000),
        (0.35, 0.055),
        (0.45, 0.103),
        (0.55, 0.104),
        (0.65, 0.143),
        (0.75, 0.140),
        (0.85, 0.143),
        (0.95, 0.143),
        (1.05, 0.126),
        (1.15, 0.223),
        (1.25, 0.217),
        (1.35, 0.232),
        (1.45, 0.225),
        (1.55, 0.219),
        (1.65, 0.223),
        (1.75, 0.236),
        (1.85, 0.216),
        (1.95, 0.222),
        (2.05, 0.244),
        (2.15, 0.244),
        (2.25, 0.257),
        (2.35, 0.221),
        (2.45, 0.221),
        (2.55, 0.213),
        (2.65, 0.238),
        (2.75, 0.209),
        (2.85, 0.223),
        (2.95, 0.226),
        (3.05, 0.212),
        (3.15, 0.247),
        (3.25, 0.247),
        (3.35, 0.236),
        (3.45, 0.215),
        (3.55, 0.218),
        (3.65, 0.241),
        (3.75, 0.209),
        (3.85, 0.239),
        (3.95, 0.221),
        (4.05, 0.169),
        (4.15, 0.246),
        (4.25, 0.230),
        (4.35, 0.229),
        (4.45, 0.242),
        (4.55, 0.264),
        (4.65, 0.188),
        (4.75, 0.182),
        (4.85, 0.248),
        (4.95, 0.172),
        (5.05, 0.189),
        (5.15, 0.228),
        (5.25, 0.183),
        (5.35, 0.272),
        (5.45, 0.201),
        (5.55, 0.204),
        (5.65, 0.203),
        (5.75, 0.198),
        (5.85, 0.187),
        (5.95, 0.244),
        (6.05, 0.229),
        (6.15, 0.202),
        (6.25, 0.234),
        (6.35, 0.231),
        (6.45, 0.173),
        (6.55, 0.206),
        (6.65, 0.173),
        (6.75, 0.178),
        (6.85, 0.183),
        (6.95, 0.188),
        (7.05, 0.181),
        (7.15, 0.153),
        (7.25, 0.150),
        (7.35, 0.183),
        (7.45, 0.188),
        (7.55, 0.111),
        (7.65, 0.145),
        (7.75, 0.195),
        (7.85, 0.192),
        (7.95, 0.156),
        (8.05, 0.126),
        (8.15, 0.095),
        (8.25, 0.151),
        (8.35, 0.127),
        (8.45, 0.130),
        (8.55, 0.095),
        (8.65, 0.127),
        (8.75, 0.219),
        (8.85, 0.122),
        (8.95, 0.094),
        (9.05, 0.128),
        (9.15, 0.054),
        (9.25, 0.122),
        (9.35, 0.080),
        (9.45, 0.139),
        (9.55, 0.107),
        (9.65, 0.097),
        (9.75, 0.087),
        (9.85, 0.050),
        (9.95, 0.090),
        (10.05, 0.053),
        (10.15, 0.121),
        (10.25, 0.055),
        (10.35, 0.056),
        (10.45, 0.014),
        (10.55, 0.087),
        (10.65, 0.044),
        (10.75, 0.150),
        (10.85, 0.077),
        (10.95, 0.140),
        (11.05, 0.064),
        (11.15, 0.065),
        (11.25, 0.132),
        (11.35, 0.050),
        (11.45, 0.068),
        (11.55, 0.017),
        (11.65, 0.000),
        (11.75, 0.072),
        (11.85, 0.110),
        (11.95, 0.056),
        (12.05, 0.057),
        (12.15, 0.115),
        (12.25, 0.098),
        (12.35, 0.060),
        (12.45, 0.101),
        (12.55, 0.041),
        (12.65, 0.062),
        (12.75, 0.063),
        (12.85, 0.064),
        (12.95, 0.065),
        (13.05, 0.066),
        (13.15, 0.157),
        (13.25, 0.023),
        (13.35, 0.093),
        (13.45, 0.094),
        (13.55, 0.072),
        (13.65, 0.048),
        (13.75, 0.098),
        (13.85, 0.125),
        (13.95, 0.101),
        (14.05, 0.051),
        (14.15, 0.104),
        (14.25, 0.053),
        (14.35, 0.054),
        (14.45, 0.054),
        (14.55, 0.083),
        (14.65, 0.112),
        (14.75, 0.113),
        (14.85, 0.115),
        (14.95, 0.087),
        (15.05, 0.029),
        (15.15, 0.000),
        (15.25, 0.091),
        (15.35, 0.031),
        (15.45, 0.124),
        (15.55, 0.031),
        (15.65, 0.032),
        (15.75, 0.065),
        (15.85, 0.033),
        (15.95, 0.033),
        (16.05, 0.000),
        (16.15, 0.068),
        (16.25, 0.000),
        (16.35, 0.070),
        (16.45, 0.141),
        (16.55, 0.143),
        (16.65, 0.072),
        (16.75, 0.073),
        (16.85, 0.000),
        (16.95, 0.037),
        (17.05, 0.113),
        (17.15, 0.077),
        (17.25, 0.039),
        (17.35, 0.078),
        (17.45, 0.079),
        (17.55, 0.040),
        (17.65, 0.041),
        (17.75, 0.082),
        (17.85, 0.041),
        (17.95, 0.042),
        (18.05, 0.042),
        (18.15, 0.043),
        (18.25, 0.043),
        (18.35, 0.000),
        (18.45, 0.133),
        (18.55, 0.134),
        (18.65, 0.045),
        (18.75, 0.091),
        (18.85, 0.046),
        (18.95, 0.093),
        (19.05, 0.236),
        (19.15, 0.048),
        (19.25, 0.145),
        (19.35, 0.049),
        (19.45, 0.000),
        (19.55, 0.050),
        (19.65, 0.000),
        (19.75, 0.101),
        (19.85, 0.205),
        (19.95, 0.155),
        (20.05, 0.052),
        (20.250, 0.034),
        (21.750, 0.029),
        (23.250, 0.036),
        (24.750, 0.033),
        (26.250, 0.048),
        (27.750, 0.051),
        (29.250, 0.033),
        (30.750, 0.033),
        (32.250, 0.089),
        (33.750, 0.084),
        (35.250, 0.068),
        (36.750, 0.089),
        (38.250, 0.017),
        (39.750, 0.049),
        (41.250, 0.093),
        (42.750, 0.043),
        (44.250, 0.069),
        (45.750, 0.049),
        (47.250, 0.096),
        (48.750, 0.065),
        (50.250, 0.098),
        (51.750, 0.042),
        (53.250, 0.088),
        (54.750, 0.105),
        (56.250, 0.074),
        (57.750, 0.065),
        (59.250, 0.096),
        (60.750, 0.129),
        (62.250, 0.075),
        (63.750, 0.142),
        (65.250, 0.116),
        (66.750, 0.035),
        (68.250, 0.091),
        (69.750, 0.170),
        (71.250, 0.119),
        (72.750, 0.082),
        (74.250, 0.086),
        (75.750, 0.223),
        (77.250, 0.163),
        (78.750, 0.097),
        (80.250, 0.175),
        (81.750, 0.182),
        (83.250, 0.108),
        (84.750, 0.196),
        (86.250, 0.145),
        (87.750, 0.090),
        (89.250, 0.372),
        (90.750, 0.224),
        (92.250, 0.132),
        (93.750, 0.171),
        (95.250, 0.141),
        (96.750, 0.146),
        (98.250, 0.225),
        (99.750, 0.503),
        (101.250, 0.199),
        (102.750, 0.123),
        (104.250, 0.169),
        (105.750, 0.174),
        (107.250, 0.224),
        (108.750, 0.368),
        (110.250, 0.284),
        (111.750, 0.243),
        (113.250, 0.150),
        (114.750, 0.256),
        (116.250, 0.263),
        (117.750, 0.540),
        (119.250, 0.332),
        (120.750, 0.567),
        (122.250, 0.407),
        (123.750, 0.477),
        (125.250, 0.122),
        (126.750, 0.313),
        (128.250, 0.384),
        (129.750, 0.328),
        (131.250, 0.335),
        (132.750, 0.411),
        (134.250, 0.351),
        (135.750, 0.574),
        (137.250, 0.367),
        (138.750, 0.150),
        (140.250, 0.536),
        (141.750, 0.391),
        (143.250, 0.479),
        (144.750, 0.489),
        (146.250, 0.333),
        (147.750, 0.510),
        (149.250, 0.520),
        (150.750, 0.796),
        (152.250, 0.090),
        (153.750, 0.092),
        (155.250, 0.563),
        (156.750, 0.478),
        (158.250, 0.487),
        (159.750, 0.298),
        (161.250, 0.405),
        (162.750, 0.206),
        (164.250, 0.735),
        (165.750, 0.428),
        (167.250, 0.653),
        (168.750, 0.332),
        (170.250, 0.113),
        (171.750, 0.459),
        (173.250, 0.117),
        ])