<p>You could use the <a href="https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.groupby.html" rel="nofollow noreferrer">groupby()</a> pandas function to group the dataframe by gene name. And then just loop through each group to plot the histogram of correlation values. I think you can use the mean of the ratio (deviation / median) to compare deviation for different genes:</p>
<pre><code>import matplotlib.pyplot as plt

for name, group in df.groupby('Gene'):

    median = group['Correlation'].median()
    deviation = abs(group['Correlation'] - median)
    ratio = deviation / median
    ratio_mean = ratio.mean()

    id_1 = group['ID_1'].astype(str)
    id_2 = group['ID_2'].astype(str)

    group['Combination'] = id_1 + '/' + id_2

    plt.figure(figsize=(8, 6))
    plt.bar(group['Combination'], group['Correlation'])

    plt.xlabel('Combination', fontsize=12, fontweight='bold')
    plt.ylabel('Correlation', fontsize=12, fontweight='bold')

    plt.xticks(rotation=45, ha=&quot;right&quot;) 
    plt.title(f'{name} (deviation ratio: {ratio_mean.round(3)})')
    plt.tight_layout()

    plt.gca().spines['top'].set_visible(False)
    plt.gca().spines['right'].set_visible(False)
    
    plt.show()
</code></pre>
<p><a href="https://i.sstatic.net/FRqki.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/FRqki.png" alt="CCL2" /></a></p>
<p><a href="https://i.sstatic.net/QAY8t.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/QAY8t.png" alt="IL10" /></a></p>
<p><a href="https://i.sstatic.net/zhzld.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/zhzld.png" alt="TNF" /></a></p>

CC BY-SA 4.0
2023-01-16T11:28:01.683Z
Is there a way to also get correlation matrix based on the above `df` data frame for each gene separately? Thank you
Riya
CC BY-SA 4.0
2023-01-16T11:55:45.337Z
@Riya I think what you want is the [corr()](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.corr.html) function for this: `corr_matrix = group[['Combination', 'Correlation']].corr()`
Steve
CC BY-SA 4.0
2023-01-16T12:22:56.467Z
Thank you Steve
Riya
CC BY-SA 4.0
2023-01-15T12:59:08.977Z
2023-01-15T12:59:08.977Z
Steve
<p>I have a data frame (df) which has correlations calculated for different genes with respect to different ID combinations. I want to get separate histogram plot based on  the gene name (separate plot for separate gene). Also after plotting I want to know which genes have more deviations in their correlation values (Like more deviated from the median in each plot).</p>
<p>Example:
<code>df</code></p>
<pre><code>Gene    ID_1    ID_2    Correlation
TNF 1175077258  1175075956  0.626115
TNF 1175077261  1175075956  0.542499
TNF 1175077261  1175077258  0.50113
TNF 1175077263  1175075956  0.431587
TNF 1175077263  1175077258  0.734858
TNF 1175077263  1175077261  0.622206
CCL2    952399885   952399878   0.632703
CCL2    952399894   952399878   0.622244
CCL2    952399894   952399885   0.697313
CCL2    952399923   952399878   0.687826
CCL2    952399923   952399885   0.612089
CCL2    952399923   952399894   0.603834
IL10    1068768000  1068767927  0.118955
IL10    1068768147  1068767927  0.32038
IL10    1068768147  1068768000  0.430287
IL10    1068768409  1068767927  0.335264
IL10    1068768409  1068768000  0.406426
IL10    1068768409  1068768147  0.546452
</code></pre>
<p>I tried the below code which only works when combining all genes together.</p>
<pre><code>import matplotlib.pyplot as plt
import seaborn as sns
plt.rcParams[&quot;figure.figsize&quot;] = (8,6)

g1 = sns.histplot(df.Correlation)
g1.set(
    xlim=(-1,1),
    title=&quot;correlation&quot;
)
g1.axvline(0.0, c='black', linestyle=&quot;--&quot;)
g1.axvline(df.Correlation.median(), c='red', linestyle=&quot;--&quot;)
</code></pre>
<p>The result/plot I am expecting is this (I have created this example plot in excel)</p>
<p><a href="https://i.sstatic.net/vghLg.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/vghLg.jpg" alt="enter image description here" /></a></p>

<python><visualization><correlation><pandas>
How to get separate histograms plots on the basis of the column value?How to detect which plot has most deviation?
# How to get separate histograms plots on the basis of the column value?How to detect which plot has most deviation?

(Asked by: username_0 on 2023-01-15)

I have a data frame (df) which has correlations calculated for different genes with respect to different ID combinations. I want to get separate histogram plot based on  the gene name (separate plot for separate gene). Also after plotting I want to know which genes have more deviations in their correlation values (Like more deviated from the median in each plot).
Example:
df
Gene    ID_1    ID_2    Correlation
TNF 1175077258  1175075956  0.626115
TNF 1175077261  1175075956  0.542499
TNF 1175077261  1175077258  0.50113
TNF 1175077263  1175075956  0.431587
TNF 1175077263  1175077258  0.734858
TNF 1175077263  1175077261  0.622206
CCL2    952399885   952399878   0.632703
CCL2    952399894   952399878   0.622244
CCL2    952399894   952399885   0.697313
CCL2    952399923   952399878   0.687826
CCL2    952399923   952399885   0.612089
CCL2    952399923   952399894   0.603834
IL10    1068768000  1068767927  0.118955
IL10    1068768147  1068767927  0.32038
IL10    1068768147  1068768000  0.430287
IL10    1068768409  1068767927  0.335264
IL10    1068768409  1068768000  0.406426
IL10    1068768409  1068768147  0.546452

I tried the below code which only works when combining all genes together.
import matplotlib.pyplot as plt
import seaborn as sns
plt.rcParams["figure.figsize"] = (8,6)

g1 = sns.histplot(df.Correlation)
g1.set(
    xlim=(-1,1),
    title="correlation"
)
g1.axvline(0.0, c='black', linestyle="--")
g1.axvline(df.Correlation.median(), c='red', linestyle="--")

The result/plot I am expecting is this (I have created this example plot in excel)

**Tags:** python, visualization, correlation, pandas

---

## 1 answers

### Answer by username_1 on 2023-01-15. Score: 2 | Accepted answer

You could use the groupby() pandas function to group the dataframe by gene name. And then just loop through each group to plot the histogram of correlation values. I think you can use the mean of the ratio (deviation / median) to compare deviation for different genes:
import matplotlib.pyplot as plt

for name, group in df.groupby('Gene'):

    median = group['Correlation'].median()
    deviation = abs(group['Correlation'] - median)
    ratio = deviation / median
    ratio_mean = ratio.mean()

    id_1 = group['ID_1'].astype(str)
    id_2 = group['ID_2'].astype(str)

    group['Combination'] = id_1 + '/' + id_2

    plt.figure(figsize=(8, 6))
    plt.bar(group['Combination'], group['Correlation'])

    plt.xlabel('Combination', fontsize=12, fontweight='bold')
    plt.ylabel('Correlation', fontsize=12, fontweight='bold')

    plt.xticks(rotation=45, ha="right") 
    plt.title(f'{name} (deviation ratio: {ratio_mean.round(3)})')
    plt.tight_layout()

    plt.gca().spines['top'].set_visible(False)
    plt.gca().spines['right'].set_visible(False)
    
    plt.show()

> **Comments:**
> **username_0**: Is there a way to also get correlation matrix based on the above `df` data frame for each gene separately? Thank you  
> **username_1**: @Riya I think what you want is the [corr()](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.corr.html) function for this: `corr_matrix = group[['Combination', 'Correlation']].corr()`  
> **username_0**: Thank you Steve  

---

