2017-08-16 105 views
1

如何顯示雙軸座標圖,以便頂軸和右軸的方向「相等」。例如,以下代碼將生成方塊圖如何使用twinx,仍然可以得到方形圖

import matplotlib.pyplot as plt 
fig, ax = plt.subplots() 
ax.set_aspect('equal') 
ax.plot([0,1],[0,1]) 

但是,只要您使用twinx函數,就會發生這種變化。

ax2 = ax.twinx() 
ax2.set_ylim([0,2]) 
ax3 = ax.twiny() 
ax3.set_xlim([0,2]) 

使用set_aspect(「等於」)上AX2和AX3似乎迫使它斧頭的方面,但set_aspect(0.5)似乎並沒有任何改變任何東西。

簡單地說,我想情節是正方形,底部和左側軸,以從0到1,頂部和右側軸從0您可以設置的方面運行2.

兩個孿生的軸?我試着堆疊軸:

ax3 = ax2.twiny() 
ax3.set_aspect('equal') 

我使用set_aspect可調關鍵字也試過:

ax.set_aspect('equal', adjustable:'box-forced') 

我能得到的最接近的是:

import matplotlib.pyplot as plt 
fig, ax = plt.subplots() 
ax.set_aspect('equal', adjustable='box-forced') 
ax.plot([0,1],[0,1]) 
ax2=ax.twinx() 
ax3 = ax2.twiny() 
ax3.set_aspect(1, adjustable='box-forced') 
ax2.set_ylim([0,2]) 
ax3.set_xlim([0,2]) 
ax.set_xlim([0,1]) 
ax.set_ylim([0,1]) 

將會產生:

enter image description here

我想刪除左側和右側的多餘空間

回答

1

使用兩個不同的雙軸來獲得兩組獨立的軸似乎過於複雜。如果目標是在圖的每一邊創建一個具有一個軸的方形圖,則可以使用兩個位於相同位置但具有不同比例的兩個axes。兩者可以被設置爲具有相等的縱橫比。

import matplotlib.pyplot as plt 

fig, ax = plt.subplots() 
ax.set_aspect('equal') 
ax.plot([0,1],[0,1]) 

ax2 = fig.add_axes(ax.get_position()) 
ax2.set_facecolor("None") 
ax2.set_aspect('equal') 
ax2.plot([2,0],[0,2], color="red") 
ax2.tick_params(bottom=0, top=1, left=0, right=1, 
       labelbottom=0, labeltop=1, labelleft=0, labelright=1) 

plt.show() 

enter image description here

+0

完美 - 感謝。我不知道你可以在同一個位置使用兩個軸。 –

相關問題