python - Matplotlib imshow() 给出一个水平翻转的密度图
问题描述
我对绘制双变量正态的混合物很感兴趣。
def func(x):
cat = tfd.Categorical(probs=np.array([.5, .5],dtype=NP_DTYPE))
comps = [tfd.MultivariateNormalDiag(loc=np.array([-5.0, -5.0],dtype=NP_DTYPE), scale_diag=tf.ones(2,dtype=DTYPE)*.1),
tfd.MultivariateNormalDiag(loc=np.array([5.0, 5.0],dtype=NP_DTYPE), scale_diag=tf.ones(2,dtype=DTYPE)*.1)]
mix = tfd.Mixture(cat=cat, components=comps)
return mix.prob(x)
这是两个二元正态的混合。一个中心在 [5,5],另一个中心在 [-5,-5];两者都有对角线协方差矩阵,沿对角线为 0.1。每个具有相同的混合重量。
我的绘图代码是这样的
# make these smaller to increase the resolution
dx, dy = 0.1, 0.1
x = np.arange(-10.0, 10.0, dx)
y = np.arange(-10.0, 10.0, dy)
X, Y = np.meshgrid(x, y)
Z = np.concatenate((X.reshape(-1,1),Y.reshape(-1,1)),axis=1)
extent = np.min(x), np.max(x), np.min(y), np.max(y)
fig = plt.figure(frameon=True)
Z2 = tf.log(func(Z) + 1e-6)
Z2 = sess.run(Z2)
Z2 = Z2.reshape(int(np.sqrt(Z2.shape[0])),int(np.sqrt(Z2.shape[0])))
im2 = plt.imshow(Z2, cmap=plt.cm.viridis, alpha=.9, interpolation='bilinear',
extent=extent)
plt.colorbar()
plt.show()
我扁平化网格的原因是我想实现一个通用的密度绘图,以便它可以绘制任何复杂的二维分布。(任意二维分布,输入形状为 [N,D];N 是点数,D 是每个点的维度)
然而,这给出了一个奇怪的情节
由于高温区域应在 [5,5] 和 [-5,-5] 附近,因此水平翻转了图
任何帮助解决这个问题?(因为 imshow() 是一种黑盒,我需要密度函数采用特定形式的输入;我不知道如何解决这个问题)
解决方案
推荐阅读
- javascript - 在 Scrollview 中查看不显示
- node.js - 节点 ImageMagick 在本地成功但在 AWS Lambda 中失败
- mysql - 加入两个表后计数
- javascript - iife 内部函数需要明确的这个赋值
- tensorflow - 在 model_config_file 中分配版本标签失败
- python - 将 `tf.constant` 与整数进行比较
- c# - mdb文件数据插入增加mdb大小
- r - Concatenate data.frame character columns based on column index stored as a vector in R w/ dplyr mutate()?
- python - Python将图像堆叠成立方体
- c# - 使用 RegEx 在文本 c# 中查找 100$