如何使用 matplotlib 和 seaborn 制作带有百分比的问题矩阵?
我想在下面看到此类型的图表。 我知道我可以用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()
,但是我该如何添加该图中包含的百分比?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用编号和底部轴的顶轴添加次级X轴(
ax.twiny()
)以显示百分比。调用
PCOLOR
带有0.5位置的X和Y位置列表,将使tick和tick标签处于整数位置。clip_on = false
确保外部单元边界的厚度与其余相同。ax.invert_yaxis()
让您倒置y轴(因此您可以使用data
而不是data [::- 1]
)。减少字体尺寸(或增加
firfigsize
)也可以显示百分比符号: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 usedata
instead ofdata[::-1]
).Decreasing the fontsize (or increasing the
figsize
) allows to also show the percentage sign: