【问题标题】:Changing the Position of Axis Values and Labels in Python在 Python 中更改轴值和标签的位置
【发布时间】:2018-09-01 22:15:16
【问题描述】:

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

这会产生图表:

Plot

为什么 x 轴值(轴上标记的数字的术语是什么?)在顶部?

如何将它们移到底部?

如何将标签“Predicted Class”放在顶部?

有没有更好的方法来设置轴上标签的值?例如没有定义 np.array 和刻度?

非常感谢!

【问题讨论】:

  • 你应该搜索这样的问题。 StackOverflow 和 Matplotlib 文档中已经有很多答案。
  • @Denziloe 我找不到任何适合我的情况 - 抱歉

标签: python matplotlib plot


【解决方案1】:

来自plt.matshow的文档

xaxis 的刻度标签位于顶部。

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

【讨论】:

  • labelpad=-600 没用。请改用plt.gca().xaxis.set_label_position('top')
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-05
  • 1970-01-01
  • 2013-07-09
  • 2011-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多