2015-09-30 74 views
-1

我已經使用下面的代碼來繪製使用matplotlib和4參數X=depth, y1=temp, y2=salinity, y3=velocity的圖形,但速度的值不能正確顯示,並且第2和第3軸不可調整(比例值)恆定比例值,我怎樣才能使它可調(比例值),我的意思是縮放比例縮放時,所有軸的比例值都是可變的。使用matplotlib不能正常工作的多個y軸

from mpl_toolkits.axes_grid1 import host_subplot 
import mpl_toolkits.axisartist as AA 
import matplotlib.pyplot as plt 
import numpy as np 

if 1: 

    host = host_subplot(111, axes_class=AA.Axes) 
    plt.subplots_adjust(right=0.75) 

    par1 = host.twinx() 
    par2 = host.twinx() 

    offset = 60 
    new_fixed_axis = par2.get_grid_helper().new_fixed_axis 
    par2.axis["right"] = new_fixed_axis(loc="right", axes=par2, offset= 
    (offset, 0)) 

    par2.axis["right"].toggle(all=True) 

    # host.set_xlim(0, 2) 
    #host.set_ylim(0, 2) 

    host.set_xlabel("Depth") 
    host.set_ylabel("Temperature") 
    par1.set_ylabel("Salinity") 
    par2.set_ylabel("Velocity") 
    x = np.array([0,5,10,15,20,25,30,35]) 
    y1 = np.array([30.340,30.080,29.116,28.395,28.093,28.040,27.975,27.872]) 
    y2 = np.array([33.130,34.046,35.119,34.933,34.764,34.824,34.860,34.852]) 
    y3 = np.array([1544.296, 1544.787, 1543.452,1542.280,1541.522,1544.548,1541.523,1541.368]) 

    p1, = host.plot(x, y1, label="Temperature") 
    p2, = par1.plot(x, y2, label="Salinity") 
    p3, = par2.plot(x, y3, label="Velocity") 

    #par1.set_ylim(0, 4) 
    #par2.set_ylim(1, 65) 

    host.legend() 

    host.axis["left"].label.set_color(p1.get_color()) 
    par1.axis["right"].label.set_color(p2.get_color()) 
    par2.axis["right"].label.set_color(p3.get_color()) 

    plt.draw() 
    plt.show() 
+0

如果您創建的「主機」使用plt.subplot代替host_subplot,那麼他們都應該平移+縮放一起爲你描述。你是否需要使用host_subplot來獲得你正在使用的漂亮的偏移量? – DanHickstein

+0

它不工作,請修改此代碼並更改 – sohamkumar

+0

我修復了縮進問題,因此現在應該可以輕鬆運行代碼。 – DanHickstein

回答

0

這裏是這樣工作,你提出一個例子:http://matplotlib.org/examples/pylab_examples/multiple_yaxis_with_spines.html

基本上,他們避免使用「new_fixed_axis」功能:

from mpl_toolkits.axes_grid1 import host_subplot 
import mpl_toolkits.axisartist as AA 
import matplotlib.pyplot as plt 
import numpy as np 

fig,host = plt.subplots() 
fig.subplots_adjust(right=0.75) 

par1 = host.twinx() 
par2 = host.twinx() 

par2.spines["right"].set_position(("axes", 1.2)) 
par2.spines["right"].set_visible(True) 

host.set_xlabel("Depth") 
host.set_ylabel("Temperature") 
par1.set_ylabel("Salinity") 
par2.set_ylabel("Velocity") 

x = np.array([0,5,10,15,20,25,30,35]) 
y1 = np.array([30.340,30.080,29.116,28.395,28.093,28.040,27.975,27.872]) 
y2 = np.array([33.130,34.046,35.119,34.933,34.764,34.824,34.860,34.852]) 
y3 = np.array([1544.296, 1544.787, 1543.452,1542.280,1541.522,1544.548,1541.523,1541.368]) 

p1, = host.plot(x, y1, 'b', label="Temperature") 
p2, = par1.plot(x, y2, 'r', label="Salinity") 
p3, = par2.plot(x, y3/10, 'g', label="Velocity/00") 

host.yaxis.label.set_color(p1.get_color()) 
par1.yaxis.label.set_color(p2.get_color()) 
par2.yaxis.label.set_color(p3.get_color()) 

tkw = dict(size=4, width=1.5) 
host.tick_params(axis='y', colors=p1.get_color(), **tkw) 
par1.tick_params(axis='y', colors=p2.get_color(), **tkw) 
par2.tick_params(axis='y', colors=p3.get_color(), **tkw) 
host.tick_params(axis='x', **tkw) 

host.legend() 
plt.show() 

還有與一個小問題奇怪的Y軸縮放,但至少他們縮放+平底鍋:)。

enter image description here

+0

感謝您的解決方案,並通過提供Y軸範圍的限制使用ylim問題與奇怪的y軸尺度將得到解決。 – sohamkumar