How to add a subplot
How to add a subplot
How can you add a graph as a subplot
.
subplot
I can plot
a stand alone graph
. But I would like to add this as a subplot
.
plot
graph
subplot
import pandas as pd
import matplotlib.pyplot as plt
d = ({
'A' : ['1','1','1','2','2','2','3','3','3','3'],
'B' : ['A','B','C','A','B','C','D','A','B','C'],
'C' : ['John','Carl','Carl','John','Lily','John','Lily','John','Carl','Carl'],
'D' : [1,2,3,4,5,6,7,8,9,10],
'E' : [0,2,4,6,5,6,7,8,9,10],
})
df = pd.DataFrame(data=d)
fig = plt.figure(figsize = (9,4))
def One_plot(ax,pid, fontsize=12):
ax.set_title('One Plot', fontsize=10)
ax.scatter(df['E'],df['D'])
ax.grid(False)
def Two_plot(ax,pid):
df.assign(A=df.A.astype(int)).pivot_table(index="C", columns="B", values="A",aggfunc='count').rename_axis(None).rename_axis(None,1).plot(kind='bar')
ax.set_title('Two Plot', fontsize=10)
def Three_plot(ax,pid, fontsize=12):
ax.set_title('Three Plot', fontsize=10)
ax.plot(df['E'],df['D'])
ax.grid(False)
ax1 = plt.subplot2grid((3,3), (0, 0), colspan = 3)
ax2 = plt.subplot2grid((3,3), (1, 0), colspan = 2)
ax3 = plt.subplot2grid((3,3), (2, 0))
One_plot(ax1,1)
Two_plot(ax2,1)
Three_plot(ax3,1)
fig.tight_layout()
Please see output below. I can add the subplots
to Plot One
and Plot Three
but the bar chart
gets produced as a stand alone graph. I would like to add it to Two Plot
.
subplots
Plot One
Plot Three
bar chart
Two Plot
This example only displays 3 subplots
. My actual code has 8. Whilst there are easier ways to achieve the above solution, I do need to use this technique.
subplots
It's not within a plot. They are two separate subplots. I have altered the question to make it clearer.
– PeterJames123
Jun 29 at 6:06
see if this solves your problem.
– Aman Agarwal
Jun 29 at 6:22
@AmanAgarwal I think you could mark that as duplicate? @PeterJames123 I don't understand what you're wanting to obtain. I think there are more simple ways to get two plots. Also what is your output? Deleting the line
plt.show()
I obtained two plots one with one subplot and one with two.– nahusznaj
Jun 29 at 8:54
plt.show()
Possible duplicate of Matplotlib - adding subplots to a subplot?
– Aman Agarwal
Jun 29 at 9:23
1 Answer
1
I'm not entirely sure I interpreted the question correctly...
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
d = ({
'A' : ['1','1','1','2','2','2','3','3','3','3'],
'B' : ['A','B','C','A','B','C','D','A','B','C'],
'C' : ['John','Carl','Carl','John','Lily','John','Lily','John','Carl','Carl'],
})
df = pd.DataFrame(data=d)
fig, (ax1, ax2, ax3) = plt.subplots(1,3, figsize=(13,5))
sns.countplot(x = df.C, ax = ax1)
sns.countplot(x = df.B, ax = ax2)
sns.countplot(x = df.C, hue =df.B, ax = ax3)
Please refer to question. I have added more information. I understand there are easier ways to achieve this. My actual plot is a lot more complex as it contains numerous subplots of unequal size.
– PeterJames123
14 hours ago
could you show what you want to obtain with a picture? I can't understand what is the output that you're trying to obtain.
– nahusznaj
5 hours ago
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Possible duplicate of How to add different graphs (as an inset) in another python graph
– Ash Sharma
Jun 29 at 6:04