Astray.R 发表于 2020-7-24 22:09:30

python有关绘制平滑曲线问题


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

x = np.array(['2020-07-15','2020-07-16','2020-07-17','2020-07-20','2020-07-21','2020-07-22','2020-07-23'])
y = np.array()
plt.xlabel("Date");
plt.ylabel("values")
plt.xticks(rotation=45)
plt.plot(x,y)

结果:
感觉很丑,现在想要绘制平滑曲线
from scipy import interpolate
import matplotlib.pyplot as plt
import numpy as np

x = np.array(['2020-07-15','2020-07-16','2020-07-17','2020-07-20','2020-07-21','2020-07-22','2020-07-23'])
y = np.array()
xnew =np.arange(0,10,0.1)
func = interpolate.interp1d(x,y)
ynew = func(xnew)
plt.plot(xnew,ynew)

结果提示:
TypeError: Cannot cast array data from dtype('float64') to dtype('<U32') according to the rule 'safe'


请问 有办法解决吗,或者有别的方法去绘制平滑曲线,网上查了好久都是x轴和y轴都为数字

Astray.R 发表于 2020-7-24 22:10:30

图片是一开始绘制的一个最简单的图形

赚小钱 发表于 2020-7-24 23:10:28

https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.interp1d.html#scipy.interpolate.interp1d

interp1d 的参数 kind
str or int, optional
Specifies the kind of interpolation as a string (‘linear’, ‘nearest’, ‘zero’, ‘slinear’, ‘quadratic’, ‘cubic’, ‘previous’, ‘next’, where ‘zero’, ‘slinear’, ‘quadratic’ and ‘cubic’ refer to a spline interpolation of zeroth, first, second or third order; ‘previous’ and ‘next’ simply return the previous or next value of the point) or as an integer specifying the order of the spline interpolator to use. Default is ‘linear’.

要曲线,至少三次差值(理论上,最多与已知点数量相同)
页: [1]
查看完整版本: python有关绘制平滑曲线问题