Graphing with Seaborn: Difference between revisions
| Line 1: | Line 1: | ||
== Very Nice Distplot/Histplot Combination == | == Very Nice Distplot/Histplot Combination == | ||
[https://stackoverflow.com/questions/35538882/seaborn-boxplot-stripplot-duplicate-legend] | |||
<pre> | <pre> | ||
Revision as of 18:57, 4 January 2023
Very Nice Distplot/Histplot Combination
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
tips = sns.load_dataset("tips")
sns.stripplot(x="day", y="total_bill", hue="smoker",
data=tips, jitter=True,
palette="Set2", split=True,linewidth=1,edgecolor='gray')
# Get the ax object to use later.
ax = sns.boxplot(x="day", y="total_bill", hue="smoker",
data=tips,palette="Set2",fliersize=0)
# Get the handles and labels. For this example it'll be 2 tuples
# of length 4 each.
handles, labels = ax.get_legend_handles_labels()
# When creating the legend, only use the first two elements
# to effectively remove the last two.
l = plt.legend(handles[0:2], labels[0:2], bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)