Search code examples
pythonseabornpairplot

seaborn.pairplot() changing the color of each graph


I am trying to produce a simple pairplot with each graph with a separate color. I don't know if this is possible as I am not using hue.

My dataset is as such:

      High Jump  Discus Throw  Long Jump
0           859           732       1061
1           749           823        975
2           887           778        866
3           878           790        898
4           803           789        913
     ...           ...        ...
7963        714           571        760
7964        767           573        845
7965        840           461        804
7966        758           487        720
7967        714           527        809

My code and graph looks as such:

t = sns.pairplot(new)

enter image description here

Is there any way to make this more colourful?


Solution

  • Since PairGrid automatically passes a color attribute to the plotting function, one way to get a different color per plot is to create your own plotting function that ignores the color passed by PairGrid (note that you loose the possibility to color code by hues obviously)

    colors = iter(['xkcd:red purple', 'xkcd:pale teal', 'xkcd:warm purple',
           'xkcd:light forest green', 'xkcd:blue with a hint of purple',
           'xkcd:light peach', 'xkcd:dusky purple', 'xkcd:pale mauve',
           'xkcd:bright sky blue', 'xkcd:baby poop green', 'xkcd:brownish',
           'xkcd:moss green', 'xkcd:deep blue', 'xkcd:melon',
           'xkcd:faded green', 'xkcd:cyan', 'xkcd:brown green',
           'xkcd:purple blue', 'xkcd:baby shit green', 'xkcd:greyish blue'])
    
    def my_scatter(x,y, **kwargs):
        kwargs['color'] = next(colors)
        plt.scatter(x,y, **kwargs)
    
    def my_hist(x, **kwargs):
        kwargs['color'] = next(colors)
        plt.hist(x, **kwargs)
    
    iris = sns.load_dataset("iris")
    g = sns.PairGrid(iris)
    g.map_diag(my_hist)
    g.map_offdiag(my_scatter)
    

    enter image description here