PairGrid 与 Seaborn 中的 Hexbin 图
Hexbin plot in PairGrid with Seaborn
我正在尝试在 Seaborn 网格中获取 hexbin 图。我有以下代码,
# Works in Jupyter with Python 2 Kernel.
%matplotlib inline
import seaborn as sns
import matplotlib as mpl
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")
# Borrowed from
def hexbin(x, y, color, **kwargs):
cmap = sns.light_palette(color, as_cmap=True)
plt.hexbin(x, y, gridsize=15, cmap=cmap, extent=[min(x), max(x), min(y), max(y)], **kwargs)
g = sns.PairGrid(tips, hue='sex')
g.map_diag(plt.hist)
g.map_lower(sns.stripplot, jitter=True, alpha=0.5)
g.map_upper(hexbin)
但是,这给了我下面的图像,
如何修复 hexbin 图,使其覆盖图形的整个表面,而不仅仅是显示绘图区域的子集?
您在此处尝试执行的操作存在(至少)三个问题。
stripplot 适用于至少一个轴是分类的数据。在这种情况下,情况并非如此。 Seaborn 猜测 x 轴是分类轴,它弄乱了子图的 x 轴。来自 docs for stripplot:
Draw a scatterplot where one variable is categorical.
在下面建议的代码中,我已将其更改为简单的散点图。
在彼此之上绘制两个六边形图只会显示后一个。我在 hexbin 参数中添加了一些 alpha=0.5
,但结果远非漂亮。
代码中的 extent 参数将 hexbin 图调整为 x
和 y
每个性别 一次 。但是两个 hexbin 图都需要大小相等,所以它们应该使用整个系列的 min/max both 性别。为了实现这一点,我将所有系列的最小值和最大值传递给 hexbin 函数,该函数然后可以选择和使用相关的值。
这是我想出的:
# Works in Jupyter with Python 2 Kernel.
%matplotlib inline
import seaborn as sns
import matplotlib as mpl
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")
# Borrowed from
def hexbin(x, y, color, max_series=None, min_series=None, **kwargs):
cmap = sns.light_palette(color, as_cmap=True)
ax = plt.gca()
xmin, xmax = min_series[x.name], max_series[x.name]
ymin, ymax = min_series[y.name], max_series[y.name]
plt.hexbin(x, y, gridsize=15, cmap=cmap, extent=[xmin, xmax, ymin, ymax], **kwargs)
g = sns.PairGrid(tips, hue='sex')
g.map_diag(plt.hist)
g.map_lower(plt.scatter, alpha=0.5)
g.map_upper(hexbin, min_series=tips.min(), max_series=tips.max(), alpha=0.5)
结果如下:
我正在尝试在 Seaborn 网格中获取 hexbin 图。我有以下代码,
# Works in Jupyter with Python 2 Kernel.
%matplotlib inline
import seaborn as sns
import matplotlib as mpl
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")
# Borrowed from
def hexbin(x, y, color, **kwargs):
cmap = sns.light_palette(color, as_cmap=True)
plt.hexbin(x, y, gridsize=15, cmap=cmap, extent=[min(x), max(x), min(y), max(y)], **kwargs)
g = sns.PairGrid(tips, hue='sex')
g.map_diag(plt.hist)
g.map_lower(sns.stripplot, jitter=True, alpha=0.5)
g.map_upper(hexbin)
但是,这给了我下面的图像,
如何修复 hexbin 图,使其覆盖图形的整个表面,而不仅仅是显示绘图区域的子集?
您在此处尝试执行的操作存在(至少)三个问题。
stripplot 适用于至少一个轴是分类的数据。在这种情况下,情况并非如此。 Seaborn 猜测 x 轴是分类轴,它弄乱了子图的 x 轴。来自 docs for stripplot:
Draw a scatterplot where one variable is categorical.
在下面建议的代码中,我已将其更改为简单的散点图。
在彼此之上绘制两个六边形图只会显示后一个。我在 hexbin 参数中添加了一些
alpha=0.5
,但结果远非漂亮。代码中的 extent 参数将 hexbin 图调整为
x
和y
每个性别 一次 。但是两个 hexbin 图都需要大小相等,所以它们应该使用整个系列的 min/max both 性别。为了实现这一点,我将所有系列的最小值和最大值传递给 hexbin 函数,该函数然后可以选择和使用相关的值。
这是我想出的:
# Works in Jupyter with Python 2 Kernel.
%matplotlib inline
import seaborn as sns
import matplotlib as mpl
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")
# Borrowed from
def hexbin(x, y, color, max_series=None, min_series=None, **kwargs):
cmap = sns.light_palette(color, as_cmap=True)
ax = plt.gca()
xmin, xmax = min_series[x.name], max_series[x.name]
ymin, ymax = min_series[y.name], max_series[y.name]
plt.hexbin(x, y, gridsize=15, cmap=cmap, extent=[xmin, xmax, ymin, ymax], **kwargs)
g = sns.PairGrid(tips, hue='sex')
g.map_diag(plt.hist)
g.map_lower(plt.scatter, alpha=0.5)
g.map_upper(hexbin, min_series=tips.min(), max_series=tips.max(), alpha=0.5)
结果如下: