如何在seaborn中插入点
How to interpolate points in seaborn
我正在学习在 Seaborn 中创建图表。现在我有一个时间序列的研究案例。我想在点之间使用不同类型的插值来制作一条线。
所以我的问题是,是否有一些本地方法可以使用 Seaborn 创建内插线?
有没有类似Plotly的line_shape
(hv, vh, hvh, spline, linear)的方法?
在 Plotly 中引用所需的形式:
(如果没有,你会推荐哪个工具来达到同样的效果?)
谢谢!
为了插值,可以使用scipy.interpolate.interp1d
,它支持多种不同的方法:
‘linear’, ‘nearest’, ‘nearest-up’, ‘zero’, ‘slinear’, ‘quadratic’,
‘cubic’, ‘previous’, or ‘next’
为了重现您提供的情节,这里有一个代码示例:
import numpy as np
from scipy.interpolate import interp1d
import matplotlib.pyplot as plt
import seaborn as sns
x = [1, 2, 3, 4, 5]
y = [1, 3, 2, 3, 1]
x_interp = np.linspace(x[0], x[-1], 1000)
methods = ['linear', 'nearest', 'nearest-up', 'zero', 'slinear', 'quadratic', 'cubic', 'previous', 'next']
fig, ax = plt.subplots()
for i, method in enumerate(methods, 0):
y_values = [y_i + 5*i for y_i in y]
y_interp = interp1d(x, y_values, kind = method)(x_interp)
sns.lineplot(x_interp, y_interp, label = method, ax = ax)
sns.scatterplot(x, y_values, ax = ax)
ax.legend(frameon = False, loc = 'upper left', bbox_to_anchor = (1.05, 1))
plt.tight_layout()
plt.show()
在seaborn中选择插值,靠的是Matplotlib。如 https://seaborn.pydata.org/generated/seaborn.lineplot.html
中所述
kwargs key, value mappings
Other keyword arguments are passed down to matplotlib.axes.Axes.plot().
因此您可以传递 drawstyle
参数,它可以让您在线性或多种不同的步进样式之间进行选择。请参阅 https://matplotlib.org/stable/gallery/lines_bars_and_markers/step_demo.html
中的第二个示例
import seaborn as sns
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [1, 3, 2, 3, 1]
dss=['default','steps-pre','steps-mid','steps-post']
fig = plt.figure()
for i,ds in enumerate(dss):
ax = fig.add_subplot(len(dss),1,1+i)
sns.lineplot(x=x,y=y,ax=ax,drawstyle=ds,markers='o')
ax.scatter(x,y)
plt.show()
如您所见,没有样条曲线等平滑器的选项。像这样的曲线平滑插值会添加数据中不存在的信息,您应该始终小心。如果您确实想要添加此类预先存在的知识,请按照其他答案中的建议使用手动插值。
我正在学习在 Seaborn 中创建图表。现在我有一个时间序列的研究案例。我想在点之间使用不同类型的插值来制作一条线。
所以我的问题是,是否有一些本地方法可以使用 Seaborn 创建内插线?
有没有类似Plotly的line_shape
(hv, vh, hvh, spline, linear)的方法?
在 Plotly 中引用所需的形式:
(如果没有,你会推荐哪个工具来达到同样的效果?) 谢谢!
为了插值,可以使用scipy.interpolate.interp1d
,它支持多种不同的方法:
‘linear’, ‘nearest’, ‘nearest-up’, ‘zero’, ‘slinear’, ‘quadratic’, ‘cubic’, ‘previous’, or ‘next’
为了重现您提供的情节,这里有一个代码示例:
import numpy as np
from scipy.interpolate import interp1d
import matplotlib.pyplot as plt
import seaborn as sns
x = [1, 2, 3, 4, 5]
y = [1, 3, 2, 3, 1]
x_interp = np.linspace(x[0], x[-1], 1000)
methods = ['linear', 'nearest', 'nearest-up', 'zero', 'slinear', 'quadratic', 'cubic', 'previous', 'next']
fig, ax = plt.subplots()
for i, method in enumerate(methods, 0):
y_values = [y_i + 5*i for y_i in y]
y_interp = interp1d(x, y_values, kind = method)(x_interp)
sns.lineplot(x_interp, y_interp, label = method, ax = ax)
sns.scatterplot(x, y_values, ax = ax)
ax.legend(frameon = False, loc = 'upper left', bbox_to_anchor = (1.05, 1))
plt.tight_layout()
plt.show()
在seaborn中选择插值,靠的是Matplotlib。如 https://seaborn.pydata.org/generated/seaborn.lineplot.html
中所述kwargs key, value mappings Other keyword arguments are passed down to matplotlib.axes.Axes.plot().
因此您可以传递 drawstyle
参数,它可以让您在线性或多种不同的步进样式之间进行选择。请参阅 https://matplotlib.org/stable/gallery/lines_bars_and_markers/step_demo.html
import seaborn as sns
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [1, 3, 2, 3, 1]
dss=['default','steps-pre','steps-mid','steps-post']
fig = plt.figure()
for i,ds in enumerate(dss):
ax = fig.add_subplot(len(dss),1,1+i)
sns.lineplot(x=x,y=y,ax=ax,drawstyle=ds,markers='o')
ax.scatter(x,y)
plt.show()
如您所见,没有样条曲线等平滑器的选项。像这样的曲线平滑插值会添加数据中不存在的信息,您应该始终小心。如果您确实想要添加此类预先存在的知识,请按照其他答案中的建议使用手动插值。