大尺寸的matplotlib像素图最佳方法

5

我有一个大型的2D数据集,想要为每个X,Y对关联一个颜色,并使用matplotlib绘制它。这里涉及到1000000个点。我想知道在性能(速度)方面最好的方法是什么,您是否可以提供一些示例。


这些点是在规则网格上吗?你想要它们在规则网格上吗?你需要提供更多信息!虽然1百万个点看起来很多,但如果你只是想要一个散点图的话,应该没问题。对于一张图片来说,1百万像素并不算太多,所以如果这些点在规则网格上,那么也不会有任何问题... - Joe Kington
1
请见https://dev59.com/-2855IYBdhLWcg3w6IzV的散点图和大量数据。 - Jeff
你看过imshow吗?http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.imshow 你想要如何指定X,Y对的颜色?你想为每1e6个位置指定一个独特的颜色吗?(这是不可能在视觉上区分1e6种颜色的...)你想将值映射到一个颜色条中吗?(这是imshow默认执行的操作) - Joe Kington
我想要一个位图图像,其中每个x、y对都有一种颜色取决于其值。我会查看你的答案。 - Open the way
1个回答

9
如果您正在处理常规网格,请将其视为图像:
import numpy as np
import matplotlib.pyplot as plt

nrows, ncols = 1000, 1000
z = 500 * np.random.random(nrows * ncols).reshape((nrows, ncols))

plt.imshow(z, interpolation='nearest')
plt.colorbar()
plt.show()

这是一个图片描述

如果你有组成规则网格的随机排序的x、y、z三元组,那么你需要将它们进行网格化。

基本上,你可能会有像这样的一些东西:

import numpy as np 
import matplotlib.pyplot as plt

# Generate some data
nrows, ncols = 1000, 1000
xmin, xmax = -32.4, 42.0
ymin, ymax = 78.9, 101.3

dx = (xmax - xmin) / (ncols - 1)
dy = (ymax - ymin) / (ncols - 1)

x = np.linspace(xmin, xmax, ncols)
y = np.linspace(ymin, ymax, nrows)
x, y = np.meshgrid(x, y)

z = np.hypot(x - x.mean(), y - y.mean())
x, y, z = [item.flatten() for item in (x,y,z)]

# Scramble the order of the points so that we can't just simply reshape z
indicies = np.arange(x.size)
np.random.shuffle(indicies)
x, y, z = [item[indicies] for item in (x, y, z)]

# Up until now we've just been generating data...
# Now, x, y, and z probably represent something like you have.

# We need to make a regular grid out of our shuffled x, y, z indicies.
# To do this, we have to know the cellsize (dx & dy) that the grid is on and
# the number of rows and columns in the grid. 

# First we convert our x and y positions to indicies...
idx = np.round((x - x.min()) / dx).astype(np.int)
idy = np.round((y - y.min()) / dy).astype(np.int)

# Then we make an empty 2D grid...
grid = np.zeros((nrows, ncols), dtype=np.float)

# Then we fill the grid with our values:
grid[idy, idx] = z

# And now we plot it:
plt.imshow(grid, interpolation='nearest', 
        extent=(x.min(), x.max(), y.max(), y.min()))
plt.colorbar()
plt.show()

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接