matplotlib/seaborn 中 y 轴外部的框

发布于 2025-01-11 06:57:01 字数 668 浏览 0 评论 0原文

我正在尝试在 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 轴供参考。 示例框在我的 y 轴之外

I"m trying to add a descriptive box outside my y axis on my heatmap in matplotlib/seaborn. I tried an additional subplot and hspan, but they weren't working for me. Help please!

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)

Here is a picture of the box beside the y axis for reference.
Example box outside my y axis

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一萌ing 2025-01-18 06:57:01

您可以创建两个子图,将热图放在右侧子图中,将文本放在左侧子图中。

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()

框外y 轴

You could create two subplots, put the heatmap in the right subplot and the text in the left one.

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()

box outside the y-axis

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文