pyhton数据可视化seaborn(一) -- 整体风格

本文详细介绍了Seaborn,一个基于matplotlib的Python图形可视化库,它简化了统计图表的创建过程。通过讲解其API、数据输入要求、整体风格设置、颜色调色板选择以及常用图表类型,帮助读者掌握如何高效使用Seaborn提升数据可视化效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

什么是Seaborn

Seaborn是基于matplotlib的图形可视化python包。它提供了一种高度交互式界面,便于用户能够做出各种有吸引力的统计图表。

Seaborn是在matplotlib的基础上进行了更高级的API封装,从而使得作图更加容易,在大多数情况下使用seaborn能做出很具有吸引力的图,而使用matplotlib就能制作具有更多特色的图。应该把Seaborn视为matplotlib的补充,而不是替代物。同时它能高度兼容numpy与pandas数据结构以及scipy与statsmodels等统计模式。

seaborn API

Seaborn 要求原始数据的输入类型为 pandas 的 Dataframe 或 Numpy 数组,画图函数有以下几种形式:

sns.图名(x=‘X轴 列名’, y=‘Y轴 列名’, data=原始数据df对象)
sns.图名(x=‘X轴 列名’, y=‘Y轴 列名’, hue=‘分组绘图参数’, data=原始数据df对象)
sns.图名(x=np.array, y=np.array[, …])

整体风格设置

对图表整体颜色、比例等进行风格设置,包括颜色色板等

调用系统风格进行数据可视化

set() / set_style() / axes_style() / set_context()

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
sns.set() 设置样式参数

seaborn.set(context =‘notebook’,style =‘darkgrid’,palette =‘deep’,font =‘sans-serif’,font_scale = 1,color_codes = True,rc = None)

# 创建正弦函数及图表

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()

在这里插入图片描述

sns.set()  # 暗格显示
sinplot()
plt.grid(linestyle = '--') 

在这里插入图片描述

set_style() 设置图标风格

seaborn.set_style(style = None,rc = None )

# set_style()
# 切换seaborn图表风格
# 风格选择包括:"white", "dark", "whitegrid", "darkgrid", "ticks" 
# rc:dict,可选,参数映射以覆盖预设的seaborn样式字典中的值


fig = plt.figure(figsize=(6,6))

ax1 = fig.add_subplot(2,1,1)
sns.set_style("whitegrid")
data = np.random.normal(size=(20,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()  # 子图显示

在这里插入图片描述

sns.despine() 设置坐标轴
'''
despine()

设置图表坐标轴

seaborn.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=(6,9))
plt.subplots_adjust(hspace=0.3) # 调整子图间距

ax1= fig.add_subplot(3,1,1)
sinplot()
sns.despine()  # 默认删除上、右坐标轴

ax2 = fig.add_subplot(3,1,2)
sns.violinplot(data=data)

#sns.despine(offset=10,trim=True)   # offset: 与坐标轴之间的偏移,trim: 为True时,将坐标轴限制在数据最大最小值

ax3 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jepson2017

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值