[seaborn] seaborn学习笔记4-核密度图DENSITYPLOT

4 核密度图Densityplot

(代码下载)
核密度图显示数值变量的分布。它只需要一组数值作为输入。它非常类似于直方图。在seaborn中使用kdeplot函数绘制核密度图,该章节主要内容有:

  1. 基础核密度图绘制 Basic density plot
  2. 核密度图的区间控制 Control bandwidth of density plot
  3. 多个变量的核密度图绘制 Density plot of several variables
  4. 边际核密度图 Marginal Density plot
#调用seaborn
import seaborn as sns
#调用seaborn自带数据集
df = sns.load_dataset('iris')
#显示数据集
df.head()
sepal_lengthsepal_widthpetal_lengthpetal_widthspecies
05.13.51.40.2setosa
14.93.01.40.2setosa
24.73.21.30.2setosa
34.63.11.50.2setosa
45.03.61.40.2setosa

1. 基础核密度图绘制 Basic density plot

  • 基础核密度图函数 default density function
  • 带阴影的核密度图 Density plot with shade
  • 水平核密度图 Horizontal density plot
# 基础核密度图函数 default density function
# 纵坐标为核密度估计值,类似概率密度函数。
# 如x=3.0,纵坐标表示的不是x=3.0处的概率,而是在x=3.0附近取值的可能性估计
sns.kdeplot(df['sepal_width']);
C:\ProgramData\Anaconda3\lib\site-packages\scipy\stats\stats.py:1713: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[seq]`. In the future this will be interpreted as an array index, `arr[np.array(seq)]`, which will result either in an error or a different result.
  return np.add.reduce(sorted[indexer] * weights, axis=axis) / sumval

png

# 带阴影的核密度图 Density plot with shade
# density plot with shade 添加阴影线
sns.kdeplot(df['sepal_width'], shade=True);

png

# 水平核密度图 Horizontal density plot
# 基本所有seaborn绘图函数只要设置vercical就能获得水平方向的图像
sns.kdeplot(df['sepal_width'], shade=True, vertical=True, color="skyblue");

png

2. 核密度图的区间控制 Control bandwidth of density plot

# bw参数控制核密度图的区间
# 其中bw表示根据多少区间范围来计算核密度
# Large bandwidth 
sns.kdeplot(df['sepal_width'], shade=True, bw=.5, color="olive");

png

 # Narrower bandwidth
sns.kdeplot(df['sepal_width'], shade=True, bw=.05, color="olive");

png

3. 多个变量的核密度图绘制 Density plot of several variables

# 有时需要比较多个变量的核密度,可以通过matplotlib创建两个子图,也可以直接画在一张图上
p1=sns.kdeplot(df['sepal_width'], shade=True, color="r")
p1=sns.kdeplot(df['sepal_length'], shade=True, color="b")

png

4. 边际核密度图 Marginal Density plot

# 边际图能够更好变现2个数值变量之间的关系
# No space 无间隔,space表示边缘图和中央图像的间隔,kind表示图像类型
sns.jointplot(x=df["sepal_length"], y=df["sepal_width"], kind='kde', color="grey", space=0);

png

# Huge space 大间隔
sns.jointplot(x=df["sepal_length"], y=df["sepal_width"], kind='kde', space=3);

png

# Make marginal bigger, ratio表示中央图像和边缘图的比例,ratio越大,比例越大
sns.jointplot(x=df["sepal_length"], y=df["sepal_width"], kind='kde',ratio=1);

png

sns.jointplot(x=df["sepal_length"], y=df["sepal_width"], kind='kde',ratio=10);

png

<think>好的,用户现在问的是关于Matplotlib中可用样式列表的介绍,特别是他们列出了一长串样式名称。首先,我需要确认用户的需求。看起来他们可能在使用某个版本的Matplotlib时,遇到了样式名称的问题,或者他们想了解不同样式的用途,以便在绘图时选择合适的外观。 回顾之前的对话历史,用户之前遇到了“seaborn-darkgrid”样式名称报错的问题,我解释了版本变化导致的名称变更,并提供了解决方案。现在用户给出的列表应该是他们运行`plt.style.available`后得到的可用样式列表,希望了解每个样式的特点。 接下来,我需要将这些样式进行分类,方便用户理解。常见的分类可能有经典样式、出版物风格、现代主题、特殊用途等。例如,`classic`和`dark_background`属于基础样式,而`seaborn`相关的则是现代主题,`ggplot`模仿R语言的ggplot2,`fivethirtyeight`模仿特定的数据新闻网站风格。 然后,每个样式需要简要描述其视觉特征和适用场景。比如,`Solarize_Light2`有浅色背景和高对比度,适合演示;`dark_background`适合暗黑模式下的展示。对于带有版本号的seaborn样式,如`seaborn-v0_8-darkgrid`,要说明这是新版本中的命名方式,避免用户混淆。 还要注意用户可能的使用场景。比如,学术论文可能需要简洁的`grayscale`,而演讲可能需要更鲜艳的`seaborn-bright`或`seaborn-poster`。此外,用户可能需要知道如何应用这些样式,是否需要额外安装库,比如seaborn相关的样式是否需要安装seaborn库。 需要检查是否有遗漏的样式,并确认每个样式的描述准确。例如,`_classic_test_patch`和`_mpl-gallery`可能属于内部或测试用样式,用户一般不需要关注,但还是要简单说明。同时,提醒用户某些样式可能在不同版本中存在差异,建议升级到最新版本以获得所有功能。 最后,给出使用建议,比如如何列出所有可用样式,如何应用样式,以及常见问题处理,比如样式不生效时的解决方法。可能还需要提及版本兼容性,特别是Matplotlib 3.5之后的版本变化,确保用户不会再次遇到之前的名称错误问题。 总结来说,需要将列表中的每个样式分门别类,简明扼要地描述其特点,提供使用示例,并给出实用建议,帮助用户根据需求选择合适的样式,同时避免潜在的兼容性问题。</think>以下是Matplotlib内置样式的详细技术说明和适用场景分类(基于Matplotlib 3.6+版本): --- ### 一、核心样式分类 #### 1. 经典样式集 | 样式名称 | 视觉特征 | 适用场景 | |-----------------------|---------------------------------|-------------------------| | `classic` | 灰白背景+黑色坐标轴 | 基础科研绘图 | | `grayscale` | 全灰阶配色(兼容黑白打印) | 论文/出版物 | | `dark_background` | 黑色背景+亮色元素 | 暗黑模式/投影演示 | | `bmh` | 浅蓝网格+柔和配色 | 贝叶斯统计可视化 | #### 2. 现代主题包 ```python # Seaborn系列(需安装seaborn库) 'seaborn-v0_8' # 基础模板 'seaborn-v0_8-darkgrid' # 深灰网格(经典科研风格) 'seaborn-v0_8-whitegrid' # 白底细网格(商务风格) 'seaborn-v0_8-deep' # 高饱和度色轮 'seaborn-v0_8-muted' # 低饱和度色轮 'seaborn-v0_8-pastel' # 粉彩色系 'seaborn-v0_8-bright' # 霓虹色系 'seaborn-v0_8-dark' # 暗色系 'seaborn-v0_8-colorblind' | 色盲友好配色(8种) 'seaborn-v0_8-poster' # 大字号+粗线条(海报制作) 'seaborn-v0_8-talk' # 中字号(学术报告) 'seaborn-v0_8-notebook' # 默认输出优化(Jupyter) ``` #### 3. 第三方风格复刻 ```python 'ggplot' # R语言ggplot2风格(红褐网格+矩形元素) 'fivethirtyeight' # 538数据新闻风格(灰底+粗体标题) 'tableau-colorblind10' # Tableau色盲安全色板 ``` --- ### 二、特殊用途样式 #### 1. 性能优化 ```python 'fast' # 最小化图形元素(关闭抗锯齿/阴影等) # 适用场景:大数据量快速渲染 ``` #### 2. 测试专用 ```python '_classic_test_patch' # 经典样式+圆形标记(单元测试专用) '_mpl-gallery' # 示例图统一风格 '_mpl-gallery-nogrid' # 无网格版本 ``` #### 3. 光照效果 ```python 'Solarize_Light2' # 模拟日光效果的浅色主题 # 特征:米黄背景+蓝橙对比色 ``` --- ### 三、样式应用示例 #### 基础使用方法 ```python import matplotlib.pyplot as plt # 设置全局样式 plt.style.use('seaborn-v0_8-darkgrid') # 创建图形 fig, ax = plt.subplots() ax.plot([1,2,3], [4,5,6], label='示例曲线') ax.set_title('样式演示') ax.legend() plt.show() ``` #### 临时样式切换 ```python with plt.style.context('dark_background'): plt.plot(np.random.randn(100).cumsum()) # 该代码块内使用暗黑主题 ``` --- ### 四、样式特征参数对比 通过以下代码查看具体样式配置: ```python # 查看'seaborn-v0_8-darkgrid'的详细配置 print(plt.style.library['seaborn-v0_8-darkgrid']) ``` 典型配置参数示例: ```python axes.facecolor: #EAEAF2 # 坐标系背景色 axes.grid: True # 显示网格 grid.color: white # 网格线颜色 grid.linewidth: 0.8 # 线宽 font.family: sans-serif # 字体类型 ``` --- ### 五、最佳实践建议 1. **版本兼容方案** ```python try: plt.style.use('seaborn-darkgrid') # 旧版本写法 except ValueError: plt.style.use('seaborn-v0_8-darkgrid') # 3.6+版本写法 ``` 2. **混合样式应用** ```python plt.style.use(['seaborn-v0_8', 'ggplot']) # 后者覆盖前者冲突参数 ``` 3. **自定义修改** ```python plt.rcParams.update({ 'font.size': 12, # 覆盖样式中的字体设置 'lines.linewidth': 2.5 }) ``` --- ### 六、扩展知识 1. **样式文件存储路径** 系统级样式文件位于: `matplotlib.get_configdir() + '/stylelib/'` 用户自定义样式可放置于: `~/.matplotlib/stylelib/your_style.mplstyle` 2. **创建自定义样式** 新建`.mplstyle`文件并添加: ```ini # my_style.mplstyle axes.grid: True grid.color: #CCCCCC font.size: 10 ``` 使用方式:`plt.style.use('my_style')`
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值