问题描述
在python中,如果我有一组数据
In python, If I have a set of data
x, y, z
我可以散布
import matplotlib.pyplot as plt
plt.scatter(x,y,c=z)
如何获得散点图的 plt.contourf(x,y,z)
?
How I can get a plt.contourf(x,y,z)
of the scatter ?
推荐答案
您可以使用 tricontourf 如b. of 的建议另一个答案:
You can use tricontourf as suggested in case b. of this other answer:
import matplotlib.tri as tri
import matplotlib.pyplot as plt
plt.tricontour(x, y, z, 15, linewidths=0.5, colors='k')
plt.tricontourf(x, y, z, 15)
旧回复:
使用如下函数转换成contourf需要的格式:
Old reply:
Use the following function to convert to the format required by contourf:
from numpy import linspace, meshgrid
from matplotlib.mlab import griddata
def grid(x, y, z, resX=100, resY=100):
"Convert 3 column data to matplotlib grid"
xi = linspace(min(x), max(x), resX)
yi = linspace(min(y), max(y), resY)
Z = griddata(x, y, z, xi, yi)
X, Y = meshgrid(xi, yi)
return X, Y, Z
现在你可以:
X, Y, Z = grid(x, y, z)
plt.contourf(X, Y, Z)
这篇关于制作散点轮廓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,WP2