<p>Real data is better, particularly if genes names are wanted (complicated simulation) .... anyway there you go. Simulated values under a negative binomial which are pretty realistic to the data supplied (that'll be negative binomial, just not ranked).</p>
<pre class="lang-py prettyprint-override"><code>import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

np.random.seed(2)
s1 = list(np.random.negative_binomial(1, 0.1, 500))
s2 = list(np.random.negative_binomial(1, 0.5, 500))
mydict = {'A':s1, 'B':s2}
df = pd.DataFrame(mydict)
fig, ax = plt.subplots()

heights, bins = np.histogram(df['A'])
heights2, bins2 = np.histogram(df['B'], bins=bins)

width = (bins[1] - bins2[0])/3

ax.bar(bins[:-1], heights, width=width, facecolor='coral')
ax.bar(bins2[:-1]+width, heights2, width=width, facecolor='blue')
</code></pre>
<p><a href="https://i.sstatic.net/gYD7d.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/gYD7d.png" alt="enter image description here" /></a></p>
<hr />
<p>Here's horizontal bars (minor differences) with slightly different negative binomial values used ...</p>
<pre><code># same imports as before 

np.random.seed(2)
s1 = list(np.random.negative_binomial(1, 0.1, 500))
s2 = list(np.random.negative_binomial(1, 0.3, 500))
mydict = {'A':s1, 'B':s2}
df = pd.DataFrame(mydict)
fig, ax = plt.subplots()

heights, bins = np.histogram(df['A'])
heights2, bins2 = np.histogram(df['B'], bins=bins)

width = (bins[1] - bins2[0])/3

ax.barh(bins[:-1], heights, width, facecolor='coral')
ax.barh(bins2[:-1]+width, heights2, width, facecolor='blue')
ax.invert_yaxis()
plt.show()

</code></pre>
<p><a href="https://i.sstatic.net/Kf2qA.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Kf2qA.png" alt="enter image description here" /></a></p>
<hr />
<p>To adapt it you just need to provide an appropriate set of values that will then be fitted to a dataframe.</p>

CC BY-SA 4.0
2023-03-25T15:16:55.687Z
2023-06-27T19:55:35.763Z
2023-06-27T19:55:35.763Z
M__
<p>How do we plot histogram in R\Python with two variables[BC and Control] and their corresponding gene names examples is below:</p>
<p><a href="https://i.sstatic.net/W6oID.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/W6oID.png" alt="enter image description here" /></a></p>
<p>Thank you,</p>

CC BY-SA 4.0
2023-03-25T10:00:49.810Z
That doesn't look like a histogram but a bar plot. To draw it with ggplot2 and `geom_col()`, check out this reference: [https://ggplot2.tidyverse.org/reference/geom_bar.html](https://ggplot2.tidyverse.org/reference/geom_bar.html)
Cloudberry
CC BY-SA 4.0
2023-03-25T10:39:15.307Z
Yeah, I was just given an example through bar plot well, thanks @Cloudberry
Mahendra Singh
CC BY-SA 4.0
2023-03-25T22:12:33.337Z
How is a bar plot an example for a histogram? They're two different kinds of plots.
Ram RS
<r><python><proteins><ggplot2>
Histogram plot using R/python with two variables[BC and Control] and their corresponding gene names
# Histogram plot using R/python with two variables[BC and Control] and their corresponding gene names

(Asked by: username_0 on 2023-03-25)

How do we plot histogram in R\Python with two variables[BC and Control] and their corresponding gene names examples is below:

[![enter image description here](https://i.sstatic.net/W6oID.png)](https://i.sstatic.net/W6oID.png)

Thank you,

**Tags:** r, python, proteins, ggplot2
> **Comments:**
> **username_1**: That doesn't look like a histogram but a bar plot. To draw it with ggplot2 and `geom_col()`, check out this reference: [https://ggplot2.tidyverse.org/reference/geom_bar.html](https://ggplot2.tidyverse.org/reference/geom_bar.html)  
> **username_0**: Yeah, I was just given an example through bar plot well, thanks @Cloudberry  
> **username_2**: How is a bar plot an example for a histogram? They're two different kinds of plots.  

---

## 1 answers

### Answer by username_3 on 2023-03-25. Score: 1

Real data is better, particularly if genes names are wanted (complicated simulation) .... anyway there you go. Simulated values under a negative binomial which are pretty realistic to the data supplied (that'll be negative binomial, just not ranked).
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

np.random.seed(2)
s1 = list(np.random.negative_binomial(1, 0.1, 500))
s2 = list(np.random.negative_binomial(1, 0.5, 500))
mydict = {'A':s1, 'B':s2}
df = pd.DataFrame(mydict)
fig, ax = plt.subplots()

heights, bins = np.histogram(df['A'])
heights2, bins2 = np.histogram(df['B'], bins=bins)

width = (bins[1] - bins2[0])/3

ax.bar(bins[:-1], heights, width=width, facecolor='coral')
ax.bar(bins2[:-1]+width, heights2, width=width, facecolor='blue')



Here's horizontal bars (minor differences) with slightly different negative binomial values used ...
# same imports as before 

np.random.seed(2)
s1 = list(np.random.negative_binomial(1, 0.1, 500))
s2 = list(np.random.negative_binomial(1, 0.3, 500))
mydict = {'A':s1, 'B':s2}
df = pd.DataFrame(mydict)
fig, ax = plt.subplots()

heights, bins = np.histogram(df['A'])
heights2, bins2 = np.histogram(df['B'], bins=bins)

width = (bins[1] - bins2[0])/3

ax.barh(bins[:-1], heights, width, facecolor='coral')
ax.barh(bins2[:-1]+width, heights2, width, facecolor='blue')
ax.invert_yaxis()
plt.show()




To adapt it you just need to provide an appropriate set of values that will then be fitted to a dataframe.


---

