seaborn从入门到精通01-seaborn介绍与load_dataset(“tips“)出现超时解决方案

简介: seaborn从入门到精通01-seaborn介绍与load_dataset(“tips“)出现超时解决方案

1a9bed745ac14360a656b89ca93191cf.jpg


seaborn介绍


官方介绍

Seaborn is a library for making statistical graphics in Python. It builds on top of matplotlib and integrates closely with pandas data structures.

Seaborn是一个用Python制作统计图形的库。它构建在matplotlib之上,并与pandas数据结构紧密集成。


Seaborn helps you explore and understand your data. Its plotting functions operate on dataframes and arrays containing whole datasets and internally perform the necessary semantic mapping and statistical aggregation to produce informative plots. Its dataset-oriented, declarative API lets you focus on what the different elements of your plots mean, rather than on the details of how to draw them.

Seaborn帮助您探索和理解您的数据。它的绘图功能对包含整个数据集的数据框架和数组进行操作,并在内部执行必要的语义映射和统计聚合以生成信息丰富的绘图。它的面向数据集的声明性API让您可以专注于图表的不同元素的含义,而不是如何绘制它们的细节。


seaborn入门流程

# Import seaborn
import seaborn as sns
# Apply the default theme
sns.set_theme()
# Load an example dataset 需要
# tips = sns.load_dataset("tips")
tips = sns.load_dataset("tips",cache=True,data_home=r'.\seaborn-data')
# Create a visualization
sns.relplot(
    data=tips,
    x="total_bill", y="tip", col="time",
    hue="smoker", style="smoker", size="size",
)

如果加载数据时出现问题,可以参考博客

seaborn从入门到精通-seaborn在load_dataset(“tips“)出现超时的错误


7c640d6f8cc3fd726bee9b61558570f2_9046aa7f5cd14c6ca83d5b4649dccbd5.png


# Import seaborn
import seaborn as sns

Seaborn is the only library we need to import for this simple example. By convention, it is imported with the shorthand sns.

对于这个简单的示例,我们需要导入的库只有Seaborn。按照惯例,它与简写sns一起导入。

Behind the scenes, seaborn uses matplotlib to draw its plots. For interactive work, it’s recommended to use a Jupyter/IPython interface in matplotlib mode, or else you’ll have to call matplotlib.pyplot.show() when you want to see the plot.

在幕后,seaborn使用matplotlib绘制它的情节。对于交互式工作,建议在matplotlib模式下使用Jupyter/IPython接口,否则当您想要查看绘图时,必须调用matplotlib.pyplot.show()。


# Apply the default theme
sns.set_theme()

This uses the matplotlib rcParam system and will affect how all matplotlib plots look, even if you don’t make them with seaborn. Beyond the default theme, there are several other options, and you can independently control the style and scaling of the plot to quickly translate your work between presentation contexts (e.g., making a version of your figure that will have readable fonts when projected during a talk). If you like the matplotlib defaults or prefer a different theme, you can skip this step and still use the seaborn plotting functions.

这将使用matplotlib rcParam系统,并将影响所有matplotlib图的外观,即使您没有使用seaborn创建它们。除了默认主题之外,还有其他几个选项,您可以独立控制图形的样式和缩放,以便在不同的演示上下文之间快速转换您的工作(例如,制作一个在演讲期间投影时具有可读字体的图形版本)。如果您喜欢matplotlib默认值或喜欢不同的主题,您可以跳过此步骤,仍然使用seaborn绘图函数。


# Load an example dataset
#tips = sns.load_dataset("tips")
tips = sns.load_dataset("tips",cache=True,data_home=r'.\seaborn-data')

Most code in the docs will use the load_dataset() function to get quick access to an example dataset. There’s nothing special about these datasets: they are just pandas dataframes, and we could have loaded them with pandas.read_csv() or built them by hand. Most of the examples in the documentation will specify data using pandas dataframes, but seaborn is very flexible about the data structures that it accepts.

文档中的大多数代码将使用load_dataset()函数来快速访问示例数据集。这些数据集没有什么特别之处:它们只是pandas数据框架,我们可以用pandas.read_csv()加载它们,也可以手工构建它们。文档中的大多数示例都将使用pandas数据框架指定数据,但是seaborn对于它所接受的数据结构非常灵活。


# Create a visualization
sns.relplot(
    data=tips,
    x="total_bill", y="tip", col="time",
    hue="smoker", style="smoker", size="size",
)

This plot shows the relationship between five variables in the tips dataset using a single call to the seaborn function relplot().

这个图通过对seaborn函数relplot()的一次调用显示了tips数据集中五个变量之间的关系。

Notice how we provided only the names of the variables and their roles in the plot. Unlike when using matplotlib directly, it wasn’t necessary to specify attributes of the plot elements in terms of the color values or marker codes.

请注意,我们如何仅提供变量的名称及其在图中的角色。与直接使用matplotlib不同,不需要根据颜色值或标记代码指定绘图元素的属性。

Behind the scenes, seaborn handled the translation from values in the dataframe to arguments that matplotlib understands. This declarative approach lets you stay focused on the questions that you want to answer, rather than on the details of how to control matplotlib.

在幕后,seaborn处理从数据框架中的值到matplotlib能够理解的参数的转换。这种声明性方法使您能够将注意力集中在想要回答的问题上,而不是集中在如何控制matplotlib的细节上。


参考


seaborn官方

seaborn官方介绍

seaborn可视化入门

【宝藏级】全网最全的Seaborn详细教程-数据分析必备手册(2万字总结)

Seaborn常见绘图总结


总结

本文主要是seaborn从入门到精通系列第1篇,本文介绍了seaborn的官方简介,同时介绍了较好的参考文档置于博客前面,读者可以重点查看参考链接。本系列的目的是可以完整的完成seaborn从入门到精通。重点参考连接

相关文章
|
6月前
|
SQL 关系型数据库 数据库
Python SQLAlchemy模块:从入门到实战的数据库操作指南
免费提供Python+PyCharm编程环境,结合SQLAlchemy ORM框架详解数据库开发。涵盖连接配置、模型定义、CRUD操作、事务控制及Alembic迁移工具,以电商订单系统为例,深入讲解高并发场景下的性能优化与最佳实践,助你高效构建数据驱动应用。
831 7
|
9月前
|
Linux 计算机视觉 C++
【解决方案】Building wheel for opencv-python:安装卡顿的原因与解决方案
当你安装OpenCV时,命令行停在Building wheel for opencv-python (PEP 517) ... -似乎卡住了。这并非程序假死,而是其编译耗时巨大。本文将揭示原因,并提供优化安装体验的实用方法。
1138 88
|
7月前
|
测试技术 开发者 Python
Python单元测试入门:3个核心断言方法,帮你快速定位代码bug
本文介绍Python单元测试基础,详解`unittest`框架中的三大核心断言方法:`assertEqual`验证值相等,`assertTrue`和`assertFalse`判断条件真假。通过实例演示其用法,帮助开发者自动化检测代码逻辑,提升测试效率与可靠性。
546 1
|
8月前
|
API 数据安全/隐私保护 开发者
Python自定义异常:从入门到实践的轻松指南
在Python开发中,自定义异常能提升错误处理的精准度与代码可维护性。本文通过银行系统、电商库存等实例,详解如何创建和使用自定义异常,涵盖异常基础、进阶技巧、最佳实践与真实场景应用,助你写出更专业、易调试的代码。
320 0
|
8月前
|
IDE 开发工具 数据安全/隐私保护
Python循环嵌套:从入门到实战的完整指南
循环嵌套是Python中处理多维数据和复杂逻辑的重要工具。本文通过实例讲解嵌套循环的基本用法、常见组合、性能优化技巧及实战应用,帮助开发者掌握其核心思想,避免常见错误,并探索替代方案与进阶方向。
608 0
|
6月前
|
Cloud Native 算法 API
Python API接口实战指南:从入门到精通
🌟蒋星熠Jaxonic,技术宇宙的星际旅人。深耕API开发,以Python为舟,探索RESTful、GraphQL等接口奥秘。擅长requests、aiohttp实战,专注性能优化与架构设计,用代码连接万物,谱写极客诗篇。
1405 1
Python API接口实战指南:从入门到精通
|
7月前
|
异构计算 Python
ERROR: pip’s dependency resolver does not currently take into 报错-Python项目依赖冲突的解决方案-优雅草优雅草卓伊凡
ERROR: pip’s dependency resolver does not currently take into 报错-Python项目依赖冲突的解决方案-优雅草优雅草卓伊凡
582 1
|
6月前
|
存储 Java 调度
Python定时任务实战:APScheduler从入门到精通
APScheduler是Python强大的定时任务框架,通过触发器、执行器、任务存储和调度器四大组件,灵活实现各类周期性任务。支持内存、数据库、Redis等持久化存储,适用于Web集成、数据抓取、邮件发送等场景,解决传统sleep循环的诸多缺陷,助力构建稳定可靠的自动化系统。(238字)
1217 1
|
10月前
|
Python
Python字符串格式化利器:f-strings入门指南
Python字符串格式化利器:f-strings入门指南
560 80

推荐镜像

更多