问题描述
我在Pandas Dataframe中有2个数据集,我想在相同的散点图上可视化它们,所以我尝试了:
I have 2 data sets in Pandas Dataframe and I want to visualize them on the same scatter plot so I tried:
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()
但是,我一直都得到2张独立的图表,而不是一张如何在同一张图上可视化两个数据集?另外,两个数据集可以具有相同的图例,而第二个数据集可以具有不同的颜色吗?
But all the time I get 2 separate charts instead of a single oneHow 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?
推荐答案
以下应该在最新版本的seaborn
(0.9.0)
The following should work in the latest version of seaborn
(0.9.0)
import matplotlib.pyplot as plt
import seaborn as sns
首先,我们将两个数据集合并为一个,并分配一个 dataset
列,这将使我们能够保留有关哪个行来自哪个数据集的信息.
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.
concatenated = pd.concat([set1.assign(dataset='set1'), set2.assign(dataset='set2')])
然后我们使用最新 seaborn 版本 (0.9.0) 中的 sns.scatterplot
函数并通过 style
关键字参数设置它,以便标记基于dataset
列:
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(x='Std', y='ATR', data=concatenated,
hue='Asset Subclass', style='dataset')
plt.show()
这篇关于Seaborn 在同一个散点图上绘制两个数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!