<p>与前面的建议类似,“移动”矩阵到正区域,然后使用x和y偏移定位坐标:</p>
<pre><code>import numpy as np
data = {(-1.0, 4.0): 1, (2.0, 2.0): 12, (3.0, 1.0): 8}
x_offset = 3
y_offset = 6
mat = np.zeros((17, 15))
for (x, y), z in data.items():
mat[int(y + y_offset), int(x + x_offset)] = z
</code></pre>
<p><strong>编辑</p>
<p>好吧,我想这就是你的想法(我假设在x和y坐标中都有负值):</p>
<p>如果要硬编码x和y值的范围(xlim:-3、13、ylim:-6、8):</p>
<pre><code>x_min, x_max = -3, 13
y_min, y_max = -6, 8
</code></pre>
<p>或根据数据确定:</p>
<pre><code>x_min = min([x for (x, y), z in data.items()])
y_min = min([y for (x, y), z in data.items()])
x_max = max([x for (x, y), z in data.items()])
y_max = max([y for (x, y), z in data.items()])
</code></pre>
<p>然后使用:</p>
<pre><code>x_offset = abs(x_min)
y_offset = abs(y_min)
mat = np.zeros((y_max + y_offset + 1, x_max + x_offset + 1)) # (Row, column) becomes (y, x)
for (x, y), z in data.items():
print(x, y)
mat[int(y + y_offset), int(x + x_offset)] = z
pd.DataFrame(mat, columns=range(x_min, x_max + 1),
index=range(y_min, y_max + 1))
</code></pre>
<p>然后使用以下公式绘图:</p>
<pre><code>plt.imshow(mat, origin='lower', extent=[x_min, x_max + 1,
y_min, y_max + 1])
</code></pre>