2012-04-27 84 views
1

我有下面的代碼,但我希望它們在同一個圖形上,但不同的子圖。繪製不同軸線類型的多個繪圖

創建不同軸的最簡單方法是什麼?

from pylab import * 

figure(0) 
x =1 
y = 2 
plot(x, y, marker ='^', linewidth=4.0) 
xlabel('time (s)') 
ylabel('cost ($)') 
title('cost vs. time') 

figure(1) 

x = 4 
y = 100 
plot(x, y, marker ='^', linewidth=4.0) 
xlabel('cost ($)') 
ylabel('performance (miles/hr) ') 
title('cost vs. time') 


show() 

回答

2

您可以使用pyplot.subplots。我使用的是推薦的編程pyplot API(參見:http://matplotlib.sourceforge.net/api/pyplot_api.html#pyplot

import matplotlib.pyplot as plt 

fig, (ax0, ax1) = plt.subplots(ncols=2) 
x = 1 
y = 2 
ax0.plot(x, y, marker ='^') 
ax0.set_xlabel('time (s)') 
ax0.set_ylabel('cost ($)') 
ax0.set_title('Plot 1') 

x = 4 
y = 100 
ax1.plot(x, y, marker ='^') 
ax1.set_xlabel('cost ($)') 
ax1.set_ylabel('performance (miles/hr)') 
ax1.set_title('Plot 2') 

fig.suptitle('cost vs. time') 
plt.subplots_adjust(top=0.85, bottom=0.1, wspace=0.25) 
plt.show() 

resulting_plot

+0

真棒,但什麼是來標明這些次要情節,給每一個特定標題的命令...謝謝 – 2012-04-28 15:53:04

+0

剛使用set_title()。我已經更新了這個例子。 – bmu 2012-04-29 18:59:09

相關問題