我正在嘗試在 matplotlib/seaborn 的熱圖上的 y 軸之外添加一個描述性框。我嘗試了一個額外的子圖和 hspan,但它們對我不起作用。請幫助!
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import numpy as np
col1, col2 = np.random.rand(31), np.random.rand(31)
df = pd.DataFrame({'first': col1,
'second': col2}, index = range(70,101))
fig, ax = plt.subplots(1,figsize = (5,10))
sns.heatmap(data=df, annot=True, cmap='RdYlGn', cbar=False)
ax.invert_yaxis()
ax.tick_params('y', labelrotation=0)
這是y軸旁邊的框的圖片供參考。

uj5u.com熱心網友回復:
您可以創建兩個子圖,將熱圖放在右側子圖中,將文本放在左側。
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
df = pd.DataFrame({'first': np.random.rand(31),
'second': np.random.rand(31)}, index=range(70, 101))
sns.set()
fig, (ax0, ax) = plt.subplots(ncols=2, figsize=(5, 10), gridspec_kw={'width_ratios': [1, 3]})
sns.heatmap(data=df, annot=True, cmap='RdYlGn', cbar=False, ax=ax)
ax.invert_yaxis()
ax.tick_params('y', labelrotation=0)
y_break = 21 / 31
ax0.axhspan(0, y_break, color='turquoise')
ax0.axhspan(y_break, 1, color='tomato')
ax0.axis('off')
ax0.set_ylim(0, 1)
ax0.text(0.5, y_break / 2, 'good', fontsize=24, rotation=90, ha='center', va='center')
ax0.text(0.5, (1 y_break) / 2, 'bad', fontsize=24, rotation=90, ha='center', va='center')
plt.tight_layout()
plt.show()

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/438519.html
標籤:Python matplotlib 注释 海运 热图
