Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sync Colors across Subplots of different types Seaborne / Matplotlib

I am trying to create a subplot with two plots. The first plot is essentially a scatter plot (i'm using regplot) and the second is a histogram.

my code is as follows:

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

data = {'source':['B1','B1','B1','C2','C2','C2'],
        'depth':[1,4,9,1,3,10],
        'value':[10,4,23,78,24,45]}

df = pd.DataFrame(data)

f, (ax1, ax2) = plt.subplots(1,2)

for source in df['source'].unique():

    x = df.loc[df['source'] == source, 'value']
    y = df.loc[df['source'] == source, 'depth']

    sns.regplot(x,
                y,
                scatter = True,
                fit_reg = False,
                label = source,
                ax = ax1)
    ax1.legend()

    sns.distplot(x,
                 bins = 'auto',
                 norm_hist =True,
                 kde = True,
                 rug = True,
                 ax = ax2,
                 label = source)
    ax2.legend()
    ax2.relim()
    ax2.autoscale_view()
plt.show()

The result is shown below.

enter image description here

As you can see, the colors between the scatter and the histogram are different. Now, I had a play around with color pallets and all, which has not worked. Can anyone shed any light on how I can sync the colors?

Thanks.

like image 355
BillyJo_rambler Avatar asked Aug 22 '17 16:08

BillyJo_rambler


People also ask

What does PLT subplot 1 2 2 mean?

The subplot() Function #the figure has 1 row, 2 columns, and this plot is the first plot. plt.subplot(1, 2, 2) #the figure has 1 row, 2 columns, and this plot is the second plot.

How do I make subplots different sizes matplotlib?

To change the size of subplots in Matplotlib, use the plt. subplots() method with the figsize parameter (e.g., figsize=(8,6) ) to specify one size for all subplots — unit in inches — and the gridspec_kw parameter (e.g., gridspec_kw={'width_ratios': [2, 1]} ) to specify individual sizes.

How do I specify colors on a matplotlib plot?

The usual way to set the line color in matplotlib is to specify it in the plot command. This can either be done by a string after the data, e.g. "r-" for a red line, or by explicitely stating the color argument.

Can you use Seaborn with subplots?

In this article, we will explore how to create a subplot or multi-dimensional plot in seaborn, It is a useful approach to draw subplot instances of the same plot on different subsets of your dataset. It allows a viewer to quickly extract a large amount of data about complex information.


1 Answers

Make use of the hue_order parameter.

From seaborn documentation : seaborn.countplot(*, x=None, y=None, hue=None, data=None, order=None, hue_order=None, orient=None, color=None, palette=None, saturation=0.75, dodge=True, ax=None, **kwargs)

order, hue_order: lists of strings, optional Order to plot the categorical levels in, otherwise the levels are inferred from the data objects

Eg:

hue_order = target_0['CODE_GENDER'].unique()

plt.subplot(2,2,1) sns.countplot(x='INCOME_BRACKET', hue ='GENDER',data = df_0,hue_order=hue_order,palette = 'mako'); plt.title("Non-defaulters : Income bracket b/w Gender - Target 0");

plt.subplot(2,2,2) sns.countplot(x='INCOME_BRACKET', hue ='GENDER',data = df_1,hue_order=hue_order,palette = 'mako'); plt.title("Defaulters : >Income bracket b/w Gender - Target 1" );

Output as seen below :

Two subplots with synced colors

I realise this is an old question. However, this is quite simple(not sure if this was option was available before) which I couldn't find in any of the other answers and these didn't work for some reason too. So, this answer is for others who are still struggling with this.

like image 130
Sneha Valabailu Avatar answered Oct 10 '22 00:10

Sneha Valabailu