adin
adin

Reputation: 833

Add patch to Seaborn jointploint

I have created a patch and want to apply it to a jointplot in Seaborn. When I go to try to apply the patch, it either splits the plots into two graphics or, if I change the kind attribute in the jointplot function from kde to anything else, it throws an error inner got multiple values for keyword argument 'ax'.

When I try to apply this solution, the variable fg does not have the attribute axes and it does not work.

In the code below, if I use kind = "scatter" and omit the ax, I get a blank output then the jointpolot. If I use kind = "scatter" and add ax = ax, I get the above mentioned error. If I use kind = "kde" and ax = ax, I get the following images:

Image 1

Image 2

My code:

import descartes
import fiona
import matplotlib.pyplot as plt
import seaborn as sns
from shapely.geometry import shape
import pandas as pd
import time
#
start_time  = time.time()
input_csv   = r"C:\path\to\a\csv\with\coordinates.csv"
shapefile   = r"C:\path\to\a\fun\shapefile.shp"
df          = pd.read_csv(input_csv, delimiter = ",")
df          = df[df["Latitude"] > 37.70833]
lat         = "Latitude"
lon         = "Longitude"
fig         = plt.figure()
ax          = fig.add_subplot(111, frameon = False)
shp         = fiona.open(shapefile)
pol         = shp.next()
geom        = shape(pol["geometry"])
un_sf       = geom.envelope.symmetric_difference(geom)
un_sf_patch = descartes.PolygonPatch(un_sf)
ax.add_patch( un_sf_patch )
my_fig      = sns.jointplot(x = lon, y = lat, data = df, color = "grey", kind = "scatter")
end_time    = round(time.time() - start_time, 5)
print "Seconds elapsed: {0}".format(end_time)

How can I add the patch to my Seaborn jointplot in a single graphic?

Upvotes: 4

Views: 1074

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339052

A seaborn jointplot creates its own figure, together with 3 axes.

g = sns.jointgrid(..)
g.ax_joint   # big axes in the middle
g.ax_marg_y  # marginal axes
g.ax_marg_x

Here you want to add your patch to the ax_joint.

g = sns.jointgrid(..)
g.ax_joint.add_patch( un_sf_patch )

Upvotes: 3

Related Questions