Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading