【问题标题】:Combine different seaborn distribution plots结合不同的seaborn分布图
【发布时间】:2021-07-27 10:23:42
【问题描述】:

我在 Python3 中使用 seaborn 和 matplotlib 来可视化两个不同数组的分布。我使用的代码是:

# Create two matrices (can be 'n' dimensional)-
x = np.random.normal(size = (5, 5))
y = np.random.normal(size = (5, 5))

# On using seaborn, it creates two different plots-
sns.displot(data = x.flatten(), label = 'x')
sns.displot(data = y.flatten(), label = 'y')

plt.legend(loc = 'best')
plt.show()

# Whereas, matplotlib merges these two distributions into one plot-
plt.hist(x = x.flatten(), label = 'x')
plt.hist(x = y.flatten(), label = 'y')

plt.legend(loc = 'best')
plt.show()

如何获得将 matplotlib 中实现的这两个分布合并到 seaborn 中的结果?

谢谢!

【问题讨论】:

  • displot 是一个图形级别的函数,用一个或多个子图创建自己的图形。您可以改为使用坐标轴级别 histplot

标签: python-3.x matplotlib seaborn distribution


【解决方案1】:

首先将您的数据合并到 pandas.DataFrame 中,然后使用 displot:

import pandas as pd
df = pd.DataFrame({'x': x.flatten(), 'y': y.flatten()})
sns.displot(data=df)

或直接使用字典:

sns.displot(data={'x': x.flatten(), 'y': y.flatten()})

【讨论】:

  • 您实际上不需要在这里使用 DataFrame ... 将字典传递给 displot 也可以。
猜你喜欢
  • 1970-01-01
  • 2018-03-11
  • 1970-01-01
  • 1970-01-01
  • 2016-10-18
  • 2016-06-25
  • 2019-12-31
  • 2021-11-03
  • 1970-01-01
相关资源
最近更新 更多