2016-11-20 74 views
1

我想在同一圖中繪製兩個不同的函數。不過,我希望他們在x軸上使用不同的比例。 一個尺度shoudl只顯示x的值,其他的將在最後顯示秒數。在兩個x軸上用不同比例繪製兩個函數

現在我有這個

k=5 
fig = plt.figure() 
ax1 = fig.add_subplot(111) 
ax1.set_xlabel(r"values of x") #adds description to scale on bottom 
ax2 = ax1.twiny()  #adds the seconds scale on top 

x = np.arange(0.1, 1.5, 0.1) #values of x for function are in range 
y = k*(np.power(x,(k-1))) * np.exp(-(np.power(x,(k-1)))) #that is the function I want to draw 
ax1.plot(x,y)  #draw function 
tx = x 
ty = x*7 
ax2.plot(x,x*7) 

ax2.set_xlabel(r"time in seconds") 
ax2.set_xlim(1484)  #set limit of time 
ax2.invert_xaxis()  #invert it so that it works like we want to 
ax1.set_xlim(0.1,1.4)  #set limit for the x axis so that it doesn't skale on its own. 
plt.show() 

我很抱歉,但我不能正確地插入代碼。 ax2函數現在只是一個假人。我只是希望能夠看到它,並最終將ax2的比例更改爲我的時間範圍。

任何幫助將不勝感激!

回答

1

我不知道您的代碼不:-P工作

您的AX2虛擬函數是不夠好,我ax2.plot(x*1000,x*50)取代它能夠看到它。

而且我做的繪製縮放後:

k=5 
fig = plt.figure() 
ax1 = fig.add_subplot(111) 
ax1.set_xlabel(r"values of x") #adds description to scale on bottom 
ax2 = ax1.twiny()  #adds the seconds scale on top 

x = np.arange(0.1, 1.5, 0.1) #values of x for function are in range 
y = k*(np.power(x,(k-1))) * np.exp(-(np.power(x,(k-1)))) #that is the function I want to draw 
ax1.plot(x,y)  #draw function 
tx = x 
ty = x*7 


ax2.set_xlabel(r"time in seconds") 
ax2.set_xlim(1484)  #set limit of time 
ax2.invert_xaxis()  #invert it so that it works like we want to 
ax2.plot(x*1000,x*50) 
ax1.set_xlim(0.1,1.4)  #set limit for the x axis so that it doesn't skale on its own. 
plt.show() 

其中給出:

enter image description here

1

第二個情節是隱藏左Y軸後面。你將能夠看到它,如果你使用較粗的線和/或標誌物:

ax2.plot(x,x*7, '-o', lw=5) 

A plot

你還可改變ax2的X限制,但你去了你的方式,使之因爲它是這樣的,所以我想它完全如你所願。

相關問題