如何在python中绘制密度图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24119920/
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
How to plot a density map in python?
提问by user3722235
I have a .txt file containing the x,y values of regularly spaced points in a 2D map, the 3rd coordinate being the density at that point.
我有一个 .txt 文件,其中包含 2D 地图中规则间隔点的 x、y 值,第三个坐标是该点的密度。
4.882812500000000E-004 4.882812500000000E-004 0.9072267
1.464843750000000E-003 4.882812500000000E-004 1.405174
2.441406250000000E-003 4.882812500000000E-004 24.32851
3.417968750000000E-003 4.882812500000000E-004 101.4136
4.394531250000000E-003 4.882812500000000E-004 199.1388
5.371093750000000E-003 4.882812500000000E-004 1278.898
6.347656250000000E-003 4.882812500000000E-004 1636.955
7.324218750000000E-003 4.882812500000000E-004 1504.590
8.300781250000000E-003 4.882812500000000E-004 814.6337
9.277343750000000E-003 4.882812500000000E-004 273.8610
When I plot this density map in gnuplot, with the following commands:
当我在 gnuplot 中绘制此密度图时,使用以下命令:
set palette rgbformulae 34,35,0
set size square
set pm3d map
splot "dens_map.map" u 1:2:(log10(+10.)) title "Density map"`
Which gives me this beautiful image:
这给了我这个美丽的形象:


Now I would like to have the same result with matplotlib.
现在我想用 matplotlib 得到相同的结果。
采纳答案by Andi
Here is my aim at a more complete answer including choosing the color map and a logarithmic normalization of the color axis.
我的目标是提供更完整的答案,包括选择颜色图和颜色轴的对数归一化。
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from matplotlib.colors import LogNorm
import numpy as np
x, y, z = np.loadtxt('data.txt', unpack=True)
N = int(len(z)**.5)
z = z.reshape(N, N)
plt.imshow(z+10, extent=(np.amin(x), np.amax(x), np.amin(y), np.amax(y)),
cmap=cm.hot, norm=LogNorm())
plt.colorbar()
plt.show()
I assume here that your data can be transformed into a 2d array by a simple reshape. If this is not the case than you need to work a bit harder on getting the data in this form. Using imshow and not pcolormesh is more efficient here if you data lies on a grid (as it seems to do). The above code snippet results in the following image, that comes pretty close to what you wanted:
我在这里假设您的数据可以通过简单的重塑转换为二维数组。如果不是这种情况,则您需要更加努力地以这种形式获取数据。如果数据位于网格上(似乎是这样),则使用 imshow 而不是 pcolormesh 会更有效。上面的代码片段产生下图,非常接近你想要的:


回答by Hooked
The comment from @HYRY is good, but a complete minimal working answer (with a picture!) is better. Using plt.pcolormesh
@HYRY 的评论很好,但完整的最小工作答案(带图片!)更好。使用plt.pcolormesh
import pylab as plt
import numpy as np
# Sample data
side = np.linspace(-2,2,15)
X,Y = np.meshgrid(side,side)
Z = np.exp(-((X-1)**2+Y**2))
# Plot the density map using nearest-neighbor interpolation
plt.pcolormesh(X,Y,Z)
plt.show()


If the data looks like your sample, numpy can load it using the command numpy.genfromtext.
如果数据看起来像您的样本,numpy 可以使用命令加载它numpy.genfromtext。

