簡體   English   中英

如何設置 matplotlib 軸縱橫比等於 twinx() 和 twiny()

[英]How to set matplotlib axes aspect to equal with twinx() and twiny()

我正在嘗試制作一個 plot ,其中所有四個面都有溫度標簽。 我可以使用 twinx() 和 twiny() 獲得標簽,但我無法弄清楚如何讓這些軸在縮放上相等(兩個比例上的一個單位相同,所以圖表將是長方形)。

import numpy as np
import matplotlib.pyplot as plt 

plt.style.use('classic')

# Grid squares values:
height = 21
width = 31

# Initialize matrix of zeros for that size
data = np.zeros((height, width)) + 75

# Make set temperatures on fixed positions
T_alpha   = 0    # (bottom boundary temperature)
T_bravo   = 40   # (left boundary temperature)
T_charlie = 100  # (top boundary temperature)
T_delta   = 100  # (right boundary temperature)

# Note: index 0,0 is bottom left

for i in range(width):
    data[0, i] = T_alpha
    data[(height - 1), i] = T_charlie
for j in range(1, (height - 1)):
    data[j, 0] = T_bravo
    data[j, (width - 1)] = T_delta

#print(data)

heatmap = plt.pcolor(data)

ax = plt.axes()
ax.xaxis.set_ticks_position('none')
ax.yaxis.set_ticks_position('none')
ax.yaxis.set_major_locator(plt.NullLocator())
ax.xaxis.set_major_formatter(plt.NullFormatter())
ax.set_xlabel("T = " + str(T_alpha) + "\N{DEGREE SIGN}C")
ax.set_ylabel("T = " + str(T_bravo) + "\N{DEGREE SIGN}C")

ax2 = ax.twinx()
ax2.yaxis.set_label_position('right')
ax2.yaxis.set_ticks_position('none')
ax2.yaxis.set_major_locator(plt.NullLocator())
ax2.set_ylabel("T = " + str(T_delta) + "\N{DEGREE SIGN}C")

ax3 = ax.twiny()
ax3.xaxis.set_label_position('top')
ax3.xaxis.set_ticks_position('none')
ax3.xaxis.set_major_formatter(plt.NullFormatter())
ax3.set_xlabel("T = " + str(T_charlie) + "\N{DEGREE SIGN}C")

plt.xlim(0, width)
plt.ylim(0, height)

cbar = plt.colorbar(heatmap)
cbar.set_label("Temperature (\N{DEGREE SIGN}C)")
plt.clim(0, 100)

plt.show()

這是我正在尋找的縮放比例(但所有四個邊都標記了): 在此處輸入圖像描述

謝謝!

感謝@ImportanceOfBeingErnest 使用text為我指出正確的方向。

這是修復:

import numpy as np
import matplotlib.pyplot as plt 

plt.axes().set_aspect('equal')
plt.style.use('classic')

# Grid squares values:
height = 21
width = 31

# Initialize matrix of zeros for that size
data = np.zeros((height, width)) + 75

# Make set temperatures on fixed positions
T_alpha   = 0    # (bottom boundary temperature)
T_bravo   = 40   # (left boundary temperature)
T_charlie = 100  # (top boundary temperature)
T_delta   = 100  # (right boundary temperature)

# Note: index 0,0 is bottom left

for i in range(width):
    data[0, i] = T_alpha
    data[(height - 1), i] = T_charlie
for j in range(1, (height - 1)):
    data[j, 0] = T_bravo
    data[j, (width - 1)] = T_delta

#print(data)

heatmap = plt.pcolor(data)

plt.text(0.5, -0.02, "T = " + str(T_alpha) + "\N{DEGREE SIGN}C",
         horizontalalignment='center',
         verticalalignment='top',
         rotation=0,
         clip_on=False,
         transform=plt.gca().transAxes)
plt.text(0, 0.5, "T = " + str(T_bravo) + "\N{DEGREE SIGN}C",
         horizontalalignment='right',
         verticalalignment='center',
         rotation=90,
         clip_on=False,
         transform=plt.gca().transAxes)
plt.text(0.5, 1, "T = " + str(T_charlie) + "\N{DEGREE SIGN}C",
         horizontalalignment='center',
         verticalalignment='bottom',
         rotation=0,
         clip_on=False,
         transform=plt.gca().transAxes)
plt.text(1, 0.5, "T = " + str(T_delta) + "\N{DEGREE SIGN}C",
         horizontalalignment='left',
         verticalalignment='center',
         rotation=270,
         clip_on=False,
         transform=plt.gca().transAxes)

plt.axis("off")

plt.xlim(0, width)
plt.ylim(0, height)

cbar = plt.colorbar(heatmap)
cbar.set_label("Temperature (\N{DEGREE SIGN}C)")
plt.clim(0, 100)

plt.show()

這使: 在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM