Smoothing the loss data using matplotlib ,scipy and numpy in python 绘制平滑的曲线

1 篇文章 0 订阅
1 篇文章 0 订阅

The problem:

import numpy as np
import matplotlib.pyplot as plt

x = np.array([1, 2, 3])
y = np.array([0.2572527229785919, 2.5656850993982516e-05, 9.75140892478521e-07])
plt.plot(x, y[0:len(y)], 'r*', label='loss', markersize=9)
for a, b in zip(range(len(y)), y):
    plt.text(a + 1, b, '%.8f' % b, ha='center', va='bottom', fontsize=12)
plt.plot(x, y)

plt.xticks(x)
plt.yscale('log')
plt.grid(True, linestyle='dashed')
plt.xlabel('number of updates')
plt.ylabel('MSE loss with logarithmic scale')
plt.show()

The plot graph from above:

在这里插入图片描述

Solution:

Instead of “make_interp_spline” from sicpy I use “CubicSpline” from scipy.interpolate:

from scipy.interpolate import CubicSpline
import numpy as np
import matplotlib.pyplot as plt

x = np.array([1, 2, 3])
y = np.log10([0.2572527229785919, 2.5656850993982516e-05, 9.75140892478521e-07])
plt.plot(x, y[0:len(y)], 'r*', label='loss', markersize=9)
for a, b in zip(range(len(y)), y):
    plt.text(a + 1, b, '%.8f' % b, ha='center', va='bottom', fontsize=12)

x_smooth =np.linspace(1, 3, 300)
y_smooth = CubicSpline(x, y)(x_smooth)
plt.plot(x_smooth, y_smooth)

plt.xticks(x)

plt.grid(True, linestyle='dashed')
plt.xlabel('number of updates')
plt.ylabel('MSE loss with logarithmic scale')
plt.show()

在这里插入图片描述

Reference:

[1] : https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.CubicSpline.html
[2] : https://python-decompiler.com/article/2011-03/plot-smooth-line-with-pyplot

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值