2013-05-31 34 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」。 –