在我们进行统计学分析时常常需要用到统计学分析的方法,其中箱线图和柱状图往往能够体现出数据的一些规律。
1. 构建dataframe
from tabulate import tabulate
table=[['A','B','C','D'],[1,2,3,4]
,[12,16,20,24],[20,22,24,26]
,[90,95,100,105]]
#设置表格输出方式,参考文献可以有不同的输出方式
table1=tabulate(table, headers='firstrow', tablefmt='fancy_grid')
print(table1)
x2 = ['A','B', 'C', 'D', 'E']
y=[[152.1680121509372,67.05118740170701,63.996290802112746,25.686278721590124],
[63.996290802112746,63.22252,69.92792,24.593584],
[37.616055,236.06926,63.850693,32.486446],
[202.42195,84.118484,69.26504,37.87419],
[142.63606,69.23667,49.89162,26.870008]]
df = pd.DataFrame(y,index = x2,
columns = pd.Index(['A1','B1','C1','D1'],name='rcm'))
print(df)
import csv
with open("M.csv","w") as csvfile:
writer = csv.writer(csvfile)
#先写入columns_name
writer.writerow(['A','B','C'])
writer.writerows([[152.1680121509372,'A1','A2'],
[67.05118740170701,'B1','B2'],
[63.996290802112746,'C1','C2'],
[25.686278721590124,'D1','D2']]
)
2. seaborn分类
Seaborn可视化-分类统计图catplot
Python数据可视化方法之Seaborn
分类散点图:
stripplot()(使用kind=“strip”; 默认值)
swarmplot()(与kind=“swarm”)
分类分布图:
boxplot()(与kind=“box”)
violinplot()(与kind=“violin”)
boxenplot()(与kind=“boxen”)
分类估计图:
pointplot()(与kind=“point”)
barplot()(与kind=“bar”)
countplot()(与kind=“count”)
代码如下(示例):
mport matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd
代码如下(示例):
绘制柱状图
tips2 = pd.read_csv('surface energy2.csv')
tips2.head()
df2 = pd.DataFrame(tips2)
sns.catplot(x="energy", y="value", hue="rcm", kind="bar", data=df2);
g = sns.catplot(x="energy", y="value", hue="rcm", kind="bar", data=df2)
sns.swarmplot(x="energy", y="value", size=3, data=df2, ax=g.ax)
2. 箱线图
tips = sns.load_dataset("tips")
sns.catplot(x="day", y="total_bill", kind="box", data=tips)
sns.catplot(x="day", y="total_bill", hue="smoker", kind="box", data=tips)
g = sns.catplot(x="day", y="total_bill", hue="smoker", kind="box", data=tips)
sns.swarmplot(x="day", y="total_bill", hue="smoker", data=tips)
经过上述的试验,对seaborn绘制柱状图和箱线图有了一定的了解。