<p>Just change your <code>g1 = sns.histplot(df.Expression)</code>
to <code>g1 = sns.histplot(data=df, x=&quot;Expression&quot;, hue=&quot;Group_name&quot;)</code></p>
<p>Here's the full code:</p>
<pre><code>import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

plt.rcParams[&quot;figure.figsize&quot;] = (12,8)

df = pd.DataFrame({
    &quot;Group_name&quot;: [&quot;GroupA&quot;, &quot;GroupA&quot;, &quot;GroupA&quot;, &quot;GroupA&quot;, &quot;GroupB&quot;, &quot;GroupB&quot;, &quot;GroupB&quot;, &quot;GroupB&quot;, &quot;GroupC&quot;, &quot;GroupC&quot;, &quot;GroupC&quot;, &quot;GroupC&quot;],
    &quot;code-1&quot;: [6, 6, 6, 6, 8, 8, 8, 8, 1, 1, 1, 1],
    &quot;code-2&quot;: [6, 8, 1, 3, 6, 8, 1, 3, 6, 8, 1, 3],
    &quot;Expression&quot;: [1.21, 0.611, 0.448, 0.3587, 0.611, 2.11, 0.60113, 0.534858, 0.448, 0.60113, 2.2, 0.622206]
})
 
g1 = sns.histplot(data=df, x=&quot;Expression&quot;, hue=&quot;Group_name&quot;)
g1.set(
    xlim=(-2,2),
    title=&quot;Expression&quot;
)
g1.axvline(0.0, c='black', linestyle=&quot;--&quot;)
g1.axvline(df.Expression.median(), c='red', linestyle=&quot;--&quot;)

g1.figure.savefig(&quot;hist.png&quot;)
</code></pre>
<p>'''<a href="https://i.sstatic.net/1snf8.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/1snf8.png" alt="New output" /></a></p>

CC BY-SA 4.0
2023-01-22T18:34:09.747Z
Thanks, very impressive Seaborn coding
M__
CC BY-SA 4.0
2023-01-22T18:07:17.613Z
2023-01-22T18:07:17.613Z
Risk Complex
<p>I am using the below script to get the histogram plot for all values together. However, I want a single plot but different colours for different groups (A, B, C)</p>
<p><code>data file</code></p>
<pre><code>Group_name  code-1    code-2    Expression
GroupA  6  6  1.21
GroupA  6  8  0.611
GroupA  6  1  0.448
GroupA  6  3  0.3587
GroupB  8  6  0.611
GroupB  8  8  2.11
GroupB  8  1  0.60113
GroupB  8  3  0.534858
GroupC  1  6  0.448
GroupC  1  8  0.60113
GroupC  1  1  2.2
GroupC  1  3  0.622206
</code></pre>
<p><code>Script</code></p>
<pre><code>import matplotlib.pyplot as plt
import seaborn as sns
plt.rcParams[&quot;figure.figsize&quot;] = (12,8)

g1 = sns.histplot(df.Expression)
g1.set(
    xlim=(-2,2),
    title=&quot;Expression&quot;
)
g1.axvline(0.0, c='black', linestyle=&quot;--&quot;)
g1.axvline(df.Expression.median(), c='red', linestyle=&quot;--&quot;)

<span class="math-container">```</span>
</code></pre>

<python><visualization>
How to get single histogram plot for all groups in different colours?
# How to get single histogram plot for all groups in different colours?

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

I am using the below script to get the histogram plot for all values together. However, I want a single plot but different colours for different groups (A, B, C)
data file
Group_name  code-1    code-2    Expression
GroupA  6  6  1.21
GroupA  6  8  0.611
GroupA  6  1  0.448
GroupA  6  3  0.3587
GroupB  8  6  0.611
GroupB  8  8  2.11
GroupB  8  1  0.60113
GroupB  8  3  0.534858
GroupC  1  6  0.448
GroupC  1  8  0.60113
GroupC  1  1  2.2
GroupC  1  3  0.622206

Script
import matplotlib.pyplot as plt
import seaborn as sns
plt.rcParams["figure.figsize"] = (12,8)

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

```

**Tags:** python, visualization

---

## 1 answers

### Answer by username_1 on 2023-01-22. Score: 1 | Accepted answer

Just change your g1 = sns.histplot(df.Expression)
to g1 = sns.histplot(data=df, x="Expression", hue="Group_name")
Here's the full code:
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

plt.rcParams["figure.figsize"] = (12,8)

df = pd.DataFrame({
    "Group_name": ["GroupA", "GroupA", "GroupA", "GroupA", "GroupB", "GroupB", "GroupB", "GroupB", "GroupC", "GroupC", "GroupC", "GroupC"],
    "code-1": [6, 6, 6, 6, 8, 8, 8, 8, 1, 1, 1, 1],
    "code-2": [6, 8, 1, 3, 6, 8, 1, 3, 6, 8, 1, 3],
    "Expression": [1.21, 0.611, 0.448, 0.3587, 0.611, 2.11, 0.60113, 0.534858, 0.448, 0.60113, 2.2, 0.622206]
})
 
g1 = sns.histplot(data=df, x="Expression", hue="Group_name")
g1.set(
    xlim=(-2,2),
    title="Expression"
)
g1.axvline(0.0, c='black', linestyle="--")
g1.axvline(df.Expression.median(), c='red', linestyle="--")

g1.figure.savefig("hist.png")

'''

> **Comments:**
> **username_2**: Thanks, very impressive Seaborn coding  

---

