1

I wrote a python function for plotting a series of filled contours taking data from some columns of a dataset composed by a list of numpy arrays using the kdeplot function of seaborn. The code is the following:

import matplotlib.pyplot as plt
import seaborn as sns

def kdeplots_figure(dataset):
    cols = int((len(dataset)-2)/2)
    figure, axes = plt.subplots(1, cols, figsize=(15,6), sharey=True)
    f = 2
    for i in range (len(axes)):
        sns.set_style("white")
        axes[i].set_title(" Cluster {0}".format(i), fontsize = 16)
        axes[i].set_ylabel('Y Axis', fontsize = 14)
        axes[i].set_xlabel('X Axis', fontsize = 14)
        axes[i].set_xlim([95,130])
        axes[i].tick_params(labelsize=13)
        sns.kdeplot(dataset[f],dataset[f+1],cmap="Blues_d",shade_lowest=True,ax=axes[i])
        sns.kdeplot(dataset[f],dataset[f+1],cmap="Blues",shade_lowest=True,shade=True,ax=axes[i])
        f = f+2
    plt.show()

However, some of the contours differ in the space they sample in the x-y plane and when I plot them there is a white portion in the background of some of the graphs that I don't know how to fill. See below:

Plot Output

Both of the graphs should look like the one is located on the right side. I have tried setting up the background color using ax.set_facecolor and taking the last color of the cmap palette with the help of this thread: Getting individual colors from a color map in matplotlib. However, the background color I get is slightly different. I also tried to use the clip argument of sns.kdeplot with no luck.

How can I solve this? Let me know if you need additional information.

webbynator
  • 31
  • 5

1 Answers1

0

So I kind of did a hack and got rid of the grid and used gimp to get the minimum face color. (I used the snipping tool to get a picture of the image, opened it in a free program called gimp, and got the hex code.) Then I changed the face color of the axis. The following code will give you what you want.

import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns

def kdeplots_figure(dataset):
    cols = int((len(dataset)-2)/2)
    figure, axes = plt.subplots(1, cols, figsize=(15,6), sharey=True)

    f = 2
    for i in range (len(axes)):
        sns.set_style("white")
        axes[i].set_title(" Cluster {0}".format(i), fontsize = 16)
        axes[i].set_ylabel('Y Axis', fontsize = 14)
        axes[i].set_xlabel('X Axis', fontsize = 14)

        axes[i].tick_params(labelsize=13)
        axes[i].grid(False) 

        axes[i].set_facecolor('#eef5fc')
        sns.kdeplot(dataset[f],dataset[f+1],cmap="Blues_d",ax=axes[i])
        sns.kdeplot(dataset[f],dataset[f+1],cmap="Blues",  shade=True, 
        extend='both', ax=axes[i])

        f = f+2
    plt.show()

a = np.random.rand(6,6)
kdeplots_figure(a)

enter image description here

user1321988
  • 513
  • 2
  • 6