使用 matplotlib 绘制二维密度等高线图

GCi*_*ien 7 python numpy contour density-plot

我正在尝试绘制我的数据集xy通过 csv 文件生成numpy.genfromtxt('/Users/.../somedata.csv', delimiter=',', unpack=True))作为简单的密度图。为了确保这是自包含的,我将在这里定义它们:

x = [ 0.2933215   0.2336305   0.2898058   0.2563835   0.1539951   0.1790058
  0.1957057   0.5048573   0.3302402   0.2896122   0.4154893   0.4948401
  0.4688092   0.4404935   0.2901995   0.3793949   0.6343423   0.6786809
  0.5126349   0.4326627   0.2318232   0.538646    0.1351541   0.2044524
  0.3063099   0.2760263   0.1577156   0.2980986   0.2507897   0.1445099
  0.2279241   0.4229934   0.1657194   0.321832    0.2290785   0.2676585
  0.2478505   0.3810182   0.2535708   0.157562    0.1618909   0.2194217
  0.1888698   0.2614876   0.1894155   0.4802076   0.1059326   0.3837571
  0.3609228   0.2827142   0.2705508   0.6498625   0.2392224   0.1541462
  0.4540277   0.1624592   0.160438    0.109423    0.146836    0.4896905
  0.2052707   0.2668798   0.2506224   0.5041728   0.201774    0.14907
  0.21835     0.1609169   0.1609169   0.205676    0.4500787   0.2504743
  0.1906289   0.3447547   0.1223678   0.112275    0.2269951   0.1616036
  0.1532181   0.1940938   0.1457424   0.1094261   0.1636615   0.1622345
  0.705272    0.3158471   0.1416916   0.1290324   0.3139713   0.2422002
  0.1593835   0.08493619  0.08358301  0.09691083  0.2580497   0.1805554 ]

y = [ 1.395807  1.31553   1.333902  1.253527  1.292779  1.10401   1.42933
  1.525589  1.274508  1.16183   1.403394  1.588711  1.346775  1.606438
  1.296017  1.767366  1.460237  1.401834  1.172348  1.341594  1.3845
  1.479691  1.484053  1.468544  1.405156  1.653604  1.648146  1.417261
  1.311939  1.200763  1.647532  1.610222  1.355913  1.538724  1.319192
  1.265142  1.494068  1.268721  1.411822  1.580606  1.622305  1.40986
  1.529142  1.33644   1.37585   1.589704  1.563133  1.753167  1.382264
  1.771445  1.425574  1.374936  1.147079  1.626975  1.351203  1.356176
  1.534271  1.405485  1.266821  1.647927  1.28254   1.529214  1.586097
  1.357731  1.530607  1.307063  1.432288  1.525117  1.525117  1.510123
  1.653006  1.37388   1.247077  1.752948  1.396821  1.578571  1.546904
  1.483029  1.441626  1.750374  1.498266  1.571477  1.659957  1.640285
  1.599326  1.743292  1.225557  1.664379  1.787492  1.364079  1.53362
  1.294213  1.831521  1.19443   1.726312  1.84324 ]
Run Code Online (Sandbox Code Playgroud)

现在,我已经多次尝试使用以下变体来绘制轮廓:

delta = 0.025
OII_OIII_sAGN_sorted = numpy.arange(numpy.min(OII_OIII_sAGN), numpy.max(OII_OIII_sAGN), delta)
Dn4000_sAGN_sorted = numpy.arange(numpy.min(Dn4000_sAGN), numpy.max(Dn4000_sAGN), delta)
OII_OIII_sAGN_X, Dn4000_sAGN_Y = np.meshgrid(OII_OIII_sAGN_sorted, Dn4000_sAGN_sorted)

Z1 = matplotlib.mlab.bivariate_normal(OII_OIII_sAGN_X, Dn4000_sAGN_Y, 1.0, 1.0, 0.0, 0.0)
Z2 = matplotlib.mlab.bivariate_normal(OII_OIII_sAGN_X, Dn4000_sAGN_Y, 0.5, 1.5, 1, 1)
# difference of Gaussians
Z = 0.2 * (Z2 - Z1)
pyplot_middle.contour(OII_OIII_sAGN_X, Dn4000_sAGN_Y, Z, 12, colors='k')
Run Code Online (Sandbox Code Playgroud)

这似乎没有给出所需的输出。我也尝试过:

H, xedges, yedges = np.histogram2d(OII_OIII_sAGN,Dn4000_sAGN)
extent = [xedges[0],xedges[-1],yedges[0],yedges[-1]]
ax.contour(H, extent=extent)
Run Code Online (Sandbox Code Playgroud)

也没有完全按照我想要的方式工作。本质上,我正在寻找与此类似的东西:

在此输入图像描述

如果有人可以帮助我解决这个问题,我将非常感激,无论是建议一种全新的方法还是修改我现有的代码。如果您有一些有用的技术或想法,还请附上您的输出图像。

σηγ*_*σηγ 8

seaborn密度图是开箱即用的吗:

import seaborn as sns
import matplotlib.pyplot as plt

sns.kdeplot(x, y)
plt.show()
Run Code Online (Sandbox Code Playgroud)

绘图


Amy*_*den 6

看来 histogram2d 需要一些摆弄才能在正确的位置绘制轮廓。我对直方图矩阵进行了转置,并且还取了 xedges 和 yedges 中元素的平均值,而不是仅仅从末尾删除一个。

from matplotlib import pyplot as plt
import numpy as np
fig = plt.figure()
h, xedges, yedges = np.histogram2d(x, y, bins=9)
xbins = xedges[:-1] + (xedges[1] - xedges[0]) / 2
ybins = yedges[:-1] + (yedges[1] - yedges[0]) / 2

h = h.T
CS = plt.contour(xbins, ybins, h)
plt.scatter(x, y)
plt.show()
Run Code Online (Sandbox Code Playgroud)

等高线密度图