Python 制作散点轮廓

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18764814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:45:19  来源:igfitidea点击:

Make contour of scatter

pythonmatplotlibcontourscatter-plot

提问by JuanPablo

In python, If I have a set of data

在python中,如果我有一组数据

x, y, z

I can make a scatter with

我可以散布

import matplotlib.pyplot as plt
plt.scatter(x,y,c=z)

How I can get a plt.contourf(x,y,z)of the scatter ?

我怎样才能得到一个plt.contourf(x,y,z)分散的?

采纳答案by elyase

You can use tricontourfas suggested in case b.of this other answer:

您可以按照情况b 的建议使用tricontourf 这个其他答案

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)

Old reply:

旧回复:

Use the following function to convert to the format required by contourf:

使用以下函数转换成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

Now you can do:

现在你可以这样做:

X, Y, Z = grid(x, y, z)
plt.contourf(X, Y, Z)

enter image description here

在此处输入图片说明

回答by David Zwicker

contourexpects regularly gridded data. You thus need to interpolate your data first:

contour期望定期网格数据。因此,您需要先插入数据:

import numpy as np
from scipy.interpolate import griddata
import matplotlib.pyplot as plt
import numpy.ma as ma
from numpy.random import uniform, seed
# make up some randomly distributed data
seed(1234)
npts = 200
x = uniform(-2,2,npts)
y = uniform(-2,2,npts)
z = x*np.exp(-x**2-y**2)
# define grid.
xi = np.linspace(-2.1,2.1,100)
yi = np.linspace(-2.1,2.1,100)
# grid the data.
zi = griddata((x, y), z, (xi[None,:], yi[:,None]), method='cubic')
# contour the gridded data, plotting dots at the randomly spaced data points.
CS = plt.contour(xi,yi,zi,15,linewidths=0.5,colors='k')
CS = plt.contourf(xi,yi,zi,15,cmap=plt.cm.jet)
plt.colorbar() # draw colorbar
# plot data points.
plt.scatter(x,y,marker='o',c='b',s=5)
plt.xlim(-2,2)
plt.ylim(-2,2)
plt.title('griddata test (%d points)' % npts)
plt.show()

Note that I shamelessly stole this code from the excellent matplotlib cookbook

请注意,我无耻地从优秀的matplotlib 食谱中窃取了这段代码

回答by ImportanceOfBeingErnest

The solution will depend on how the data is organized.

解决方案将取决于数据的组织方式。

Data on regular grid

常规网格上的数据

If the xand ydata already define a grid, they can be easily reshaped to a quadrilateral grid. E.g.

如果xy数据已经定义了网格,则可以轻松地将它们重新调整为四边形网格。例如

#x  y  z
 4  1  3
 6  1  8
 8  1 -9
 4  2 10
 6  2 -1
 8  2 -8
 4  3  8
 6  3 -9
 8  3  0
 4  4 -1
 6  4 -8
 8  4  8 

can plotted as a contourusing

可以绘制为contour使用

import matplotlib.pyplot as plt
import numpy as np
x,y,z = np.loadtxt("data.txt", unpack=True)
plt.contour(x.reshape(4,3), y.reshape(4,3), z.reshape(4,3))

Arbitrary data

任意数据

a. Interpolation

一种。插值

In case the data is not living on a quadrilateral grid, one can interpolate the data on a grid. One way to do so is scipy.interpolate.griddata

如果数据不在四边形网格上,可以在网格上插入数据。一种方法是 scipy.interpolate.griddata

import numpy as np
from scipy.interpolate import griddata

xi = np.linspace(4, 8, 10)
yi = np.linspace(1, 4, 10)
zi = griddata((x, y), z, (xi[None,:], yi[:,None]), method='linear')
plt.contour(xi, yi, zi)

b. Non-gridded contour

湾 非网格轮廓

Finally, one can plot a contour completely without the use of a quadrilateral grid. This can be done using tricontour.

最后,可以在不使用四边形网格的情况下完全绘制轮廓。这可以使用tricontour.

plt.tricontour(x,y,z)

An example comparing the latter two methods is found on the matplotlib page.

matplotlib 页面上可以找到比较后两种方法的示例。