pandas Seaborn 在同一个散点图上绘制两个数据集
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51732867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Seaborn plot two data sets on the same scatter plot
提问by Michael Dz
I have 2 data sets in Pandas Dataframe and I want to visualize them on the same scatter plot so I tried:
我在 Pandas Dataframe 中有 2 个数据集,我想在同一个散点图上将它们可视化,所以我尝试了:
import matplotlib.pyplot as plt
import seaborn as sns
sns.pairplot(x_vars=['Std'], y_vars=['ATR'], data=set1, hue='Asset Subclass')
sns.pairplot(x_vars=['Std'], y_vars=['ATR'], data=set2, hue='Asset Subclass')
plt.show()
But all the time I get 2 separate charts instead of a single one
How can I visualize both data sets on the same plot? Also can I have the same legend for both data sets but different colors for the second data set?
但是我总是得到 2 个单独的图表而不是一个单独的图表
如何在同一个图上可视化两个数据集?我也可以对两个数据集使用相同的图例,但对第二个数据集使用不同的颜色吗?
回答by tobsecret
The following should work in the latest version of seaborn
(0.9.0)
以下应该在seaborn
(0.9.0)的最新版本中工作
import matplotlib.pyplot as plt
import seaborn as sns
First we concatenate the two datasets into one and assign a dataset
column which will allow us to preserve the information as to which row is from which dataset.
首先,我们将两个数据集连接成一个并分配一dataset
列,这将允许我们保留关于哪一行来自哪个数据集的信息。
concatenated = pd.concat([set1.assign(dataset='set1'), set2.assign(dataset='set2')])
Then we use the sns.scatterplot
function from the latest seaborn version (0.9.0) and via the style
keyword argument set it so that the markers are based on the dataset
column:
然后我们使用sns.scatterplot
最新的 seaborn 版本 (0.9.0) 中的函数并通过style
关键字参数设置它,以便标记基于dataset
列:
sns.scatterplot(x='Std', y='ATR', data=concatenated,
hue='Asset Subclass', style='dataset')
plt.show()