MatPlotLib填充过多

2024-06-02 18:42:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我在用matplotlib做熊猫数据框的热图。一切看起来都不错,只是热图上面的填充物太多了,我不知道怎么改: heatmap with too much padding on the top

我当前使用的代码:

plt.figure(num=1,figsize=(10,10))
plt.pcolor(table, cmap='Reds', vmin=0, vmax=5, edgecolors="black")
plt.tight_layout()
plt.yticks(np.arange(0.5, len(table.index), 1), table.index)
plt.xticks(np.arange(0.5, len(table.columns), 1), table.columns, rotation=45)
plt.savefig('test.png', bbox_inches='tight')
plt.show()

这是因为yticks是字符串,matplotlib试图腾出空间,以防我要旋转它们?你知道吗

谢谢!富豪们!你知道吗


Tags: columns数据代码indexlenmatplotlibnptable
1条回答
网友
1楼 · 发布于 2024-06-02 18:42:54

Matplotlib尝试为轴限制找到一个合适的整数(例如,45而不是41),以使绘图在自动记号的情况下看起来更好。重新定义记号并不会改变界限。您可以手动强制它们与您的数据完全匹配:

plt.gca().set_ylim((0, len(table.index)))
plt.gca().set_xlim((0, len(table.columns)))

相关问题 更多 >