I have a pandas data frame (df) which looks like following:enter image description here I was able to create a box plot and scatter plot separately from the given data frame using the following code:
JavaScript
x
15
15
1
x=df.columns
2
y1=df.iloc[10]
3
y2=df.iloc[13]
4
y3=df.iloc[14]
5
6
#Create a scatter plot
7
plt.scatter(x,y1)
8
plt.scatter(x,y2)
9
plt.scatter(x,y3)
10
plt.xticks([2017,2030,2040,2050])
11
plt.yticks([20,30,40,50,60])
12
13
df.plot(kind="box")
14
plt.ylim(20,70)
15
The results look like this: enter image description here
I want to add the dots from the scatter plot which represent only three rows of the data frame into the boxplot. I was able to recreate this using Excel enter image description here. However, how can I formulate my code to do it using matplotlib in Python?
Advertisement
Answer
If I understand correctly you want this:
JavaScript
1
19
19
1
df = pd.DataFrame({a: np.random.randint(0,10,100) for a in 'abc'})
2
3
fig, ax = plt.subplots()
4
5
x=range(1, len(df.columns)+1)
6
7
y1=df.iloc[10]
8
y2=df.iloc[13]
9
y3=df.iloc[14]
10
11
#Create a scatter plot
12
ax.scatter(x,y1)
13
ax.scatter(x,y2)
14
ax.scatter(x,y3)
15
16
ax.boxplot(df.values);
17
18
ax.set_xticklabels(df.columns);
19