我对matplotlib中的格式有一些疑问。我有一个矩阵,并使用plt.matshow()对其进行了绘制。我正在努力使它看起来不错。我的代码如下:
norm_conf_mx =
norm_conf_mx = np.array([[5728, 3, 24, 10, 11, 51, 44, 8, 40, 4],
[ 1, 6484, 42, 27, 6, 51, 6, 10, 105, 10],
[ 52, 34, 5337, 108, 79, 23, 91, 54, 164, 16],
[ 47, 40, 137, 5335, 2, 243, 37, 62, 139, 89],
[ 19, 25, 36, 8, 5385, 12, 54, 32, 81, 190],
[ 73, 35, 33, 183, 78, 4631, 102, 27, 173, 86],
[ 37, 23, 52, 2, 47, 93, 5615, 3, 45, 1],
[ 26, 20, 70, 30, 47, 10, 8, 5820, 17, 217],
[ 49, 146, 71, 155, 18, 165, 56, 25, 5027, 139],
[ 43, 33, 25, 94, 168, 34, 3, 204, 89, 5256]])
nums = np.array([0,1,2,3,4,5,6,7,8,9])
my_ticks = [0,1,2,3,4,5,6,7,8,9]
plt.figure(figsize=(10,10))
plt.matshow(norm_conf_mx,cmap="gray",fignum=1)
plt.ylabel('Actual Class')
plt.xlabel('Predicted Class')
plt.xticks(nums, my_ticks)
plt.yticks(nums, my_ticks)
plt.show()
这将产生图:
为什么x轴值(轴上标有数字的术语是什么?)在顶部?
如何将它们移至底部?
如何将“ Predicted Class”标签放在顶部?
是否有更好的方法在轴上设置标签的值?例如没有定义np.array和滴答声?
非常感谢!
答案 0 :(得分:0)
摘自plt.matshow
的文档
x轴的刻度标签位于顶部。
按照ImportanceOfBeingErnest的建议,您可以使用plt.gca().xaxis.set_label_position('top')
移动x标签。
plt.figure(figsize=(10,10))
plt.matshow(norm_conf_mx,cmap="gray",fignum=1)
plt.ylabel('Actual Class')
plt.xlabel('Predicted Class')
plt.xticks(nums, my_ticks)
plt.yticks(nums, my_ticks)
plt.gca().xaxis.set_label_position('top')
plt.show()
您可以使用plt.imshow
在底部显示x轴值
plt.figure(figsize=(10,10))
plt.imshow(norm_conf_mx,cmap="gray",)
plt.ylabel('Actual Class')
plt.xlabel('Predicted Class')
plt.xticks(nums, my_ticks)
plt.yticks(nums, my_ticks)
plt.show()