Python:来自 3 个列表的 2d 等高线图:x、y 和 rho? [英] Python : 2d contour plot from 3 lists : x, y and rho?

查看:19
本文介绍了Python:来自 3 个列表的 2d 等高线图:x、y 和 rho?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 python 和 matplotlib 中有一个简单的问题.我有 3 个列表:x、y 和 rho,其中 rho[i] 是点 x[i]、y[i] 处的密度.x 和 y 的所有值都在 -1 之间.和 1. 但它们没有特定的顺序.

I have a simple problem in python and matplotlib. I have 3 lists : x, y and rho with rho[i] a density at the point x[i], y[i]. All values of x and y are between -1. and 1. but they are not in a specific order.

如何制作密度 rho(在点 x、y 处插值)的等高线图(如使用 imshow).

How to make a contour plot (like with imshow) of the density rho (interpolated at the points x, y).

非常感谢.

我使用大数组:x、y 和 rho 有 10,000 到 1,000,000 个元素

EDIT : I work with large arrays : x, y and rho have between 10,000 and 1,000,000 elements

推荐答案

您需要插入 rho 值.没有一种方法可以做到这一点,最佳"方法完全取决于您应该纳入插值中的先验信息.

You need to interpolate your rho values. There's no one way to do this, and the "best" method depends entirely on the a-priori information you should be incorporating into the interpolation.

在我对黑盒"插值方法进行咆哮之前,径向基函数(例如薄板样条"是一种特殊类型的径向基函数)通常是一个不错的选择.如果你有数百万个点,这个实现将是低效的,但作为一个起点:

Before I go into a rant on "black-box" interpolation methods, though, a radial basis function (e.g. a "thin-plate-spline" is a particular type of radial basis function) is often a good choice. If you have millions of points, this implementation will be inefficient, but as a starting point:

import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate

# Generate data:
x, y, z = 10 * np.random.random((3,10))

# Set up a regular grid of interpolation points
xi, yi = np.linspace(x.min(), x.max(), 100), np.linspace(y.min(), y.max(), 100)
xi, yi = np.meshgrid(xi, yi)

# Interpolate
rbf = scipy.interpolate.Rbf(x, y, z, function='linear')
zi = rbf(xi, yi)

plt.imshow(zi, vmin=z.min(), vmax=z.max(), origin='lower',
           extent=[x.min(), x.max(), y.min(), y.max()])
plt.scatter(x, y, c=z)
plt.colorbar()
plt.show()

这篇关于Python:来自 3 个列表的 2d 等高线图:x、y 和 rho?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆