【问题标题】:Relocate colorbar重新定位颜色条
【发布时间】:2017-04-15 05:08:17
【问题描述】:

是否可以将颜色图(现在在原始图的右侧)放在图的顶部?

我的代码:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.axes3d import Axes3D, get_test_data
from matplotlib import cm
import numpy as np


# set up a figure twice as wide as it is tall
fig = plt.figure(figsize=plt.figaspect(0.5))

#===============
#  First subplot
#===============
# set up the axes for the first plot
ax = fig.add_subplot(1, 2, 1, projection='3d')

# plot a 3D surface like in the example mplot3d/surface3d_demo

X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm,
                       linewidth=0, antialiased=False)
ax.set_zlim(-1.01, 1.01)
fig.colorbar(surf, shrink=0.5, aspect=10)
fig.savefig('64bit.png')

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    您必须添加额外的轴 (add_axes) 才能将您的颜色条放在所需的位置:

    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d.axes3d import Axes3D, get_test_data
    from matplotlib import cm
    from mpl_toolkits.axes_grid1 import make_axes_locatable
    import numpy as np
    
    
    # set up a figure twice as wide as it is tall
    fig = plt.figure(figsize=plt.figaspect(0.5))
    
    #===============
    #  First subplot
    #===============
    # set up the axes for the first plot
    ax = fig.add_subplot(1, 2, 1, projection='3d')
    
    # plot a 3D surface like in the example mplot3d/surface3d_demo
    
    X = np.arange(-5, 5, 0.25)
    Y = np.arange(-5, 5, 0.25)
    X, Y = np.meshgrid(X, Y)
    R = np.sqrt(X**2 + Y**2)
    Z = np.sin(R)
    surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm,
                           linewidth=0, antialiased=False)
    ax.set_zlim(-1.01, 1.01)
    
    # position of colorbar
    # where arg is [left, bottom, width, height]
    cax = fig.add_axes([0.15, .87, 0.35, 0.03])
    fig.colorbar(surf, orientation='horizontal', cax=cax)
    plt.show()
    

    【讨论】:

      【解决方案2】:

      是的,网站上有多个答案向您展示如何像这样移动 colorbarpositioning the colorbar

      在您的情况下,您希望将其与 orientation 参数结合使用。据我所知,没有简单的方法可以自动将颜色条放在图形顶部,您必须手动放置。这是替换您的fig.colorbar(surf, shrink=0.5, aspect=10)的代码:

      cbax = fig.add_axes([0.1, 0.89, 0.5, 0.05])
      fig.colorbar(surf, orientation="horizontal", cax=cbax)
      

      列表中的数字描述了 [left, bottom, width, height] 的颜色条的一些特征,如我所附的另一个答案中所述。

      这些数字非常适合您的情节,您可以根据自己的喜好随意更改。

      【讨论】:

        【解决方案3】:

        为了使颜色条位于绘图顶部,您需要创建一些轴,指定用于承载颜色条。 这可以通过在图形坐标中的某个给定位置放置一个新轴手动完成,

        cax = fig.add_axes([0.2,0.8,0.3,.05])
        fig.colorbar(surf, cax=cax, orientation="horizontal")
        

        或者,通过使用 subplot grid (gridspec),如下所示:

        import matplotlib.pyplot as plt
        from mpl_toolkits.mplot3d.axes3d import Axes3D
        import matplotlib.gridspec as gridspec
        import numpy as np
        
        x = np.arange(-5, 5, 0.25)
        X, Y = np.meshgrid(x,x)
        Z = np.sin(np.sqrt(X**2 + Y**2))
        
        gs = gridspec.GridSpec(2, 2, height_ratios=[0.05,1])
        fig = plt.figure()
        ax  = fig.add_subplot(gs[1,0], projection='3d')
        cax = fig.add_subplot(gs[0,0])
        
        surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap="coolwarm",
                               linewidth=0, antialiased=False, vmin=-1, vmax=1)
        
        fig.colorbar(surf, cax=cax, orientation="horizontal", ticks=[-1,0,1])
        
        plt.show()
        

        【讨论】:

          【解决方案4】:

          对于避免手动创建新轴并允许我们保持颜色条链接到现有绘图轴的方法,我们可以使用 location 关键字(该方法最初改编自 here)。

          位置参数旨在用于引用列表中多个轴的颜色条(如果颜色条仅给出一个轴,则会引发错误),但如果您只是将一个轴放在列表中,它将允许你使用的论点。您可以使用以下代码作为示例:

          import matplotlib.pyplot as plt
          import numpy as np
          
          fig = plt.figure()
          ax = fig.add_subplot(111)
          axp = ax.imshow(np.random.randint(0, 100, (100, 100)))
          cb = plt.colorbar(axp,ax=[ax],location='top')
          plt.show()
          

          产生this plot。从这里,您可以使用典型方法(填充、收缩等)编辑颜色条,以进一步调整绘图的外观。

          公平的警告,我还没有看到这种方法在许多其他地方使用过,它可能不如通过为颜色条创建新轴的额外步骤来实现。

          【讨论】:

            猜你喜欢
            • 2012-10-29
            • 2023-03-29
            • 2021-05-14
            • 1970-01-01
            • 1970-01-01
            • 2013-08-15
            • 1970-01-01
            • 1970-01-01
            • 2020-03-21
            相关资源
            最近更新 更多