Home > Mobile >  Is there a way to hide unique identifier of Linear Segmented Colormap object?
Is there a way to hide unique identifier of Linear Segmented Colormap object?

Time:07-30

I am trying to write a function that visualizes a color bar in a subplot, accidentally stuck with a unique identifier problem! The way I want to write a function is to have a colorbar and two additional subplots. The code for the Linear Segmented Colormap is given below.

I am running the python file from command prompt. The output is popped up as a window. I want this function to work as jupyter (.ipynb) and as script file (.py) too.

def cb(fig, ax, cmap):
    cmap = cm.get_cmap(cmap)

    norm = mpl.colors.Normalize(vmin=1, vmax=5)
    bounds = [i for i in range(5   1)]
    fig.colorbar(mpl.cm.ScalarMappable(norm=norm, cmap=cmap),
                 cax=ax, orientation='horizontal', label=cmap, ticks=bounds)
    ax.plot()

The above function is for creating a subplot of colorbar in a segmented view. The below code is for creating a plot grid.

def color_bar(cmap):
    fig = plt.figure()
    fig.set_figheight(6)
    fig.set_figwidth(6)

    ax1 = plt.subplot2grid(shape=(2, 2), loc=(0, 0), colspan=2)
    ax2 = plt.subplot2grid(shape=(2, 2), loc=(1, 0), colspan=1)
    ax3 = plt.subplot2grid(shape=(2, 2), loc=(1, 1), colspan=1)

    x = np.arange(0, 10, 0.1)
    y = np.cos(x)

    cb(fig, ax1, cmap)
    ax1.set_title('ax1')
    ax2.plot(x, y)
    ax2.set_title('ax2')
    ax3.plot(x, y)
    ax3.set_title('ax3')

    plt.tight_layout()


color_bar("copper")
plt.show();

The output of the following code contains the unique identifier of the linear segmented colormap, How to hide that linear segmented colormap identifier?

Output

Hope experts could help me! Thanks in advance

CodePudding user response:

In this call

    fig.colorbar(mpl.cm.ScalarMappable(norm=norm, cmap=cmap),
                 cax=ax, orientation='horizontal', label=cmap, ticks=bounds)

you have label=cmap. So the label becomes str(cmap), which is not want you want. Change that argument to the desired label, or remove that argument if you don't want a label on the colorbar.

  • Related