Seaborn--facetgrid画图(五)

本文深入探讨了使用Seaborn库进行数据可视化的多种方法,包括直方图、散点图、回归图、箱形图等,展示了如何通过FacetGrid和PairGrid对复杂数据集进行多维度分析。
%matplotlib inline
import numpy as np
import pandas as pd
import seaborn as sns
from scipy import stats
import matplotlib as mpl
import matplotlib.pyplot as plt

sns.set(style="ticks")
np.random.seed(sum(map(ord, "axis_grids")))
tips = sns.load_dataset("tips")
tips.head()

在这里插入图片描述

g = sns.FacetGrid(tips, col="time")
g.map(plt.hist, "tip")

在这里插入图片描述

g = sns.FacetGrid(tips, col="sex", hue="smoker")
g.map(plt.scatter, "total_bill", "tip", alpha=.7)
g.add_legend();

在这里插入图片描述

g = sns.FacetGrid(tips, row="smoker", col="time", margin_titles=True)
g.map(sns.regplot, "size", "total_bill", color=".1", fit_reg=False, x_jitter=.1);

在这里插入图片描述

g = sns.FacetGrid(tips, col="day", size=4, aspect=.5)
g.map(sns.barplot, "sex", "total_bill")

在这里插入图片描述

from pandas import Categorical
ordered_days = tips.day.value_counts().index
print (ordered_days)
ordered_days = Categorical(['Thur', 'Fri', 'Sat', 'Sun'])
g = sns.FacetGrid(tips, row="day", row_order=ordered_days,
                  size=1.7, aspect=4,)
g.map(sns.boxplot, "total_bill")

在这里插入图片描述

pal = dict(Lunch="seagreen", Dinner="gray")
g = sns.FacetGrid(tips, hue="time", palette=pal, size=5)
g.map(plt.scatter, "total_bill", "tip", s=50, alpha=.7, linewidth=.5, edgecolor="white")
g.add_legend()

在这里插入图片描述

g = sns.FacetGrid(tips, hue="sex", palette="Set1", size=5, hue_kws={"marker": ["^", "v"]})
g.map(plt.scatter, "total_bill", "tip", s=100, linewidth=.5, edgecolor="white")
g.add_legend()

在这里插入图片描述

with sns.axes_style("white"):
    g = sns.FacetGrid(tips, row="sex", col="smoker", margin_titles=True, size=2.5)
g.map(plt.scatter, "total_bill", "tip", color="#334488", edgecolor="white", lw=.5);
g.set_axis_labels("Total bill (US Dollars)", "Tip");
g.set(xticks=[10, 30, 50], yticks=[2, 6, 10]);
g.fig.subplots_adjust(wspace=.02, hspace=.02);
#g.fig.subplots_adjust(left  = 0.125,right = 0.5,bottom = 0.1,top = 0.9, wspace=.02, hspace=.02)

在这里插入图片描述

iris = sns.load_dataset("iris")
g = sns.PairGrid(iris)
g.map(plt.scatter)

在这里插入图片描述

g = sns.PairGrid(iris, hue="species")
g.map_diag(plt.hist)
g.map_offdiag(plt.scatter)
g.add_legend()

在这里插入图片描述

g = sns.PairGrid(iris, vars=["sepal_length", "sepal_width"], hue="species")
g.map(plt.scatter

在这里插入图片描述

g = sns.PairGrid(tips, hue="size", palette="GnBu_d")
g.map(plt.scatter, s=50, edgecolor="white")
g.add_legend()

在这里插入图片描述

<think>好的,我现在需要帮助用户解决在Matplotlib中找不到'seaborn-v0_8-whitegrid'样式的问题。首先,我得回忆一下用户提到的引用内容,特别是引用[1]提到从Matplotlib 3.6开始,seaborn样式被弃用,但保留为'seaborn-v0_8-<style>'格式。用户可能正在使用较新版本的Matplotlib,导致旧样式名称不可用。 用户的问题是如何正确使用seaborn样式,尤其是当指定'seaborn-v0_8-whitegrid'找不到时。首先,我应该确认用户是否正确地使用了新的样式名称。比如,是否应该去掉'v0_8'部分?或者是否需要更新Seaborn库? 根据引用[2]和[3],Matplotlib现在推荐直接使用Seaborn的API,而不是通过Matplotlib的样式。因此,可能的解决方案是建议用户直接使用Seaborn来设置样式,比如sns.set_style("whitegrid")。同时,确保用户安装了最新版本的Seaborn,因为旧版本可能不兼容新的Matplotlib。 另外,用户可能需要检查Matplotlib的版本是否在3.6及以上,如果是的话,可能需要使用更新后的样式名称,如'seaborn-v0_8-whitegrid',但用户提到这个名称找不到,可能存在拼写错误或者版本问题。需要让用户确认是否正确输入样式名,或者是否存在其他可用样式。 还有,引用[5]提到在Jupyter中使用%matplotlib inline来显示图表,用户可能需要确认他们的环境设置是否正确,是否需要重启内核或更新库。 总结可能的解决步骤: 1. 更新Matplotlib和Seaborn到最新版本。 2. 使用Seaborn的API直接设置样式,如sns.set_style()。 3. 检查正确的样式名称,可能需要使用新的命名约定。 4. 如果必须使用Matplotlib的样式,尝试旧的或替代样式名称,如'seaborn-whitegrid',或者使用其他内置样式如'ggplot'。 5. 确保代码中没有拼写错误,并正确应用样式。 需要验证这些步骤是否正确,并考虑用户可能遇到的常见错误,比如版本不匹配或拼写错误。同时,参考引用中的信息,确保建议的方法符合最新的库更新。</think>### 逐步解答 1. **检查Matplotlib和Seaborn版本** 首先确保你的Matplotlib版本≥3.6Seaborn版本≥0.12.0。旧版本可能不支持新的样式命名规则。 升级命令: ```bash pip install --upgrade matplotlib seaborn ``` 2. **使用Seaborn原生API设置样式** Seaborn已将其样式控制与Matplotlib解耦,**推荐直接通过Seaborn设置**: ```python import seaborn as sns sns.set_style("whitegrid") # 直接调用Seaborn的样式设置 ``` 3. **Matplotlib中手动调用新格式样式(备选方案)** 如果仍需通过Matplotlib使用,需使用**完整的样式名称**: ```python import matplotlib.pyplot as plt plt.style.use('seaborn-v0_8-whitegrid') # 注意完整前缀 ``` 4. **验证可用样式列表** 若仍报错,检查当前Matplotlib支持的样式: ```python print(plt.style.available) ``` 若列表中无`seaborn-v0_8-*`样式,则可能是版本不兼容或安装问题。 5. **兼容性替代方案** - **使用Seaborn默认样式**(无需显式指定版本): ```python sns.set(style="whitegrid", font_scale=1.2) ``` - **手动配置类似效果**: ```python plt.style.use({ "axes.facecolor": "white", "axes.grid": True, "grid.color": ".9", "axes.edgecolor": "black" }) ``` ### 关键原理 Matplotlib 3.6+将Seaborn样式标记为过时,但保留为`seaborn-v0_8-<style>`格式以区分新旧版本[^1]。Seaborn自身通过`sns.set_style()`提供更稳定的样式控制[^4]。 ### 示例代码 ```python # 方法1:通过Seaborn设置(推荐) import seaborn as sns sns.set_style("whitegrid") # 方法2:通过Matplotlib调用(需确认版本) import matplotlib.pyplot as plt plt.style.use('seaborn-v0_8-whitegrid') # 若存在该样式 # 绘图测试 plt.plot([1,2,3], [2,4,1]) plt.show() ``` ### 常见问题排查 - **错误提示`Style 'seaborn-v0_8-whitegrid' not found`** 检查Matplotlib版本是否≥3.6Seaborn≥0.12.0。若版本正确但问题仍存在,尝试重启Python内核。 - **样式未生效** 确保`sns.set_style()`在绘图代码**之前**调用。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值