我是新手seaborn,我有这个数据集,想要创建一个像这样的图表,但是使用seaborn.
这是我的数据:
max_depth = [ 3, 3, 3, 3, 3, 5, 5, 5, 5, 5, 7, 7, 7, 7, 7, 10, 10,
10, 10, 10, 12, 12, 12, 12, 12]
min_samples_split = [2, 5, 15, 20, 25, 2, 5, 15, 20, 25, 2, 5,
15, 20, 25, 2, 5, 15, 20, 25, 2, 5, 15, 20, 25]
test_score = [0.85089537, 0.85089537, 0.85089537, 0.85348114, 0.85354819, 0.87357118, 0.87328475, 0.87147859, 0.87425471, 0.87402261,
0.86355856, 0.86120602, 0.87259394, 0.87582926, 0.87943536, 0.80913078, 0.82786446, …Run Code Online (Sandbox Code Playgroud) 我正在尝试以一种很好的方式显示我的数据,例如在seaborn文档中看到的:

我不太确定如何继续。我设法获得了点的值及其各自的标准差,但它看起来很分散,而我只想显示一种趋势:

这是我玩的:
Final_array = Mean Std
0 0.739269 0.157892
1 0.807382 0.160464
2 0.800024 0.137239
3 0.825854 0.132472
4 0.864854 0.070544
.. ... ...
95 0.797202 0.101961
96 0.747578 0.143394
97 0.751472 0.158651
98 0.587009 0.198987
99 0.728447 0.104601
sns.set(style="darkgrid", palette="muted", color_codes=True)
fig, ax = plt.subplots(figsize=(7,5))
y_pos = np.arange(Final_array.shape[0])
ax.errorbar(y_pos, Final_array[:,0], yerr=Final_array[:,1], elinewidth=0.5)
plt.show()
Run Code Online (Sandbox Code Playgroud)
有人有想法吗?我在使用绘图方面非常初学者。可以平滑吗?并获得像seaborn图像中那样漂亮的叠加层而不是误差线?
这些可能是愚蠢的问题。
亲切的问候,
简单的问题,我有一个在 row_colors 中包含变量“age_range”的聚类图,我也想将变量“education”添加为 row_color。我有以下工作代码:
agerange = df_cor_small.pop("agerange")
lut = dict(zip(agerange.unique(), "rbg"))
row_colors = agerange.map(lut)
ax = sns.clustermap(df_cor_small, cmap='YlGnBu', row_colors=row_colors, figsize=(15,100), cbar_pos=(1.05, .2, .03, .4))
Run Code Online (Sandbox Code Playgroud)
(目前 df_cor_small 不包含变量“education”,但一旦我知道如何实现它,它就会包含在内,因此它将像“agerange”一样可弹出)
有什么建议我可以如何实现这个吗?
我正在尝试使用 sns 绘制多线图,但仅将美国线保持为红色,而其他国家/地区为灰色
这是我到目前为止所拥有的:
df = px.data.gapminder()
sns.lineplot(x = 'year', y = 'pop', data = df, hue = 'country', color = 'grey', dashes = False, legend = False)
Run Code Online (Sandbox Code Playgroud)
但这不会将线条更改为灰色。我想在这之后,我可以单独添加红色的美国线......
设置y=[0.7,0.2,0.1,0.05]和x=[1,2,3,4],如何在 seaborn 中创建条形图/条形图,其中高度由 iey中给出的标签指定x,第一个条形的高度为 0.7,第二个条形的高度为 0.2 等,就像matplotlib
plt.bar(x=[1,2,3,4],height=y)
我运行了多重回归并将系数和标准误差存储到数据框中,如下所示:
我想制作一个图表来显示每个组的系数如何随时间变化,如下所示:
import matplotlib.pyplot as plt
import seaborn as sns
plt.figure(figsize=(14,8))
sns.set(style= "whitegrid")
sns.lineplot(x="time", y="coef",
hue="group",
data=eventstudy)
plt.axhline(y=0 , color='r', linestyle='--')
plt.legend(bbox_to_anchor=(1, 1), loc=2)
plt.show
plt.savefig('eventstudygraph.png')
Run Code Online (Sandbox Code Playgroud)
但我想包含使用主数据集中的“stderr”数据的错误栏。我想我可以使用“plt.errorbar”来做到这一点。但似乎无法弄清楚如何使其发挥作用。目前,我尝试添加 'plt.errorbar 行并尝试不同的迭代:
import matplotlib.pyplot as plt
import seaborn as sns
plt.figure(figsize=(14,8))
sns.set(style= "whitegrid")
sns.lineplot(x="time", y="coef",
hue="group",
data=eventstudy)
plt.axhline(y=0 , color='r', linestyle='--')
plt.errorbar("time", "coef", xerr="stderr", data=eventstudy)
plt.legend(bbox_to_anchor=(1, 1), loc=2)
plt.show
plt.savefig('eventstudygraph.png')
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,它似乎正在图表中创建自己的组/线。我想如果我只有一组,我会知道如何使用“plt.errorbar”,但我不知道如何让它适用于 3 个组。有没有某种方法可以制作 3 个版本的“plt.errorbar”,以便我可以分别为每个组创建误差线?或者有更简单的东西吗?
使用时sns.pairplot我有这个:
import seaborn as sns
iris = sns.load_dataset("iris")
g = sns.pairplot(iris,
markers="+",
kind='reg',
diag_kind="kde",
plot_kws={'line_kws':{'color':'#aec6cf'},
'scatter_kws': {'alpha': 0.5,
'color': '#82ad32'}},
corner=True)
Run Code Online (Sandbox Code Playgroud)
没有 kde 颜色的配对图:
但我需要更改绘图的对角线颜色,但是当我尝试时diag_kws,出现以下错误:
import seaborn as sns
iris = sns.load_dataset("iris")
g = sns.pairplot(iris,
markers="+",
kind='reg',
diag_kind="kde",
plot_kws={'line_kws':{'color':'#aec6cf'},
'scatter_kws': {'alpha': 0.5,
'color': '#82ad32'},
'diag_kws': {'color': '#82ad32'}},
corner=True)
TypeError: regplot() got an unexpected keyword argument 'diag_kws'
Run Code Online (Sandbox Code Playgroud) 您好,我刚刚为seaborn 热图创建了自定义cmap,但是当我想使用它时,它没有显示正确的颜色。我已经一步一步完成了:
import seaborn as sns
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
matrix = np.array([[149030, 34],[7442, 12]])
norm = matplotlib.colors.Normalize(matrix.min(), matrix.max())
boundaries = [value for value in matrix.flatten().tolist()]
list.sort(boundaries)
colors = [[norm(boundaries[0]), "#90AFC5"],
[norm(boundaries[1]), "#336B87"],
[norm(boundaries[2]), "#2a3132"],
[norm(boundaries[3]), "#763626"]]
cmap = matplotlib.colors.LinearSegmentedColormap.from_list("", colors)
fig = plt.figure(figsize=(6, 6))
ax = plt.subplot()
annot = np.array([[f"{matrix[0,0]}", f"{matrix[0,1]}"],
[f"{matrix[1,0]}", f"{matrix[1,1]}"]], dtype=object)
sns.heatmap(matrix,
annot=annot,
annot_kws={"size": 11},
fmt="",
ax=ax,
vmin=matrix.min(),
vmax=matrix.max(),
cmap=cmap,
cbar=True,
cbar_kws={'format': '%.0f%%', 'ticks': boundaries, 'drawedges': True},
xticklabels=False,
yticklabels=False)
Run Code Online (Sandbox Code Playgroud)
如您所见,我的输出有两个蓝色列,但我定义了不同的颜色:

我正在使用 Seaborn 绘制累积分布,它是使用以下代码的 KDE:
sns.distplot(values, bins=20,
hist_kws= {'cumulative': True},
kde_kws= {'cumulative': True} )
Run Code Online (Sandbox Code Playgroud)
这给了我下面的图表:
我想绘制一条垂直线和相应的 x 索引,其中 y 为 0.8。就像是:
如何获取特定 y 的 x 值?
我有如下数据:
#df
df = pd.DataFrame({
'id': {0: -3, 1: 2, 2: -3, 3: 1},
'val': {0: 0.4, 1: 0.03, 2: 0.88, 3: 1.3},
'indicator': {0: 'A', 1: 'A', 2: 'B', 3: 'B'},
'count': {0: 40000, 1: 5779, 2: 3000, 3: 31090}
})
df
Run Code Online (Sandbox Code Playgroud)
如果我执行以下代码,我将得到:
sns.relplot(x = 'id', y = 'val', hue = 'indicator', size = 'count', data = df)
Run Code Online (Sandbox Code Playgroud)
我想要一条线连接这些点。但如果我将绘图更改为线图,我将得到任何图表。
sns.lineplot(x = 'id', y = 'val', hue = 'indicator', size = 'count', data = df)
Run Code Online (Sandbox Code Playgroud)