gpt4 book ai didi

python - Imshow/Matshow 收缩轴内

转载 作者:行者123 更新时间:2023-12-01 07:36:31 25 4
gpt4 key购买 nike

我想绘制一个数字矩阵(通过 matshow/imshow 完成)。然后我想在“轴边界上”绘制一些与轴相关的函数。

但是:我希望 matshow 输出有一个黑色边界框,但似乎我无法使用 edge_colorline_width。其次,这两个命令似乎都“收缩”了结果轴,使得轴+刻度+标签都适合轴内。然而,这使得情节与我想要在边界上绘制的内容不一致。

特别是看下面的图片。 matshow 上方的密度图与 matshow 中的 x 轴 网格具有相同的网格,但并未完全跨越它。此外,它还错过了 matshow 周围的黑框。

如何添加/修复这两个功能?

incorrect output

这是我当前的代码片段:

import numpy as np
from scipy.stats import norm
import matplotlib.pyplot as plt
%matplotlib inline
plt.style.use('ggplot')
nX, nY = 50, 70
realXGrid = np.linspace(-0.5, 0.5, nX)
realYGrid = np.linspace(-1, 1, nY)
XX, YY = np.meshgrid(realXGrid, realYGrid, indexing='ij')
Z = XX * 3 - YY*5

# prepare figure/grid
fig5 = plt.figure()
widths = [1, 0.1, 0.1]
heights = [0.1, 1]
matAx = 3
caxAx = 5
spec5 = fig5.add_gridspec(ncols=3, nrows=2, width_ratios=widths,
height_ratios=heights, hspace=0, wspace=0)
axes = []
for row in range(2):
for col in range(3):
ax = fig5.add_subplot(spec5[row, col])
axes.append(ax)

for i, ax in enumerate(axes):
if i != matAx:
ax.axis('off')
else:
ax.grid(False)
ax.patch.set_edgecolor('black')
ax.patch.set_linewidth('1')

# plot
cax = axes[matAx].imshow(Z, cmap='RdGy', origin='lower')
axes[matAx].axis('image')
fig5.colorbar(cax, ax=axes[caxAx], orientation='vertical', fraction=1)

xDensity = norm.pdf(realXGrid, scale=0.1)
yDensity = norm.pdf(realYGrid, scale=0.001)
axes[0].plot(realXGrid, xDensity)
axes[4].plot(yDensity, realYGrid)
plt.show()

最佳答案

我建议在实际数据坐标中绘制图像。这允许对边际分布图使用相同的限制。
要获得正确的间距,您可以放弃图像的相等长宽比。

import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import norm

nX, nY = 50, 70
realXGrid = np.linspace(-0.5, 0.5, nX)
realYGrid = np.linspace(-1, 1, nY)
XX, YY = np.meshgrid(realXGrid, realYGrid, indexing='ij')
Z = XX * 3 - YY*5

# prepare figure/grid
fig5 = plt.figure()
widths = [1, 0.1, 0.1]
heights = [0.1, 1]
matAx = 3
caxAx = 5
spec5 = fig5.add_gridspec(ncols=3, nrows=2, width_ratios=widths,
height_ratios=heights, hspace=0, wspace=0)
axes = []
for row in range(2):
for col in range(3):
ax = fig5.add_subplot(spec5[row, col])
axes.append(ax)

for i, ax in enumerate(axes):
if i != matAx:
ax.axis('off')
else:
ax.grid(False)
ax.patch.set_edgecolor('black')
ax.patch.set_linewidth('1')

# plot
dx = np.diff(realXGrid)[0]
dy = np.diff(realYGrid)[0]
extent = [realXGrid[0]-dx/2, realXGrid[-1]+dx/2,
realYGrid[0]-dx/2, realYGrid[-1]+dx/2]
im = axes[matAx].imshow(Z, cmap='RdGy', origin='lower', aspect="auto",
extent=extent)
#axes[matAx].axis('image')
fig5.colorbar(im, ax=axes[caxAx], orientation='vertical', fraction=1)

xDensity = norm.pdf(realXGrid, scale=0.1)
yDensity = norm.pdf(realYGrid, scale=0.001)
axes[0].plot(realXGrid, xDensity)
axes[4].plot(yDensity, realYGrid)
axes[0].set_xlim(*extent[:2])
axes[4].set_ylim(*extent[2:])
plt.show()

enter image description here

关于python - Imshow/Matshow 收缩轴内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56971194/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com