【问题标题】:seaborn heatmap y-axis reverse orderseaborn热图y轴逆序
【发布时间】:2016-03-17 20:53:00
【问题描述】:

看看 this 可在 seaborn 热图文档中找到热图。

现在,y 轴从底部的 9 开始,到顶部的 0 结束。 有没有办法扭转这种局面,即底部以 0 开始,顶部以 9 结束?

【问题讨论】:

    标签: python matplotlib heatmap seaborn


    【解决方案1】:

    看起来ax.invert_yaxis() 解决了它。

    按照您获得该图的示例:

    import numpy as np; np.random.seed(0)
    import seaborn as sns; sns.set()
    uniform_data = np.random.rand(10, 12)
    ax = sns.heatmap(uniform_data)
    ax.invert_yaxis()
    

    提供:

    【讨论】:

      【解决方案2】:

      如果您像我一样使用“十六进制”jointplot() 作为热图,那么您可以这样做:

      import matplotlib.pyplot as plt
      import numpy
      import seaborn
      
      x = numpy.arange(10)
      y = x**2
      
      g = seaborn.jointplot(x, y, kind='hex')
      g.fig.axes[0].invert_yaxis()
      
      plt.show()
      

      【讨论】:

        【解决方案3】:

        我找到了一种更简单的方法来设置坐标轴顺序,使用选项 ylimxlim。在以下示例中,我绘制了 H,一个 2d 矩阵 (NX x NY),改变了轴的顺序:

        import matplotlib.pyplot as plt
        import seaborn as sns
        
        NX=10
        NY=20
        H = np.random.rand(NY, NX)
        sns.heatmap(H, xticklabels=True, yticklabels=True, annot = True)
        plt.ylim(0,NY)
        plt.xlim(0,NX)
        plt.show()
        

        NX=10
        NY=20
        H = np.random.rand(NY, NX)
        sns.heatmap(H, xticklabels=True, yticklabels=True, annot = True)
        plt.ylim(NY,0)
        plt.xlim(NX,0)
        plt.show()
        

        【讨论】:

          猜你喜欢
          • 2020-04-19
          • 2021-01-22
          • 1970-01-01
          • 2021-01-14
          • 2021-08-03
          • 1970-01-01
          • 2021-11-16
          • 2017-04-16
          • 2021-04-07
          相关资源
          最近更新 更多