Seaborn
安装
conda install seaborn
加载数据集
import seaborn as sns
anscombe = sns.load_dataset('anscombe')
dataset_1 = anscombe[anscombe.dataset == 'I']
多变量图表
def recode_sex(sex):
if sex == 'Femail':
return 0
else:
return 1
tips['sex_color'] = tips['sex'].apply(recode_sex)
scatter_plot = plt.figure()
axes1 = scatter_plot.add_subplot(1, 1, 1)
axes1.scatter(x=tips['total_bill'], y=tips['tip'], s=tips['size']*10, c=tips['sex_color'], alpha=0.5)
axes1.set_title('Total Bill vs Tip color by Sex and sized by Size')
axes1.set_xlabel('Total Bill')
axes1.set_ylabel('Tip')
scatter_plot.show()
单变量图标
直方图
hist = sns.distplot(tips['total_bill'])
hist.set_title('Total Bill Histogram with Density Plot')
去掉 kde:
hist = sns.distplot(tips['total_bill'], kde=False)
hist.set_title('Total Bill Histogram')
hist.set_xlabel('Total Bill')
hist.set_ylabel('Frequency')
plt.show()
只要 kde:
hist = sns.distplot(tips['total_bill'], hist=False)
hist.set_title('Total Bill Density')
hist.set_xlabel('Total Bill')
hist.set_ylabel('Unit Probability')
plt.show()
添加地毯图:
hist = sns.distplot(tips['total_bill'], rug=True)
hist.set_title('Total Bill Histogram with Desity and Rug Plot')
hist.set_xlabel('Total Bill')
plt.show()
条形图
count = sns.countplot('day', data=tips)
count.set_title('Count of days')
count.set_xlabel('Day of the Week')
count.set_ylabel('Frequency')
plt.show()
散点图:
scatter = sns.regplot(x='ttotal_bill', y='tip', data=tips)
scatter.set_title('Scatterplot of Total Bill and Tip')
scatter.set_xlabel('Total Bill')
scatter.set_ylabel('Tip')
plt.show()
fig = sns.lmplot(x='total_bill', y='tip', data=tips)
plt.show()
hbin = sns.jointplot(x='total_bill', y='tip', data=tips, kind='hex')
hbin = set_axis_labels(xlabel='Total Bill', ylabel='Tip')
hbin.fig.suptitle('Hexbin Joint plot of Total Bill and Tip')
plt.show()
kde = sns.kdeplot(data=tips['total_bill'], data2=tips['tip'], shade=True)
kde.set_title('Kernel Density Plot of Total Bill and Tip')
kde.set_xlabel('Total Bill')
kde.set_ylabel('Tip')
plt.show()
kde_joint = sns.jointplot(x='total_bill', y='tip', data=tips, kind='kde')
plt.show()
bar = sns.barplot(x='time', y='total_bill', data=tips)
bar.set_title('Barplot of average total bill for time of day')
bar.set_xlabel('Time of day')
bar.set_ylabel('Average total bill')
plt.show()
box = sns.boxplot(x='time', y='total_bill', data=tips)
box.set_title('Box plot of total bill by time of day')
box.set_xlabel('Time of day')
box.set_ylabel('Total Bill')
plt.show()
小提琴图:
violin = sns.violinplot(x='time', y='total_bill', data=tips)
violin.set_title('Violin plot of total bill by time of day')
violin.set_xlabel('Time of day')
violin.set_ylabel('Total Bill')
plt.show()
九宫格:
tips = tips[['total_bill', 'tip', 'size']]
pair_grid = sns.PairGrid(tips)
pair_grid = pair_grid.map_upper(sns.regplot)
pair_grid = pair_grid.map_lower(sns.kdeplot)
pair_grid = pair_grid.map_diag(sns.distplot, rug=True)
plt.show()
violin = sns.violinplot(x='time', y='total_bill', hue='sex', data=tips, split=True)
plt.show()
scatter = sns.lmplot(x='total_bill', y='tip', data=tips, hue='sex', fit_reg=False)
plt.show()
pair_plot = sns.pairplot(tips, hue='sex')
plt.show()
scatter = sns.lmplot(x='total_bill', y='tip', data=tips, fit_reg=False, hue='sex', markers=['o', 'x'], scatter_kws={'s': tips['size']*10})
plt.show()
网格图:
anscombe = sns.lmplot(x='x', y='y', data=anscombe, fit_reg=False, col='dataset', col_wrap=2)
plt.show()
多面图:
facet = sns.FaceGrid(tips, col='time')
facet.map(sns.distplot, 'total_bill', rug=True)
plt.show()
facet = sns.FaceGrid(tips, col='day', hue='sex')
facet.map(plt.scatter, 'total_bill', 'tip')
facet = facet.add_legend()
plt.show()
facet = sns.lmplot(x='total_bill', y='tip', data=tips, fit_reg=False, hue='sex', col='day')
plt.show()
facet = sns.FaceGrid(tips, col='time', row='smoker', hue='sex')
facet.map(plt.scatter, 'total_bill', 'tip')
factor_plot = sns.factorplot(x='day', y='total_bill', hue='sex', data=tips, row='smoker', col='time', kind='violin')
plt.show()