matplotlib - 使用 matplotlib 具有等高线热叠加的散点图
问题描述
我有大量数据,其中包括 >200k 的值。
目标是显示数据的分布和粗略趋势,而不是单个点。我想添加一个轮廓热覆盖,类似于这个问题,但使用 matplotlib,并带有指定轮廓数量及其间隔的选项:
https://stats.stackexchange.com/questions/31726/scatterplot-with-contour-heat-overlay
我希望这很清楚。如果没有,请告诉我!
解决方案
Seaborn 的 kdeplot 将允许您仅绘制阴影而不是绘制所有数据点。可以通过指定 n_levels 参数来调整轮廓的数量。对于覆盖关节图和 kdeplot 的较小数据集,允许显示数据点和等高线。人物美学可以有很大的不同,因此我在下面编译了一些不同的选项。带有联合图的子图是使用 ImportanceOfBeingErnest 对这个问题的回答生成的。
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
mean = [0, 0]
cov = [[50, 0], [0, 100]]
x, y = np.random.multivariate_normal(mean, cov, 5000).T
# For large data sets
plt.subplots(1, 2)
plt.subplot(1, 2, 1)
cmap = sns.cubehelix_palette(as_cmap=True, dark=0, light=1, reverse=True)
a = sns.kdeplot(x, y, n_levels=20, shade=True, cmap=cmap, gridsize=200, cbar=True)
a.set_xlabel('x')
a.set_ylabel('y')
plt.subplot(1, 2, 2)
b = sns.kdeplot(x, y, n_levels=60, shade=True, gridsize=100, cmap="RdBu_r", cbar=True)
b.set_xlabel('x')
b.set_ylabel('y')
# For smaller data sets - jointplot and kdeplot overlayed.
# Plain overlay
c = (sns.jointplot(x, y, color="g", marker='.').plot_joint(sns.kdeplot, n_levels=20)).set_axis_labels('x', 'y')
# with shade=True
d = (sns.jointplot(x, y, color="g").plot_joint(sns.kdeplot, n_levels=20, shade=True)).set_axis_labels('x', 'y')
# with transparency control, alpha=
e = (sns.jointplot(x, y, color="g", marker='.').plot_joint(sns.kdeplot, n_levels=20, shade=True, alpha=0.5)).set_axis_labels('x', 'y')
# diverging colour palette - blue to red
f = (sns.jointplot(x, y, marker='.').plot_joint(sns.kdeplot, n_levels=20, cmap="RdBu_r")).set_axis_labels('x', 'y')
# diverging colour palette and shade and reg
g = (sns.jointplot(x, y, marker='.', kind='reg').plot_joint(sns.kdeplot, n_levels=20, cmap="RdBu_r")).set_axis_labels('x', 'y')
# diverging colour palette, shade,
h = (sns.jointplot(x, y).plot_joint(sns.kdeplot, n_levels=20, shade=True, cmap="RdBu_r")).set_axis_labels('x', 'y')
推荐阅读
- angular - (更改)仅在角 2 中按 enter 后才能工作
- django - django manytomany访问相关列
- sql - 如果没有明确指定,liquibase 如何选择模式?
- ocaml - 如何将模块映射到 OCaml 中的模块类型?
- c# - 如果先前的操作超时,EF SqlQuery 将抛出不支持的 MultipleActiveResultSets 异常
- javascript - 即时修改 .serialize() 的结果
- selenium - Selenium 和 Firefox 的可重现截图
- c# - 计算总和 - Datagridview 的 CellFormating 事件性能太慢
- jquery-select2 - Select2 刷新丢失所有类?更改单个选项文本
- elasticsearch - 发现 Elasticsearch 的历史趋势(非可视化)