0

so I have a canvas with a figure in matplotlib and I would like to add the plot output of a seaborn facetgrid object to it. the seaborn catplot function (which I need) returns me a facetgrid but how can I add the ax to the matplotlib.pyplot.figure ? It would be also fine if I could specify the figure_id (=num) for facetgrid.fig. (normally I would just call sns.swarmplot() and the figure is added)
thanks in advance !!

example code with approaches I tried:

import matplotlib.pyplot as plt 
import seaborn as sns
import pandas as pd

### example data
test_data = {'sample': ['id_1', 'id_2', 'id_1','id_2'], 
             'val': [1, 2, 3, 2], 
             'cond':['cond_1','cond_1','cond_2','cond_2']}
df = pd.DataFrame(data = test_data)


figure_id = 17
fig = plt.figure(num=figure_id)
fg = sns.catplot(x="cond", y="val", hue="sample", kind="point", data = df) # returns facetgrid
### draw fg.ax on figure with num 17


### tried out different ways to copy it into the subplot or ax object
# fig, ax = plt.subplots(1,1, num=figure_id)
# ax = fg.ax  # => shows coordinates but no data
# fig.axes.append(fg.ax)   # also do not work
# fig.add_subplot(fg.subplots)
  • 1
    What you're looking for is not really possible. A hack is found [here](https://stackoverflow.com/questions/35042255/how-to-plot-multiple-seaborn-jointplot-in-subplot/47664533#47664533) but it has several drawbacks and I would not recommend using it (it's merely a proof of concept). – ImportanceOfBeingErnest Nov 24 '19 at 19:54
  • Looking at your code you do not actually need a `catplot`. So just create an axes first and plot a pointplot or whatever you need to that axes. – ImportanceOfBeingErnest Nov 24 '19 at 19:57

0 Answers0