2013-05-31 32 views
7

我想用matplotlib来绘制3D热图与我的模拟结果。我读过this topic并尝试使用imshow。不幸的是,当我用SVG或EPS格式保存图形时,它将heatmat转换为图片(这对于期刊是不可接受的)。所以,我也试过hexbin - 但图像是如此奇怪。我不确定它会被期刊接受。我们还有别的东西吗,或者我必须用矩形填充heatmat?matplotlib与矢量格式的热图

例如,如果一个运行此代码:

import numpy as np 
import numpy.random 
import matplotlib.pyplot as plt 

# Generate some test data 
x = np.random.randn(8873) 
y = np.random.randn(8873) 

heatmap, xedges, yedges = np.histogram2d(x, y, bins=50) 
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]] 

print extent 
print heatmap 
plt.clf() 
surf = plt.imshow(heatmap, extent=extent) 
plt.colorbar(surf, shrink=0.75, aspect=5) 
plt.show() 

并保存SVG文件,它会containe PNG图像:

<g clip-path="url(#p6def4f5150)"> 
    <image height="347" width="315" x="115.127800906" xlink:href="data:image/png;base64, 

我使用matplotlib,1.1.1版本的OpenSUSE和Ubuntu下OS。

+0

@tcaswell我编辑了主题 – rth

回答

9

使用pcolormesh如果您要使用矢量输出,则使用imshow

使用pcolorpcolormesh时,不能插入图像,但是。另一方面,如果你想要矢量输出,你可能不想插值。

这基本上是imshowpcolor/pcolormesh之间的差异的原因。 imshow产生光栅,而pcolormeshpcolor产生矩形色块。

您还需要稍微改变传递图像范围的方式。作为基于你一个例子:

import numpy as np 
import numpy.random 
import matplotlib.pyplot as plt 

# Generate some test data 
x = np.random.randn(8873) 
y = np.random.randn(8873) 

heatmap, xedges, yedges = np.histogram2d(x, y, bins=50) 

surf = plt.pcolormesh(xedges, yedges, heatmap) 
plt.axis('image') 
plt.colorbar(surf, shrink=0.75, aspect=5) 
plt.show() 

enter image description here

当你保存为SVG,输出为载体的补丁。例如。

... 
    <g id="QuadMesh_1"> 
    <defs> 
    <path d=" 
M75.9063 -43.2 
L82.9705 -43.2 
L82.9705 -50.112 
L75.9063 -50.112 
L75.9063 -43.2" id="C0_0_9d1ab33858"/> 
    <path d=" 
M82.9705 -43.2 
L90.0348 -43.2 
L90.0348 -50.112 
L82.9705 -50.112 
L82.9705 -43.2" id="C0_1_d828245e6a"/> 
... 
+0

谢谢!有用! :)你能解释一下这个:'plt.axis('image')'? – rth

+0

它将绘图的高宽比设置为1(即,正方形像素将为正方形)并修剪极限值。它只是在那里,以便输出看起来类似于“imshow”。 –