I have a question about overlapping graphs inside one panel in Python.
I generated numbers of two groups
JavaScript
x
13
13
1
import numpy as np
2
cv1 = np.random.normal(50, 4, 1000)
3
cv2 = np.random.normal(40, 7, 1000)
4
5
6
import matplotlib.pyplot as plt
7
8
plt.hist(cv1, numpy.linspace(0, 70, 100))
9
plt.hist(cv2, numpy.linspace(0, 70, 100))
10
plt.xlabel("Grain weight (mg)", size=12)
11
plt.ylabel("Frequency_Histogram", size=12)
12
plt.show()
13
It works perfectly. However, if the data is the format of data frame, how can I make the same graph?
For example, I simulated that I have two data set for different genotypes, cv1, cv2. In each data, grain weight for 1,000 grains was measured.
JavaScript
1
6
1
cv1_data = {"Genotype": ["cv1"]*1000,"AGW": cv1}
2
cv1_weight = DataFrame(cv1_data)
3
4
cv2_data = {"Genotype": ["cv2"]*1000,"AGW": cv2}
5
cv2_weight = DataFrame(cv2_data)
6
Then, I’d like to make the same graph above. So I tried below codes
JavaScript
1
6
1
plt.hist(cv1,y="AGW", numpy.linspace(0, 70, 100))
2
plt.hist(cv2,y="AGW", numpy.linspace(0, 70, 100))
3
plt.xlabel("Grain weight (mg)", size=12)
4
plt.ylabel("Frequency_Histogram", size=12)
5
plt.show()
6
but it does not work. Could you let me know how to make the same overlapping graph in case of data frame?
Always, many thanks!!!
Advertisement
Answer
You can do that using Seaborn library in python
JavaScript
1
19
19
1
import numpy as np
2
import pandas as pd
3
import seaborn as sns
4
5
6
cv1 = np.random.normal(50, 4, 1000)
7
cv2 = np.random.normal(40, 7, 1000)
8
9
cv1_data = {"Genotype": ["cv1"]*1000,"AGW": cv1}
10
cv1_weight = pd.DataFrame(cv1_data)
11
12
cv2_data = {"Genotype": ["cv2"]*1000,"AGW": cv2}
13
cv2_weight = pd.DataFrame(cv2_data)
14
15
df = pd.concat([cv1_weight,cv2_weight],axis = 1)
16
df.columns = ['Genotype', 'cv1_AGW', 'Genotype', 'cv2_AGW']
17
18
sns.histplot(data = df[['cv1_AGW','cv2_AGW']])
19