图表可视化--Seaborn

基于matplatlib的python数据可视化库,提升更高层次的API封装,包括一些高级图表可视化等工具。javascript

 

 主要技术点

  • 风格及调色盘
  • 分布数据可视化
  • 分类数据可视化
  • 线性关系数据可视化
  • 其余图标可视化
  • 结构化图表可视化

 

 风格及调色盘  

 

 

In [ ]:
"""
对图表总体颜色、比例等进行风格设置,包括颜色色板等
调用系统风格进行数据可视化


set()/set_style()/axes_style()/despine()/set_context()
"""
In [9]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
warnings.filterwarnings("ignore")
In [10]:
# 建立正弦函数以及图表

def sinplot(flip=1):
    x = np.linspace(0,14,100)
    for i in range(1,7):
        plt.plot(x,np.sin(x+i*0.5)*(7-i)*flip)
sinplot()
# 建立正弦函数,以及出图
 
In [11]:
sns.set()   # 一旦设置,全局都回更改,只有重启能够修改
def sinplot(flip=1):
    x = np.linspace(0,14,100)
    for i in range(1,7):
        plt.plot(x,np.sin(x+i*0.5)*(7-i)*flip)
sinplot()
plt.grid(linestyle='--')
# 建立正弦函数,以及出图
 
In [12]:
# 二、set_style()
# 设置seaborn图表风格 white    dark    whitegrid  darkgrid  ticks
fig = plt.figure(figsize=(10,6))
ax1 = fig.add_subplot(2,1,1)
sns.set_style('darkgrid')     # 修改须要重启
data = np.random.normal(size=(10,6))+np.arange(6)/2

sns.boxplot(data=data)

plt.title('style - whitegrid')

ax2 = fig.add_subplot(2,1,2)
sns.set_style('dark')
sinplot() # 子图显示      仍然能够使用原来的
 
In [13]:
# despine()
# 设置图表坐标轴
"""
sns.despine(
    ['fig=None', 'ax=None', 'top=True', 'right=True', 'left=False', 'bottom=False', 'offset=None', 'trim=False'],
) 
"""
sns.set_style('ticks')# 设置风格

fig = plt.figure(figsize=(10,6))
plt.subplots_adjust(hspace=0.3)
# 建立图表

ax1 = fig.add_subplot(3,1,1)
sinplot()
sns.despine()
"""
全局做用
sns.despine( 
    ['fig=None', 'ax=None', 'top=True', 'right=True', 'left=False', 'bottom=False', 'offset=None', 'trim=False'],
)
offset  与坐标轴之间的偏移
trim 为True时,将坐标轴限制在数据最大最小值
top、。。bottom 布尔型,为True时不显示,默认是不显示上右

"""



with sns.axes_style('darkgrid'): # 局部设置风格
    ax2 = fig.add_subplot(3,1,2)
    sns.violinplot(data=data)  # 小提琴图
     




ax3 = fig.add_subplot(3,1,3)
sns.boxplot(data=data,palette='deep')# 直方图
Out[13]:
<matplotlib.axes._subplots.AxesSubplot at 0x189501a7f60>
 
In [14]:
# set_context()
# 设置显示比例尺度
"""
sns.set_context(context=None, font_scale=1, rc=None)
context    :    paper, notebook, talk, poster
"""

sns.set_context('notebook')
sinplot()
 
In [ ]:
 
In [ ]:
相关文章
相关标签/搜索