I have points in sns.lineplot but I want a smooth line between them instead of a broken…
- User
I have points in sns.lineplot but I want a smooth line between them instead of a broken line, how to do that?
- Assistant
You can use the ci parameter to specify the confidence interval to draw transparent bands around the line. By setting ci=None, you can remove the bands and get a smooth line: import seaborn as sns import matplotlib.pyplot as plt
Sample data
x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10]
sns.lineplot(x=x, y=y, ci=None) plt.show() Copied
- User
I want a smooth line that moves through my points instead of a broken line
- Assistant
You can use the kind parameter to specify the kind of plot to be drawn. By setting kind='spline', you can get a smooth line that moves through your points: import seaborn as sns import matplotlib.pyplot as plt
Sample data
x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10]
sns.lineplot(x=x, y=y, kind='spline') plt.show() Copied
- User
When I do that I get: AttributeError: Line2D.set() got an unexpected keyword argument 'kind'
- Assistant
The error message indicates that you are passing the kind parameter to the wrong function. The kind parameter should be passed to the sns.lineplot() function, not to the plt.set() function. Here is the corrected code: import seaborn as sns import matplotlib.pyplot as plt
Sample data
x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10]
sns.lineplot(x=x, y=y, kind='spline') plt.show() Copied
- User
A better answer: 3
In order to interpolate, you can use scipy.interpolate.interp1d, which supports many different methods:
‘linear’, ‘nearest’, ‘nearest-up’, ‘zero’, ‘slinear’, ‘quadratic’, ‘cubic’, ‘previous’, or ‘next’
In order to reproduce the plot you provided, here is an example of code:
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()
- Assistant
The code you provided demonstrates the use of different interpolation methods to create a smooth line through given data points. It imports the necessary libraries, defines the x and y data, and then iterates through a list of interpolation methods. For each method, it adds an offset to the y-values to separate the lines, interpolates the data using interp1d, and plots the interpolated line and the original data points. The legend is positioned outside the plot, and the layout is adjusted for better visualization.
- User
Given two dfs. Add to one df only the columns that are not on the second
- Assistant
You can use the set difference to find the columns that are in df1 but not in df2, and then use loc to select those columns: import pandas as pd
Sample dataframes
df1 = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}) df2 = pd.DataFrame({'B': [10, 11, 12], 'D': [13, 14, 15]})
Find columns in df1 that are not in df2
cols_to_add = set(df1.columns) - set(df2.columns)
Add columns from df1 to df2
df2[list(cols_to_add)] = df1.loc[:, list(cols_to_add)]
print(df2) Copied