2017-09-13 69 views
0

我打算在matplotlib中創建一個圖形,左邊是3D表面,右邊是對應的等高線圖。Python Subplot 3d Surface and Heat Map

我使用了subplots,但它只顯示等值線圖(帶有表面的空白空間)和表面的單獨圖形。

是否可以在一個圖中並排創建這些圖?

編輯:代碼如下:

import matplotlib.pyplot as plt 
from mpl_toolkits.mplot3d import axes3d 
import numpy as np 

x = np.arange(-5, 5, 0.25) 
y = np.arange(-5, 5, 0.25) 
x, y = np.meshgrid(x, y) 
r = np.sqrt(x**2 + y**2) 
z = np.sin(r) 

fig, (surf, cmap) = plt.subplots(1, 2) 

fig = plt.figure() 
surf = fig.gca(projection='3d') 

surf.plot_surface(x,y,z) 

cmap.contourf(x,y,z,25) 

plt.show() 
+0

你可以顯示你的代碼,你目前如何做到這一點? –

+0

是的,但顯示所需的輸出 – Serenity

+1

有關於此的[官方示例](https://matplotlib.org/examples/mplot3d/mixed_subplots_demo.html)。如果您遇到執行問題,請參閱[問]和[mcve]。 – ImportanceOfBeingErnest

回答

1

我想it's hard以創建具有不同凸曲線的網格使用plt.subplots()

所以最直接的解決方案是用plt.subplot單獨創建每個子圖。

import matplotlib.pyplot as plt 
from mpl_toolkits.mplot3d import axes3d 
import numpy as np 

x = np.arange(-5, 5, 0.25) 
y = np.arange(-5, 5, 0.25) 
x, y = np.meshgrid(x, y) 
r = np.sqrt(x**2 + y**2) 
z = np.sin(r) 

ax = plt.subplot(121, projection='3d') 
ax.plot_surface(x,y,z) 

ax2 = plt.subplot(122) 
ax2.contourf(x,y,z,25) 

plt.show() 

當然,人們也可以使用gridspec功能的更復雜的電網結構。