2012-09-17 44 views
2

我正在繪製來自多個來源的數據並需要multiple x axes,最好是偏移,如鏈接中所見。我非常喜歡我的x軸有可變長度,允許我在同一個圖上放置很多圖。什麼是迄今爲止我所做的是:python/matplotlib - 指定寄生蟲軸長度

import matplotlib.pyplot as plt 
from mpl_toolkits.axes_grid1 import host_subplot 
import mpl_toolkits.axisartist as AA 
host = host_subplot(111, axes_class=AA.Axes,yscale='log') 
plt.subplots_adjust(bottom=0.25) 

par1 = host.twiny() 

offset = 60 

new_fixed_axis = par1.get_grid_helper().new_fixed_axis 
par1.axis['bottom'] = new_fixed_axis(loc='bottom', 
            axes=par1, 
            offset=(0, -60)) 

host.set_xlim(200, 350) 
host.set_ylim(1050, 100) 
par1.set_xlim(0, 1) 

host.set_xlabel('Temperature (K)') 
host.set_ylabel('Pressure (hPa)') 
par1.set_xlabel('Relative Humidity (%)') 

p1, = host.plot(T,P) 
p2, = host.plot(pT,P) 
p2, = par1.plot(RH,P) 

所以我得到的軸降了下來,但不能爲我的生活,弄清楚如何獲得軸實際水平壓縮(如喜歡藍色軸在上面的鏈接圖中)。

我的問題是如何做到這一點(如果有的話)?


@ Oz123

以下是我有:

host = host_subplot(111, axes_class=AA.Axes,yscale='log') 
plt.subplots_adjust(bottom=0.25) 

par1 = host.twiny() 

new_fixed_axis = par1.get_grid_helper().new_fixed_axis 

cax1 = plt.axes(axisbg='none',frameon=False) 
cax1 = plt.add_axes(plt.get_position(), frameon=False) 
par1.axis['bottom'] = new_fixed_axis(loc='bottom', 
            axes=cax1, 
            offset=(0, -60)) 

當我到:

cax1 = plt.add_axes(plt.get_position(), frameon=False) 

我以前的X/Y軸消失,我留下用只有cax1的灰色屏幕。

我的歉意,我只是拿起matplotlib,所以恐怕我還是個新手。

回答

1

您正在創建par1.axis['bottom']與主要的ax對象,所以你實際上做的事情非常有限。
相反,您應該創建2個或更多個axes實例。並把它們放在圖形實例上。

增加新的軸實例

cax1 = plt.axes(axisbg = '無',frameon = FALSE)

這樣你你可以有你的溼度規模大小的精細控制。

以下行:

par1.axis['bottom'] = new_fixed_axis(loc='bottom', 
            axes=par1, 
            offset=(0, -60)) 

應該例如:

par1.axis['bottom'] = new_fixed_axis(loc='bottom', 
            axes=cax1, # custom axis number 1 
            offset=(0, -60)) 

注意,使用IPython,您可以快速找到哪些方法可以用來控制 新創建的軸實例。

In [38]: cax1.set_ #tab pressed 
cax1.set_adjustable   cax1.set_axis_bgcolor   cax1.set_frame_on    cax1.set_subplotspec   cax1.set_xticks 
cax1.set_agg_filter   cax1.set_axis_off    cax1.set_gid     cax1.set_title     cax1.set_ybound 
cax1.set_alpha     cax1.set_axis_on    
# many more options trimmed, but I think you might want to take a look in: 

控制新創建的實例的位置:

In [38]: cax1.set_position? 
Type:  instancemethod 
String Form:<bound method AxesSubplot.set_position of <matplotlib.axes.AxesSubplot object at 0x2d7fb90>> 
File:  /usr/lib/pymodules/python2.7/matplotlib/axes.py 
Definition: cax1.set_position(self, pos, which='both') 
Docstring: 
Set the axes position with:: 

    pos = [left, bottom, width, height]