气泡积加权2D散点图数据到目/格

时间:2019-02-02 21:19:14

标签: python arrays matplotlib plot scatter-plot

背景

我有一组二维的“原始”数据点,以数组的形式表示,即:

T[]

数据表示2D空间中的圆形“质量分布”图。 x-y坐标表示数据点的x-y分布,每个x-y坐标上的值是在该数据点处测得的质量/强度。

我想仅使用Python (only at the integral x-y intersections)来绘制此数据,就像下面的图一样,但是使用我自己的xy散点图数据,而不是绘制2D线/功能。

enter image description here

此外,我想结合"specify dot size" logic from another SO question,它允许我指定“每采样/值”的基础上的点的大小,即:

enter image description here


问题

如何合并上述逻辑来呈现数据集,如下所示:

[[0, 0, 1, 0, 0],
 [0, 1, 2, 1, 0],
 [1, 2, 4, 2, 1],
 [0, 1, 2, 1, 0],
 [0, 0, 1, 0, 0]]

类似这样,通过[[0, 0, 1, 0, 0], [0, 1, 2, 1, 0], [1, 2, 4, 2, 1], [0, 1, 2, 1, 0], [0, 0, 1, 0, 0]] / matplotlib(离散域,离散范围,连续值): enter image description here


额外

How can I re-use the above data set to generate a heatmap of the same data (i.e. continuous-domain, continuous-range, continuous valued)?

enter image description here

或者,更是这样的:

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以先创建一个meshgrid来定义x和y坐标,然后使用data数组来定义点的大小。的项,其是0将不被显示,因为0的大小。我使用100的缩放比例来放大点。

完整的工作代码:


import numpy as np
import matplotlib.pyplot as plt

data = np.array([[0, 0, 1, 0, 0],
 [0, 1, 2, 1, 0],
 [1, 2, 4, 2, 1],
 [0, 1, 2, 1, 0],
 [0, 0, 1, 0, 0]])

mesh = np.arange(len(data))
x, y = np.meshgrid(mesh, mesh)
plt.scatter(x, y, s=data*100)
plt.xticks(range(len(data))) # To put ticks at integer values
plt.yticks(range(len(data))) # To put ticks at integer values
plt.show()

enter image description here

<强>生成的热图

import numpy as np
import matplotlib.cm as cm
import matplotlib.pyplot as plt

# data here

x, y = np.meshgrid(np.arange(len(data)), np.arange(len(data)))
plt.scatter(x, y, s=data*100, c=data, cmap=cm.Oranges)
plt.xticks(range(len(data)))
plt.yticks(range(len(data)))

enter image description here