GVKun编程网logo

如何在Seaborn中叠加两个图形?(seaborn多个图一起输出)

37

如果您对如何在Seaborn中叠加两个图形?和seaborn多个图一起输出感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解如何在Seaborn中叠加两个图形?的各种细节,并对seaborn多个图

如果您对如何在Seaborn中叠加两个图形?seaborn多个图一起输出感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解如何在Seaborn中叠加两个图形?的各种细节,并对seaborn多个图一起输出进行深入的分析,此外还有关于matplotlib – 如何在Seaborn boxplot中加宽盒子?、python – 在seaborn中使用distplot的FutureWarning、python – 在seaborn中绘制两列dataFrame、python – 如何在seaborn分布图中填充曲线下面积的实用技巧。

本文目录一览:

如何在Seaborn中叠加两个图形?(seaborn多个图一起输出)

如何在Seaborn中叠加两个图形?(seaborn多个图一起输出)

如何在Seaborn中叠加两个图形?我的数据中有两列,我希望它们在同一图中。如何保存两个图形的标签。

答案1

小编典典

在单个轴上运行的seaborn函数可以作为一个参数。

例如,文档seaborn.kdeplot包括:

ax : matplotlib axis, optional    Axis to plot on, otherwise uses current axis

因此,如果您这样做:

df = function_to_load_my_data()fig, ax = plt.subplots()

然后,您可以执行以下操作:

seaborn.kdeplot(df[''col1''], ax=ax)seaborn.kdeplot(df[''col2''], ax=ax)

matplotlib – 如何在Seaborn boxplot中加宽盒子?

matplotlib – 如何在Seaborn boxplot中加宽盒子?

我正在尝试使用Seaborn( Reference)制作一个分组的盒子图,盒子都非常窄 – 太窄而无法看到分组颜色.

g = seaborn.factorplot("project_code",y="num_mutations",hue="organ",data=grouped_donor,kind="Box",aspect=3)

如果我放大或者将图形拉伸几倍于我的屏幕宽度,我可以看到这些框,但显然这不是一个标准图形.

这似乎是我的数据量的函数;如果我只绘制前500点(6000点),我会得到可见但小的盒子.它可能特别是我的数据的高差异的函数;根据matplotlib Boxplot文档,

The default [width] is 0.5,or 0.15x(distance between extreme positions) if that is smaller.

无论是什么原因,如果我可以加宽它们,那么图表本身就有足够的空间用于更宽的盒子.

不幸的是,控制框宽度的Boxplot关键字宽度不是有效的factorplot关键字,我找不到matplotlib函数,它会改变绘图函数本身之外的条形图或框的宽度.我甚至找不到任何人讨论这件事;我找到的最接近的是Boxplot线宽.有什么建议?

解决方法

为了将来的参考,以下是使用图例制作正确图形的相关代码位:(显然这是缺少重要的东西,实际上不会按原样运行,但希望它显示了棘手的部分)

import matplotlib.pylab as pyp
import seaborn as sns

def custom_legend(colors,labels,legend_location = 'upper left',legend_boundary = (1,1)):
    # Create custom legend for colors
    recs = []
    for i in range(0,len(colors)):
        recs.append(mpatches.Rectangle((0,0),1,fc=colors[i]))
    pyp.legend(recs,loc=legend_location,bBox_to_anchor=legend_boundary)

# Color Boxplots by organ
organ_list = sorted(df_unique(grouped_samples,'type'))
colors = sns.color_palette("Paired",len(organ_list))
color_dict = dict(zip(organ_list,colors))
organ_palette = grouped_samples.drop_duplicates('id')['type'].map(color_dict)

# Plot grouped Boxplot
g = sns.factorplot("id","num_mutations",data=grouped_samples,order=id_list,size=7,aspect=3,palette=organ_palette)
sns.despine(left=True)
plot_setup_pre()
pyp.yscale('log')
custom_legend(colors,organ_list)

python – 在seaborn中使用distplot的FutureWarning

python – 在seaborn中使用distplot的FutureWarning

参见英文答案 > FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated use `arr[tuple(seq)]`                                    4个
每当我尝试使用seaborn的distplot时,我都会出现这个警告,我似乎无法弄清楚我做错了什么,对不起,如果这很简单的话.

警告:

FutureWarning: Using a non-tuple sequence for multidimensional
indexing is deprecated; use arr[tuple(seq)] instead of arr[seq].
In the future this will be interpreted as an array index,
arr[np.array(seq)],which will result either in an error or a
different result. return np.add.reduce(sorted[indexer] * weights,
axis=axis) / sumval

这是一个可重复的例子:

import numpy as np 
import pandas as pd 
import random

import seaborn as sns

kde_data = np.random.normal(loc=0.0,scale=1,size=100) # fake data
kde_data = pd.DataFrame(kde_data)
kde_data.columns = ["value"]
#kde_data.head()

现在,情节是正确的,但我继续得到上面的警告并使用arr [tuple(seq)]而不是arr [seq]对我没什么帮助.

sns.distplot(kde_data.value,hist=False,kde=True)

enter image description here

我正在研究Jupyter,这是模块版本:

seaborn==0.9.0
scipy==1.1.0
pandas==0.23.0
numpy==1.15.4

解决方法

你没有做错任何事.目前除了可能的 suppressing it之外,没有办法摆脱这个警告.

这告诉你的是seaborn使用scipy函数,由于最近numpy版本的更改,将来会改变行为.我期望在这里发生的是,在未来的scipy版本中,该功能将被改变以适应任何过去未来的numpy版本.在此之前,您可能只是决定接受警告.它不会以任何方式恶化绘图结果.

python – 在seaborn中绘制两列dataFrame

python – 在seaborn中绘制两列dataFrame

我正在尝试在seaborn中创建一个条形图,显示数据框中每行(因子)的两个变量(权重,方差)的值.这是我的数据:

Factor    Weight  Variance
    Growth    10%      0.15
    Value     20%      0.35

这是我的代码:

fig=plt.figure(figsize=(10,10))
    ax1=fig.add_subplot(221)
    sns.barplot(x=df.index,y=df[['Weight','Variance']],ax=ax1)

每次我无法调试时,上面会抛出一个错误.我想要实现的是有一个图,每个因子显示两个彩色条;一种颜色的重量(例如:红色)和另一种颜色的差异(例如:蓝色).

有人有建议或潜在的解决方法吗?

谢谢

解决方法

除了将数据清理为整洁的格式之外,还需要将文本数据(百分比)重新格式化为数字数据类型.由于这与条形图无关,我假设您可以自己处理并专注于绘图和数据结构:

df = pandas.DataFrame({
    'Factor': ['Growth','Value'],'Weight': [0.10,0.20],'Variance': [0.15,0.35]
})
fig,ax1 = pyplot.subplots(figsize=(10,10))
tidy = (
    df.set_index('Factor')
      .stack()  # un-pivots the data 
      .reset_index()  # moves all data out of the index
      .rename(columns={'level_1': 'Variable',0: 'Value'})
)
seaborn.barplot(x='Factor',y='Value',hue='Variable',data=tidy,ax=ax1)
seaborn.despine(fig)

enter image description here

python – 如何在seaborn分布图中填充曲线下面积

python – 如何在seaborn分布图中填充曲线下面积

我有两个变量

x = [1.883830,7.692308,8.791209,9.262166]
y = [5.337520,4.866562,2.825746,6.122449]

而且我想使用包裹在matplotlib中的seaborn来拟合高斯分布.似乎sns.distplot函数是执行此操作的最佳方法,但我无法弄清楚如何填充曲线下方的区域.救命?

fig,ax = plt.subplots(1)
sns.distplot(x,kde_kws={"shade":True},kde=False,fit=stats.gamma,hist=None,color="red",label="2016",fit_kws={'color':'red'});
sns.distplot(y,color="blue",label="2017",fit_kws={'color':'blue'})

我认为“阴影”参数可能是fit_kws参数的一部分,但我没有得到这个.

另一种选择是使用ax.fill()?

解决方法

是的,fit_kws不支持shade参数,与kde_kws不同.但是正如您猜测的那样,我们可以使用ax.fill_between()填充两条曲线下的区域.我们必须从ax对象及其x-y数据中获取线,然后使用它来填充曲线下的区域.这是一个例子.

import numpy as np
import seaborn as sns
import scipy.stats as stats
import matplotlib.pyplot as plt

x = [1.883830,6.122449]
ax = sns.distplot(x,fit_kws={"color":"red"},label="label 1");
ax = sns.distplot(y,fit_kws={"color":"blue"},label="label 2");

# Get the two lines from the axes to generate shading
l1 = ax.lines[0]
l2 = ax.lines[1]

# Get the xy data from the lines so that we can shade
x1 = l1.get_xydata()[:,0]
y1 = l1.get_xydata()[:,1]
x2 = l2.get_xydata()[:,0]
y2 = l2.get_xydata()[:,1]
ax.fill_between(x1,y1,alpha=0.3)
ax.fill_between(x2,y2,alpha=0.3)

plt.show(block=False)

结果如下所示:

enter image description here

关于如何在Seaborn中叠加两个图形?seaborn多个图一起输出的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于matplotlib – 如何在Seaborn boxplot中加宽盒子?、python – 在seaborn中使用distplot的FutureWarning、python – 在seaborn中绘制两列dataFrame、python – 如何在seaborn分布图中填充曲线下面积的相关信息,请在本站寻找。

本文标签: