标签:arm 折线 palette int spine aac 查看 method port
以下操作都是翻译的官方文档(不全)
安装seaborn
pip3 install seaborn
seborn加载数据集
import seaborn as sb
df = sb.load_dataset(‘tips‘)
print(type(df))
print(df.head)
可以看出df的类型是<class ‘pandas.core.frame.DataFrame‘>,所以在使用seaborn之前需要安装pandas
查看支持多少数据集
import seaborn as sb
print(sb.get_dataset_names())
不安装bs4会报错
pip3 install bs4
matplotlib画图
import numpy as np
from matplotlib import pyplot as plt
def sinplot(flip=1):
x = np.linspace(0, 14, 100)
for i in range(1, 5):
plt.plot(x, np.sin(x + i * .5) * (7 - i) * flip)
sinplot()
plt.show()
sb.set()
转化为seaborn的默认格式
import numpy as np
from matplotlib import pyplot as plt
def sinplot(flip=1):
x = np.linspace(0, 14, 100)
for i in range(1, 5):
plt.plot(x, np.sin(x + i * .5) * (7 - i) * flip)
import seaborn as sb
sb.set()
sinplot()
plt.show()
set_style()
设置画图格式,可供选择的如下
如下
import numpy as np
from matplotlib import pyplot as plt
def sinplot(flip=1):
x = np.linspace(0, 14, 100)
for i in range(1, 5):
plt.plot(x, np.sin(x + i * .5) * (7 - i) * flip)
import seaborn as sb
sb.set_style(‘whitegrid‘)
sinplot()
plt.show()
sb.despine()
在white 和 ticks 主题下,可以通过这个函数去掉上部和右侧的图像边框线
自定义样式
sb.set_style()
查看格式中有多少元素,比入线的粗细等等
一个demo
sb.set_style("darkgrid", {‘axes.axisbelow‘: False})
import pandas as pd
import seaborn as sb
from matplotlib import pyplot as plt
df = sb.load_dataset(‘iris‘) #鸢尾花数据集
sb.distplot(df[‘petal_length‘],kde=False)
plt.show()
其中kde设置为False代表只画直方图,设置为True代表除了直方图还有折线图
import pandas as pd
import seaborn as sb
from matplotlib import pyplot as plt
df = sb.load_dataset(‘iris‘)
sb.jointplot(x=‘petal_length‘,y=‘petal_width‘,data=df)
plt.show()
在数据密度稀疏的情况下,二元数据分析采用六边形二元化方法,当数据非常零散且难以通过散点图进行分析时可以用这种方法。
import pandas as pd
import seaborn as sb
from matplotlib import pyplot as plt
df = sb.load_dataset(‘iris‘)
sb.jointplot(x=‘petal_length‘,y=‘petal_width‘,data=df,kind=‘hex‘)
plt.show()
核密度估计是一种估计变量分布的非参数方法。在seaborn中,我们可以使用jointplot()
kind参数使用kde
import pandas as pd
import seaborn as sb
from matplotlib import pyplot as plt
df = sb.load_dataset(‘iris‘)
sb.jointplot(x=‘petal_length‘,y=‘petal_width‘,data=df,kind=‘kde‘)
plt.show() v
import pandas as pd
import seaborn as sb
from matplotlib import pyplot as plt
df = sb.load_dataset(‘iris‘)
sb.distplot(df[‘petal_length‘],hist=False)
plt.show()
hist设置为false即生成核密度估计图
seaborn.pairplot(data,…)
参数介绍
parameter | description |
---|---|
data | Datafram |
hue | Variable in data to map plot aspects to different colors. |
palette | Set of colors for mapping the hue variable |
kind | Kind of plot for the non-identity relationships. {‘scatter’, ‘reg’} |
diag_kind | Kind of plot for the diagonal subplots. {‘hist’, ‘kde’} |
直接复制的官方文档的介绍
import pandas as pd
import seaborn as sb
from matplotlib import pyplot as plt
df = sb.load_dataset(‘iris‘)
sb.set_style("ticks")
sb.pairplot(df,hue=‘species‘,diag_kind="kde",kind="scatter",palette="husl")
plt.show()
import pandas as pd
import seaborn as sb
from matplotlib import pyplot as plt
df = sb.load_dataset(‘iris‘)
sb.stripplot(x="species", y="petal_length", data=df)
plt.show()
Swarmplot()
另外一种风格
import pandas as pd
import seaborn as sb
from matplotlib import pyplot as plt
df = sb.load_dataset(‘iris‘)
sb.swarmplot(x="species", y="petal_length", data=df)
plt.show()
import pandas as pd
import seaborn as sb
from matplotlib import pyplot as plt
df = sb.load_dataset(‘titanic‘)
sb.barplot(x="sex", y="survived", hue="class", data=df)
plt.show()
省略了几种柱状图
import pandas as pd
import seaborn as sb
from matplotlib import pyplot as plt
df = sb.load_dataset(‘tips‘)
sb.regplot(x="total_bill", y="tip", data=df)
sb.lmplot(x="total_bill", y="tip", data=df)
plt.show()
regplot vs lmplot
regplot | lmplot |
---|---|
接受各种格式的x和y变量,包括简单的numpy数组、pandas系列对象,或作为pandas数据帧中变量的引用 | 将数据作为必需参数,并且必须将x和y变量指定为字符串。这种数据格式称为“长格式”数据 |
当其中一个变量取离散值时,我们也可以拟合线性回归
import pandas as pd
import seaborn as sb
from matplotlib import pyplot as plt
df = sb.load_dataset(‘tips‘)
sb.lmplot(x="size", y="tip", data=df)
plt.show()
标签:arm 折线 palette int spine aac 查看 method port
原文地址:https://www.cnblogs.com/realwuxiong/p/13140263.html