Search code examples
pythonseaborncolorbarkdeplot

Hiding values next to seaborn colormap


Plot I'm getting right now

Here's a picture of the plot I get for a bivariate density. However the numbers next to the colorbar are really not of much use here, and I would like either to delete them or replace by 'LOW' and 'HIGH'. I didn't find any way of doing this so if any of you has info, it would be great. Here's the code i used for creation of this plot :

            df = self.dfuinsights
            ax = sns.kdeplot(data = df, x = key1, y = key2, fill = True, alpha = 0.7, tresh = tsh, levels = lvls, cmap = "viridis")
            xlabel = self.colnamesMapper_uinsights[key1]
            ylabel = self.colnamesMapper_uinsights[key2]
            title = "Bivariate Densities : " + xlabel + " - " + ylabel
            self.__convert_capitals(ax, title, xlabel, ylabel) #just modifying titles and xlabel, ignore this)
            plt.show()
            plt.close()

Solution

  • kdeplot can take arguments to be passed to the colorbar in cbar_kws=. To remove the ticks+labels, you can request an empty list of ticks:

    geyser = sns.load_dataset("geyser")
    sns.kdeplot(
        data=geyser, x="waiting", y="duration",
        fill=True, thresh=0, levels=100, cmap="viridis",
        cbar=True, cbar_kws=dict(ticks=[])
    )
    

    enter image description here

    Alternatively, if you capture the return value from kdeplot(), you should be able to get a reference to the colorbar axes, and then you can use any of the functions provided by Axes to modify ticks and ticklabels

    ax = sns.kdeplot(
        data=geyser, x="waiting", y="duration",
        fill=True, thresh=0, levels=100, cmap="viridis",
        cbar=True
    )
    cax = ax.figure.axes[-1]  # This assume there are no other axes in the figure
    cax.set_yticklabels([])