是大数据管理研究的作业,虽然我大部分都是抄的我们课代表的,但是也从中学到了不少画图技巧。所以特地开一个帖子记下来:

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

#---------读取数据-----------#
names = 'Sex,Length,Diameter,Height,Whole weight,Shucked weight,Viscera weight,Shell weight,Rings'.split(',')
abalone = pd.read_csv('abalone.txt', names=names)
#---------------------------#

#---------绘图设定-----------#
#风格
sns.set(style='darkgrid')
#字体
plt.rcParams['font.family'] = 'SimHei'
#存储格式
plt.rcParams["savefig.format"] = 'png'
#存储路径
path = os.path.dirname(__file__) + '/charts/'
if not os.path.exists(path):
    os.makedirs(path)
#---------------------------#

#--------性别比例饼图---------#
f = plt.figure(figsize=(7,5))
label = ['M', 'I', 'F']
explode = [0.01, 0.01, 0.01]
M_I_F = abalone['Sex'].value_counts().values.tolist()
plt.pie(M_I_F, explode=explode, labels=label, autopct='%.2f')
plt.legend()
title = '性别比例饼图'
plt.title(title)
plt.savefig(path + title)
#---------------------------#


#------性别-参数-箱线图-------#
f, axes = plt.subplots(3,3, figsize=(13, 13))
for i in range(1, len(abalone.columns)):
    r = (i-1)//3
    c = (i-1)%3
    sns.boxplot(y=abalone.columns[i], x='Sex', hue='Sex', data=abalone, ax=axes[r, c], fliersize=1)
    axes[r,c].get_legend().remove()
    axes[r,c].set_title(abalone.columns[i])
    axes[r,c].set_xlabel('')
    axes[r,c].set_ylabel('')
f.subplots_adjust(wspace=0.4, hspace=0.4)
handles, labels = axes[0,0].get_legend_handles_labels()
axes[0,2].legend(handles, labels, loc='upper right', bbox_to_anchor=(1.15, 1.15))
title = '性别-参数-箱线图'
f.suptitle(title)
plt.savefig(path + title)
#---------------------------#


#------性别-参数-直方图-------#
M = abalone[abalone['Sex'] == 'M']
F = abalone[abalone['Sex'] == 'F']
I = abalone[abalone['Sex'] == 'I']
f, axes = plt.subplots(3,3, figsize=(13,13))
for i in range(1, len(abalone.columns)):
    r = (i-1)//3
    c = (i-1)%3
    ylabel = abalone.columns[i]
    x = [M[ylabel], F[ylabel], I[ylabel]]
    axes[r, c].hist(x, label=list("MFI"))
    axes[r, c].set_title(ylabel)
f.subplots_adjust(wspace=0.4, hspace=0.4)
handles, labels = axes[2,1].get_legend_handles_labels()
axes[0,2].legend(handles, labels, loc='upper right', bbox_to_anchor=(1.15, 1.15))
title = '性别-参数-直方图'
f.suptitle(title)
plt.savefig(path + title)
#---------------------------#


#----Sex-Based-PairGrid-----#
pg = sns.PairGrid(abalone, hue='Sex', palette='Set2')
pg.map_diag(plt.hist, bins=20, alpha=0.5)
pg.map_offdiag(plt.scatter, s=1, alpha=0.5)
pg.add_legend()
title = 'Sex-Based-PairGrid'
pg.fig.suptitle(title)
plt.savefig(path + title)
#---------------------------#


#------性别-参数-散点图-------#
#Length vs Diameter
fg = sns.FacetGrid(abalone, col='Sex', hue='Sex', palette="Set2", height=3, aspect=1)
fg.map(plt.scatter, 'Diameter', 'Length', s=1)
fg.fig.subplots_adjust(top=0.8)
title = 'Length vs Diameter'
pg.fig.suptitle(title)
plt.savefig(path + title)
#Height vs Diameter
fg = sns.FacetGrid(abalone, col='Sex', hue='Sex', palette="Set2", height=3, aspect=1)
fg.map(plt.scatter, 'Diameter', 'Height', s=1)
fg.fig.subplots_adjust(top=0.8)
title = 'Height vs Diameter'
pg.fig.suptitle(title)
plt.savefig(path + title)
#Length vs Whole weight
fg = sns.FacetGrid(abalone, col='Sex', hue='Sex', palette="Set2", height=3, aspect=1)
fg.fig.subplots_adjust(top=0.8)
fg.map(plt.scatter, 'Whole weight', 'Length', s=1)
title = 'Length vs Whole weight'
pg.fig.suptitle(title)
plt.savefig(path + title)
#Rings vs Diameter
fg = sns.FacetGrid(abalone, col='Sex', hue='Sex', palette="Set2", height=3, aspect=1)
fg.fig.subplots_adjust(top=0.8)
title = 'Rings vs Diameter'
fg.map(plt.scatter, 'Diameter', 'Rings', s=1)
pg.fig.suptitle(title)
plt.savefig(path + title)
#Rings vs Whole weight
fg = sns.FacetGrid(abalone, col='Sex', hue='Sex', palette="Set2", height=3, aspect=1)
fg.fig.subplots_adjust(top=0.8)
title = 'Rings vs Whole weight'
fg.map(plt.scatter, 'Rings', 'Whole weight', s=1)
pg.fig.suptitle(title)
plt.savefig(path + title)
#Rings vs Shucked weight
fg = sns.FacetGrid(abalone, col='Sex', hue='Sex', palette="Set2", height=3, aspect=1)
fg.fig.subplots_adjust(top=0.8)
title = 'Rings vs Shucked weight'
fg.map(plt.scatter, 'Rings', 'Shucked weight', s=1)
pg.fig.suptitle(title)
plt.savefig(path + title)
#Rings vs Viscera weight
fg = sns.FacetGrid(abalone, col='Sex', hue='Sex', palette="Set2", height=3, aspect=1)
fg.fig.subplots_adjust(top=0.8)
title = 'Rings vs Viscera weight'
fg.map(plt.scatter, 'Rings', 'Viscera weight', s=1)
pg.fig.suptitle(title)
plt.savefig(path + title)
#Rings vs Shell weight
fg = sns.FacetGrid(abalone, col='Sex', hue='Sex', palette="Set2", height=3, aspect=1)
fg.fig.subplots_adjust(top=0.8)
title = 'Rings vs Shell weight'
fg.map(plt.scatter, 'Rings', 'Shell weight', s=1)
pg.fig.suptitle(title)
plt.savefig(path + title)