如何使用 matplotlib 和 seaborn 制作带有百分比的问题矩阵?

发布于 01-18 04:59 字数 527 浏览 2 评论 0 原文

我想在下面看到此类型的图表。 我知道我可以用matplotlib制作矩阵图 喜欢这样

    cmap = colors.ListedColormap(['white','red'])
data = [
    [0,0,0,0,0,1,1,1,1,],
    [0,0,0,0,0,1,0,0,1,],
]
plt.figure(figsize=(9,5))
plt.pcolor(data[::-1],cmap=cmap,edgecolors='k', linewidths=3)
plt.xlabel('Problem') 
plt.ylabel('Particpant')
plt.show()

,但是我该如何添加该图中包含的百分比?

I want to make this type of graph you see below.
I get that I can make a matrix graph with matplotlib
like so

    cmap = colors.ListedColormap(['white','red'])
data = [
    [0,0,0,0,0,1,1,1,1,],
    [0,0,0,0,0,1,0,0,1,],
]
plt.figure(figsize=(9,5))
plt.pcolor(data[::-1],cmap=cmap,edgecolors='k', linewidths=3)
plt.xlabel('Problem') 
plt.ylabel('Particpant')
plt.show()

But how would I go about adding percentages to be included in this graph?

I want to make this type of graph

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

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

发布评论

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

评论(1

妖妓 2025-01-25 04:59:48

您可以使用编号和底部轴的顶轴添加次级X轴( ax.twiny())以显示百分比。

调用 PCOLOR 带有0.5位置的X和Y位置列表,将使tick和tick标签处于整数位置。 clip_on = false 确保外部单元边界的厚度与其余相同。 ax.invert_yaxis()让您倒置y轴(因此您可以使用 data 而不是 data [::- 1] )。

import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
import numpy as np

cmap = ListedColormap(['white', 'orangered'])
data = np.random.randint(0, 3, size=(28, 30)) % 2
data[:, 9] = 1  # one full column to simulate 100%
data[:, 11] = 0  # one empty  column to simulate 0%
fig, ax = plt.subplots(figsize=(9, 5))
ax.pcolor(np.arange(data.shape[1] + 1) + 0.5, np.arange(data.shape[0] + 1) + 0.5, data,
          cmap=cmap, edgecolors='k', linewidths=3, clip_on=False)
ax.set_yticks(range(1, data.shape[0] + 1))
ax.set_xticks(range(1, data.shape[1] + 1))
ax.set_xticklabels([f'{p:.0f}' for p in data.mean(axis=0) * 100])
ax.invert_yaxis()

ax2 = ax.twiny()
ax2.set_xlim(ax.get_xlim())
ax2.set_xticks(range(1, data.shape[1] + 1))
ax2.set_xlabel('Problem')
ax.tick_params(length=0)
ax2.tick_params(length=0)
ax.set_ylabel('Particpant')
plt.tight_layout()
plt.show()

减少字体尺寸(或增加 firfigsize )也可以显示百分比符号:

ax.set_xticklabels([f'{p:.0f}%' for p in data.mean(axis=0) * 100], fontsize=8)

You can add a secondary x-axis (ax.twiny()), using the top axis for the numbering and the bottom axis to show the percentages.

Calling pcolor with a list of x and y positions that are 0.5 shifted will put the ticks and tick labels at integer positions. clip_on=False makes sure the outer cell borders have the same thickness as the rest. ax.invert_yaxis() lets you invert the y axis (so you can use data instead of data[::-1]).

import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
import numpy as np

cmap = ListedColormap(['white', 'orangered'])
data = np.random.randint(0, 3, size=(28, 30)) % 2
data[:, 9] = 1  # one full column to simulate 100%
data[:, 11] = 0  # one empty  column to simulate 0%
fig, ax = plt.subplots(figsize=(9, 5))
ax.pcolor(np.arange(data.shape[1] + 1) + 0.5, np.arange(data.shape[0] + 1) + 0.5, data,
          cmap=cmap, edgecolors='k', linewidths=3, clip_on=False)
ax.set_yticks(range(1, data.shape[0] + 1))
ax.set_xticks(range(1, data.shape[1] + 1))
ax.set_xticklabels([f'{p:.0f}' for p in data.mean(axis=0) * 100])
ax.invert_yaxis()

ax2 = ax.twiny()
ax2.set_xlim(ax.get_xlim())
ax2.set_xticks(range(1, data.shape[1] + 1))
ax2.set_xlabel('Problem')
ax.tick_params(length=0)
ax2.tick_params(length=0)
ax.set_ylabel('Particpant')
plt.tight_layout()
plt.show()

pcolor with shifted cell borders, secondary ax for extra info

Decreasing the fontsize (or increasing the figsize) allows to also show the percentage sign:

ax.set_xticklabels([f'{p:.0f}%' for p in data.mean(axis=0) * 100], fontsize=8)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文