python - Matplotlib:来自两个数组的高斯等高线图
问题描述
我有两个 numpy 数组,一个形状 (239,2) 包含我的数据点,另一个形状 (239,) 包含这些数据点的二元高斯密度值。
如何使用 Matplotlib 在数据点的散点图上为我的密度函数创建等高线图?
目前我TypeError: Input z must be a 2D array
在尝试使用contour
matplotlib 中的函数时得到一个 , 。如果我有每个数据点的 X 轴、Y 轴和概率密度值,为什么 z 必须是 2D 的?我需要以某种方式进行插值吗?
fig, ax = plt.subplots()
ax.scatter(X[:,0], X[:,1], c='green')
ax.scatter(X[:,0], X[:,1], c='orange')
ax.contour(z, X[:,0], X[:,1]) <-- TypeError happens here
plt.show()
X 的形状为 (239, 2),z 的形状为 (239,)
解决方案
contour
needs z values on a regular 2D grid, perhaps your data is better suitable for the tricontour
method:
from matplotlib.tri import Triangulation
import matplotlib.pyplot as plt
import numpy as np
x = np.random.random((100))
y = np.random.random((100))
z = x * y
tri = Triangulation(x,y)
plt.tricontour(tri, z, )
plt.scatter(x,y, c=z)
plt.show()
推荐阅读
- php - 从 php 中的条带 api 客户数组中获取条带客户数据
- java - 当我在 Android Studio 上的服务器中使用 PrintWriter.println() 时出现致命异常
- flutter - 与实际运行时看到的相比,我的颤振 apk 看起来真的很奇怪?
- sql - 在语句 Oracle SQL 的情况下使用 IN 条件
- windows - 路径变量 Windows 10 - 未找到 Python
- r - make.keys 函数错误 `[<-`(`*tmp*`, abs(list.i), i, value = sign(list.i)) 中的错误:下标越界
- flutter - 是否可以从 Flutter 中的 firebase 集合创建 pdf?
- php - 通过 ftp 创建文件夹时避免特殊字符
- flutter - Flutter:onPressed 在按钮右侧显示文本
- laravel - Laravel Vue SPA Sourcemaps 未显示原始行