【问题标题】:Why can't seaborn.pairplot finish drawing this plot?为什么 seaborn.pairplot 无法完成绘制此图?
【发布时间】:2021-05-04 17:47:13
【问题描述】:

我有一个数据框central

然后我想用sns.pairplot(central) 绘制列之间的成对关系。你能解释一下为什么这个过程永远运行吗?我在笔记本电脑和 Colab 上都试过了,但问题仍然存在。

import urllib3
%matplotlib inline
%config InlineBackend.figure_format = 'svg' # Change the image format to svg for better quality

import networkx as nx
import pandas as pd
import seaborn as sns
from scipy import stats
import matplotlib.pyplot as plt

## Import dataset
http = urllib3.PoolManager()
url = 'https://raw.githubusercontent.com/leanhdung1994/WebMining/main/airports.net'
f = http.request('GET', url)
open('airports.net', 'wb').write(f.data)
G = nx.read_pajek('airports.net', encoding = 'UTF-8')
G = nx.DiGraph(G)

## Compute measures of centrality
degree_central = nx.degree_centrality(G)
closeness_central = nx.closeness_centrality(G)
eigen_central = nx.eigenvector_centrality_numpy(G, max_iter = 200)
katz_central = nx.katz_centrality_numpy(G)
between_central = nx.betweenness_centrality(G)
pagerank = nx.pagerank_numpy(G)
[hub, authority] = nx.hits(G)

##  Create a dataframe using with above calculated centralities
central = pd.DataFrame([degree_central, closeness_central, eigen_central, katz_central, between_central, hub, authority]).T
central.columns = ['degree', 'closeness', 'eigen', 'katz', 'between', 'hub', 'authority']
central

## Plot the pairwise relationships between centralities
sns.pairplot(central)

【问题讨论】:

  • 问题是由列eigen_central引起的,但我不知道这个问题的本质是什么。与所有其他列一样,该列包含 float64 值。它不包含 NaN 或 Inf 值。它是唯一包含负值的列,但这不应该是问题。当您向其中添加另一列时问题消失,但如果添加浮点值则不会。也许是一些棘手的 numpy 类型问题?
  • 有关此问题的更多信息:这不是此列的特定值,正如我一度怀疑的那样。您可以为任何n 绘制sns.pairplot(central.iloc[n:n+150, :]),但不能为sns.pairplot(central.iloc[n:n+200, :]) 绘制。不过,eigen_central 的直方图看起来很奇怪,并且此列的孤立histplot 也不起作用,所以看起来这是主要问题。我关注了这个问题并希望其他人可以阐明这种行为。

标签: python-3.x pandas matplotlib seaborn networkx


【解决方案1】:

由于我不知道的原因,eigen_central 列的 histplot 在确定合理数量的 bin 时出现问题。 pairplot 与对角线 sns.pairplot(central, diag_kind="kde") 中的 kde 图一起使用,而单独的列 eigen_central 的 histplot 也无法按预期工作。您可以通过定义 bin 编号来解决此问题:

sns.pairplot(central, diag_kws = {"bins": 10})

输出:

我会支持任何可以解释为什么 seaborn 在定义垃圾箱时遇到问题的答案。这个问题是 seaborn 特有的,因为 plt.hist(central.eigen) 可以正常工作,但 sns.histplot(central.eigen) 不能正常工作。

【讨论】:

  • 非常感谢您为消除我的困惑所做的努力。我想知道eigen_central 列的非常小的值是否是问题的原因。例如,该列包含-3.574483e-181.179547e-17
  • 不太可能。这些数据的分布可能会影响 seaborn 的 bin 计算。希望@mwaskom 出现在这里并启发我们。
  • 这是一个未解决的问题,与 numpy 提出太多垃圾箱有关:github.com/mwaskom/seaborn/issues/2325
  • @JohanC 这只发生在某些数据集上。是否知道失败数据集的特定属性?
  • 可能涉及到很多不同的量级。例如sns.histplot(10**np.random.uniform(-20, -4, 1000)) as np.histogram_bin_edges(10**np.random.uniform(-20, -4, 1000), 'auto').size 给出大于 20000 的大小。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-19
  • 1970-01-01
  • 2013-10-14
  • 2021-08-28
相关资源
最近更新 更多