A strip plot is a scatter plot where one of the variables is categorical. They can be combined with other plots to provide additional information. For example, a boxplot with an overlaid strip plot becomes more similar to a violin plot because some additional information about how the underlying data is distributed becomes visible.
dataset:
In [66]:
%matplotlib inline
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
plt.rcParams['figure.figsize'] = (20.0, 10.0)
plt.rcParams['font.family'] = "serif"
In [67]:
from sklearn import datasets
iris_data = datasets.load_iris()
In [68]:
df = pd.DataFrame(data=np.column_stack([iris_data['data'],iris_data['target']]),
columns=iris_data['feature_names'] + ['species'])
In [69]:
df['species'] = iris_data['target_names'][df.species.astype(int)]
Basic plot
In [126]:
p = sns.stripplot(data=df, x='species',y='sepal length (cm)',jitter=1,marker='^',hue='species')
handles,labels = p.get_legend_handles_labels()
for l in labels[:3]:
l += " Sepal Length"
print(l)
for l in labels[:3]:
print(l)
plt.legend(handles, labels)
# sns.stripplot(data=df, x='species',y='sepal width (cm)',palette='magma',jitter=1,marker='s',hue='species')
# handles,labels = p.get_legend_handles_labels()
# # for h in handles[:2]:
# # h.set_label(h.get_label() + " Sepal Length")
# # for h in handles[2:]:
# # h.set_label(h.get_label() + " Sepal Width")
# for l in labels[2:]:
# l+= " Sepal Width"
# plt.legend(handles, labels)
Out[126]:
In [89]:
#ns.stripplot(data=df, x='species',y='sepal length (cm)',jitter=1,marker='^',hue='species')
#sns.stripplot(data=df, x='species',y='sepal width (cm)',palette='magma',jitter=1,marker='^',hue='species')
In [150]:
fig, ax = plt.subplots(1,1)
p = sns.stripplot(data=df, x='species',y='sepal length (cm)',jitter=1,marker='^',hue='species',ax = ax)
handles,labels = ax.get_legend_handles_labels()
new_labels = []
for l in labels:
new_labels.append(l.capitalize() + " Sepal Length")
plt.legend(handles, new_labels)
Out[150]:
In [143]:
for i in labels:
i=0
print(labels)
In [109]:
plt.show()
In [100]:
dir(h[0])
Out[100]:
In [102]:
h[3].get_label()
Out[102]:
In [ ]: