【问题标题】:Moving x-axis to the top of a plot in matplotlib在 matplotlib 中将 x 轴移动到绘图的顶部
【发布时间】:2013-01-02 14:14:59
【问题描述】:

基于this question about heatmaps in matplotlib,我想将 x 轴标题移到绘图顶部。

import matplotlib.pyplot as plt
import numpy as np
column_labels = list('ABCD')
row_labels = list('WXYZ')
data = np.random.rand(4,4)
fig, ax = plt.subplots()
heatmap = ax.pcolor(data, cmap=plt.cm.Blues)

# put the major ticks at the middle of each cell
ax.set_xticks(np.arange(data.shape[0])+0.5, minor=False)
ax.set_yticks(np.arange(data.shape[1])+0.5, minor=False)

# want a more natural, table-like display
ax.invert_yaxis()
ax.xaxis.set_label_position('top') # <-- This doesn't work!

ax.set_xticklabels(row_labels, minor=False)
ax.set_yticklabels(column_labels, minor=False)
plt.show()

但是,调用matplotlib's set_label_position(如上所述)似乎没有达到预期的效果。这是我的输出:

我做错了什么?

【问题讨论】:

    标签: python matplotlib plot data-visualization


    【解决方案1】:

    tick_params 对于设置刻度属性非常有用。标签可以移动到顶部:

        ax.tick_params(labelbottom=False,labeltop=True)
    

    【讨论】:

    • Kwargs 是布尔值,所以应该分别是 FalseTrue,否则完美运行!
    【解决方案2】:

    如果您希望蜱(而不是标签)显示在顶部和底部(而不仅仅是顶部),则必须进行一些额外的按摩。我能做到这一点的唯一方法是对 unutbu 的代码稍作改动:

    import matplotlib.pyplot as plt
    import numpy as np
    column_labels = list('ABCD')
    row_labels = list('WXYZ')
    data = np.random.rand(4, 4)
    fig, ax = plt.subplots()
    heatmap = ax.pcolor(data, cmap=plt.cm.Blues)
    
    # put the major ticks at the middle of each cell
    ax.set_xticks(np.arange(data.shape[1]) + 0.5, minor=False)
    ax.set_yticks(np.arange(data.shape[0]) + 0.5, minor=False)
    
    # want a more natural, table-like display
    ax.invert_yaxis()
    ax.xaxis.tick_top()
    ax.xaxis.set_ticks_position('both') # THIS IS THE ONLY CHANGE
    
    ax.set_xticklabels(column_labels, minor=False)
    ax.set_yticklabels(row_labels, minor=False)
    plt.show()
    

    输出:

    【讨论】:

      【解决方案3】:

      使用

      ax.xaxis.tick_top()
      

      在图像顶部放置刻度线。命令

      ax.set_xlabel('X LABEL')    
      ax.xaxis.set_label_position('top') 
      

      影响标签,而不是刻度线。

      import matplotlib.pyplot as plt
      import numpy as np
      column_labels = list('ABCD')
      row_labels = list('WXYZ')
      data = np.random.rand(4, 4)
      fig, ax = plt.subplots()
      heatmap = ax.pcolor(data, cmap=plt.cm.Blues)
      
      # put the major ticks at the middle of each cell
      ax.set_xticks(np.arange(data.shape[1]) + 0.5, minor=False)
      ax.set_yticks(np.arange(data.shape[0]) + 0.5, minor=False)
      
      # want a more natural, table-like display
      ax.invert_yaxis()
      ax.xaxis.tick_top()
      
      ax.set_xticklabels(column_labels, minor=False)
      ax.set_yticklabels(row_labels, minor=False)
      plt.show()
      

      【讨论】:

        【解决方案4】:

        你想要set_ticks_position而不是set_label_position

        ax.xaxis.set_ticks_position('top') # the rest is the same
        

        这给了我:

        【讨论】:

          猜你喜欢
          • 2020-03-20
          • 1970-01-01
          • 2023-03-06
          • 2017-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-02-24
          • 1970-01-01
          • 2017-03-25
          相关资源
          最近更新 更多