0

How can you add a graph as a subplot.

I can plot a stand alone graph. But I would like to add this as a 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)

ax1 = plt.subplot2grid((3,3), (0, 0), colspan = 3)
ax2 = plt.subplot2grid((3,3), (1, 0), colspan = 2)

One_plot(ax1,1)
Two_plot(ax2,1)

fig.tight_layout()

Please see output below. I can add the subplot to One Plot but the bar chart in Two Plot gets produced as a stand alone graph. I would like to add it to Two Plot.

This example only displays 2 subplots. My actual code has 8. Whilst there are easier ways to achieve the above solution, I do need to use this technique.

enter image description here If I try assign the bar chart to Plot Two I get an error: SyntaxError: can't assign to function call

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'),

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 = ax2# Error
  • 1
    It's not within a plot. They are two separate subplots. I have altered the question to make it clearer. –  Jun 29 '18 at 06:06
  • 1
    see if [this](https://stackoverflow.com/a/34934631/7546606) solves your problem. – Aman Agarwal Jun 29 '18 at 06: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 '18 at 08:54
  • The subplots are unequal sizes. I cannot use GridSpec –  Jul 02 '18 at 00:24
  • @AmanAgarwal, nahuszanj. Could you please read the question further. The attached questions aren't helpful at all. The duplication tag is hindering this process and anyone trying to attach a subplot using this method. If it still doesn't make sense, please explain. –  Jul 04 '18 at 07:30

2 Answers2

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)

enter image description here

nahusznaj
  • 463
  • 4
  • 15
  • 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. –  Jul 02 '18 at 00:26
  • 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 Jul 02 '18 at 09:27
  • It's in the question. I can add the subplots to plot 1 and 3. I can't add the subplot to plot 2. It gets produced as a stand alone graph –  Jul 03 '18 at 01:07
  • Does the question still not make sense? –  Jul 03 '18 at 22:57
  • @PeterJames123 sorry, I tried to help you. Hope you find a solution to your question and if you do do post it here as an answer! – nahusznaj Jul 04 '18 at 09:03
  • Thanks. I appreciate it. Does the question make sense. I'll have to delete this question because the duplication tag is stopping people from answering! –  Jul 04 '18 at 10:06
0

Answer found here:

How to add a subplot to a group of plots

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 = ax2),