Insightful and aesthetic visualizations don’t have to be a pain to create. This article will prevent 5+ simple one-liners you can add to your code to increase its style and informational value.

富有洞察力和美学的可视化不必费心创建。 本文将防止您添加到代码中以增加其样式和信息价值的5种以上简单的单行代码。

将线图绘制成面积图 (Line plot into area chart)

Consider the following standard line plot, created with seaborn’s lineplot, with the husl palette and whitegrid style. The data is generated as a sine wave with normally distributed data and elevated above the x-axis.

考虑下面的标准线图,该线图是用seaborn的lineplot创建的,具有husl调色板和whitegrid样式。 数据以正弦波的形式生成,具有正态分布的数据,并高于x轴。

With a few styling choices, the plot looks presentable. However, there is one issue: by default, Seaborn does not begin at a zero baseline, and the numerical impact of the y-axis is lost. Assuming that the x and y variables are named as such, adding plt.fill_between(x,y,alpha=0.4) will turn the data into an area chart that more nicely begins at the base line and emphasizes the y-axis.

通过一些样式选择,该图看起来很合适。 但是,存在一个问题:默认情况下,Seaborn并不是从零基线开始的,并且y轴的数值影响会丢失。 假设xy变量是这样命名的,则添加plt.fill_between(x,y,alpha=0.4)会将数据转换为面积图,该图从基线开始会更好,并强调y轴。

Note that this line is added in conjunction with the original lineplot, sns.lineplot(x,y), which provides the bolded line at the top. The alpha parameter, which appears in many seaborn plots as well, controls the transparency of the area (the less, the lighter). plt represents the matplotlib library. In some cases, using area may not be suitable.

请注意,这条线是与原始线图sns.lineplot(x,y) ,它在顶部提供了粗体线。 alpha参数(也出现在许多海洋图中)控制着区域的透明度(越少越亮)。 plt代表matplotlib库。 在某些情况下,使用区域可能不合适。

When multiple area plots are used, it can emphasize overlapping and intersections of the lines, although, again, it may not be appropriate for the visualization context.

当使用多个区域图时,它可以强调线的重叠和相交,尽管同样,它可能不适用于可视化上下文。

线图到堆叠区域图 (Line plot to stacked area plot)

Sometimes, the relationship between lines requires that the area plots be stacked on top of each other. This is easy to do with matplotlib stackplot: plt.stackplot(x,y,alpha=0.4). In this case, colors were manually specified through colors=[], which takes in a list of color names or hex codes.

有时,线之间的关系要求面积图彼此堆叠。 使用matplotlib stackplot很容易做到: plt.stackplot(x,y,alpha=0.4) 。 在这种情况下,颜色是通过colors=[]手动指定的,它接受颜色名称或十六进制代码的列表。

Note that y is a list of y1 and y2, which represent the noisy sine and cosine waves. These are stacked on top of each other in the area representation, and can heighten understanding of the relative distance between two area plots.

请注意, yy1y2的列表,它们代表有噪声的正弦波和余弦波。 它们在区域表示中彼此堆叠,可以加深对两个区域图之间相对距离的理解。

删除讨厌的传说 (Remove pesky legends)

Seaborn often uses legends by default when the hue parameter is called to draw multiple of the same plot, differing by the column specified as the hue. These legends, while sometimes helpful, often cover up important parts of the plot and contain information that could be better expressed elsewhere (perhaps in a caption).

默认情况下,在调用hue参数绘制同一图的倍数时,Seaborn通常默认使用图例,不同之处在于指定为hue的列。 这些图例虽然有时会有所帮助,但通常会掩盖剧情中的重要部分,并包含可以在其他地方更好地表达的信息(也许在标题中)。

For example, consider the following medical dataset, which contains signals from various subjects. In this case, we want to use multiple line plots to visualize the general trend and range across different patients by setting the subject column as the hue (yes, putting this many lines is known as a ‘spaghetti chart’ and is generally not advised). One can see how the default labels are a) not ordered, b) so long that it obstructs part of the chart, and c) not the point of the visualization.

例如,考虑以下医疗数据集,其中包含来自各个受试者的信号。 在这种情况下,我们希望使用多个折线图,通过将subject列设置为hue来可视化不同患者的总体趋势和范围(是的,放置这么多折线被称为“意大利面条图”,通常不建议这样做) 。 可以看到默认标签是如何排列的:a)没有排序,b)太长以致于它阻碍了图表的一部分,并且c)没有可视化的要点。

This can be done by setting the plot equal to a variable (commonly g), like such: g=sns.lineplot(x=…, y=…, hue=…). Then, by accessing the plot object’s legend attributes, we can remove it: g.legend_.remove(). If you are working with a grid object like PairGrid or FacetGrid, use g._legend.remove().

这可以通过将绘图设置为等于变量(通常为g )来完成,例如: g=sns.lineplot(x=…, y=…, hue=…) 。 然后,通过访问绘图对象的图例属性,可以将其删除: g.legend_.remove() 。 如果您正在使用诸如PairGrid或FacetGrid之类的网格对象,请使用g._legend.remove()

Much better.
好多了。

手动X和Y轴基线 (Manual x and y axis baselines)

Seaborn does not draw the x and y axis lines by default, but the axes are important for understanding not only the shape of the data but where they stand in relation to the coordinate system.

Seaborn默认情况下不会绘制xy轴线,但是这些轴不仅对于理解数据的形状而且对于理解其相对于坐标系的位置非常重要。

Matplotlib provides a simple way to add the x-axis by simply adding g.axhline(0), where g is the grid object and 0 represents the y-axis value at which the horizontal line is placed. Additionally, one can specify color (in this case color=’black’) and alpha (transparency, in this case alpha=0.5). linestyle is a parameter used to create dotted lines by being set to ‘--’.

Matplotlib提供了一种简单的方法,只需添加g.axhline(0)即可添加x轴,其中g是网格对象,0表示放置水平线的y轴值。 另外,可以指定color (在这种情况下为color='black' )和alpha (透明度,在这种情况下为alpha=0.5 )。 linestyle是用于通过将其设置为'--'来创建虚线的参数。

Additionally, vertical lines can be added through g.axvline(0).

另外,可以通过g.axvline(0)添加垂直线。

You can also use axhline to display averages or benchmarks for, say, bar plots. For example, say that we want to show the plants that were able to meet the 0.98 petal_width benchmark based on sepal_width.

您还可以使用axhline显示axhline平均值或基准。 例如,假设我们要显示能够满足基于sepal_width petal_width基准的sepal_width

对数刻度 (Logarithmic Scales)

Logarithmic scales are used because they can show a percent change. In many scenarios, this is exactly what is necessary — after all, an increase of $1000 for a business that normally earns $300 is not the same as an increase of $1000 for a megacorporation that earns billions. Instead of needing to calculate percentages in the data, matplotlib can convert scales to logarithmic.

使用对数刻度,因为它们可以显示百分比变化。 在许多情况下,这正是必要的条件—毕竟,通常赚取300美元的企业增加1000美元,与赚取数十亿美元的大型企业增加1000美元并不相同。 matplotlib无需计算数据中的百分比,而是可以将比例转换为对数。

As with many matplotlib features, logarithmic scales operate on the ax of a standard figure created with fig, ax = plt.subplots(figsize=(x,y)). Then, a logarithmic x-scale is as simple as ax.set_xscale(‘log’):

与许多matplotlib功能一样,对数刻度在用fig, ax = plt.subplots(figsize=(x,y))创建的标准图形的fig, ax = plt.subplots(figsize=(x,y)) 。 然后,对数x ax.set_xscale('log')ax.set_xscale('log')一样简单:

A sine wave. Note that matplotlib creates exponential-notation x-labels for you!
正弦波。 请注意,matplotlib为您创建指数符号x标签!

A logarithmic y-scale, which is more commonly used, can be done with ax.setyscale(‘log’):

可以使用ax.setyscale('log')完成更常用的对数y ax.setyscale('log')

y-logarithmic scale for a sine wave with noise, showing the percent change from the previous time step.
带有噪声的正弦波的y对数标度,显示与上一个时间步长相比的变化百分比。

荣誉奖 (Honorable mentions)

  • Invest in a good default palette. Color is one of the most important aspects of a visualization: it ties it together and expressed a theme. You can choose and set one of Seaborn’s many great palettes with sns.set_palette(name). Check out demonstrations and tips to choosing palettes here.

    投资一个好的默认调色板。 颜色是可视化的最重要方面之一:颜色将其绑在一起并表达了主题。 您可以使用sns.set_palette(name)选择并设置Seaborn的众多出色调色板sns.set_palette(name) 。 在此处查看演示和选择调色板的提示。

  • You can add grids and change the background color with sns.set_style(name), where name can be white (default), whitegrid, dark, or darkgrid.

    您可以使用sns.set_style(name)添加网格并更改背景颜色,其中name可以是white (默认), whitegriddarkdarkgrid

  • Did you know that matplotlib and seaborn can process LaTeX, the beautiful mathematical formatting language? You can use it in your x/y axis labels, titles, legends, and more by enclosing LaTeX expressions within dollar signs $expression$.

    您是否知道matplotlib和seaborn可以处理LaTeX(一种漂亮的数学格式化语言) ? 通过将LaTeX表达式包含在美元符号$expression$ ,可以在x / y轴标签,标题,图例等中使用它。

  • Explore different linestyles, annotation sizes, and fonts. Matplotlib is full of them, if only you have the will to explore its documentation pages.探索不同的线型,注释大小和字体。 Matplotlib充满了它们,只要您愿意探索它的文档页面。
  • Most plots have additional parameters, such as error bars for bar plots, thickness, dotted lines, and transparency for line plots. Taking some time to visit the documentation pages and peering through all the available parameters can take only a minute but has the potential to bring your visualization to top-notch aesthetic and informational value.

    大多数图都有其他参数,例如条形图的误差线,厚度,虚线和线图的透明度。 花一些时间访问文档页面并浏览所有可用参数仅需一分钟,但有可能使您的可视化达到一流的美学和信息价值。

    For example, adding the parameter

    例如,添加参数

    inner=’quartile’ in a violinplot draws the first, second, and third quartiles of a distribution in dotted lines. Two words for immense informational gain — I’d say that’s a good deal!

    小提琴图中的inner='quartile' quartile inner='quartile'用虚线绘制分布的第一,第二和第三四分位数。 两个词可带来巨大的信息收益-我说这很划算!

附加阅读 (Additional Reading)

All charts created by author.

所有图表由作者创建。

翻译自: https://towardsdatascience.com/5-simple-one-liners-youve-been-looking-for-to-level-up-your-python-visualization-42ebc1deafbc


http://www.taodudu.cc/news/show-995029.html

相关文章:

  • 产品观念:更好的捕鼠器_故事很重要:为什么您需要成为更好的讲故事的人
  • 面向Tableau开发人员的Python简要介绍(第2部分)
  • netflix_Netflix的计算因果推论
  • 高斯金字塔 拉普拉斯金字塔_金字塔学入门指南
  • 语言认知偏差_我们的认知偏差正在破坏患者的结果数据
  • python中定义数据结构_Python中的数据结构。
  • plotly django_使用Plotly为Django HTML页面进行漂亮的可视化
  • 软件工程方法学要素含义_日期时间数据的要素工程
  • 数据湖 data lake_在Data Lake中高效更新TB级数据的模式
  • ai对话机器人实现方案_显然地引入了AI —无代码机器学习解决方案
  • 图片中的暖色或冷色滤色片是否会带来更多点击? —机器学习A / B测试
  • 图卷积 节点分类_在节点分类任务上训练图卷积网络
  • 回归分析预测_使用回归分析预测心脏病。
  • aws spark_使用Spark构建AWS数据湖时的一些问题以及如何处理这些问题
  • 数据科学家编程能力需要多好_我们不需要这么多的数据科学家
  • sql优化技巧_使用这些查询优化技巧成为SQL向导
  • 物种分布模型_减少物种分布建模中的空间自相关
  • 清洁数据ploy n_清洁屋数据
  • 基于边缘计算的实时绩效_基于绩效的营销中的三大错误
  • 上凸包和下凸包_使用凸包聚类
  • 决策树有框架吗_决策框架
  • mysql那本书适合初学者_3本书适合初学者
  • 阎焱多少身价_2020年,数据科学家的身价是多少?
  • 卡尔曼滤波滤波方程_了解卡尔曼滤波器及其方程
  • 朴素贝叶斯分类器 文本分类_构建灾难响应的文本分类器
  • Seaborn:Python
  • 销货清单数据_2020年8月数据科学阅读清单
  • 米其林餐厅 盐之花_在世界范围内探索《米其林指南》
  • spotify 数据分析_我的Spotify流历史分析
  • 纹个鸡儿天才小熊猫_给熊猫用户的5个提示

您一直在寻找5+个简单的一线工具来提升Python可视化效果相关推荐

  1. 3秒搜电影,基于Pyqt5的简单电影搜索工具,会Python真牛逼!

    今天给喜欢看电影的同学推荐一款 Python 制作的搜索电影片源的工具. 经常在网上搜索片源的同学应该知道,各大片源网站广告不断,有些网站,点一下查询就弹出广告,关掉广告后再点查询,片源信息才显示正常 ...

  2. Nodejs开发简单的脚手架工具

    脚手架,这个名词对于作为前端的我们来说,也许并不陌生吧,像vue-cli,react-native-cli等,全局安装后,只需要在命令行中敲入一个简单的命令,便可帮我们快速的生成一个初始项目,如vue ...

  3. 用几个最简单的例子带你入门 Python 爬虫

    作者 | ZackSock 来源 | 新建文件夹X(ID:ZackSock) 头图 | CSDN下载自视觉中国 前言 爬虫一直是Python的一大应用场景,差不多每门语言都可以写爬虫,但是程序员们却独 ...

  4. 图解爬虫,用几个最简单的例子带你入门Python爬虫

    一.前言 爬虫一直是Python的一大应用场景,差不多每门语言都可以写爬虫,但是程序员们却独爱Python.之所以偏爱Python就是因为她简洁的语法,我们使用Python可以很简单的写出一个爬虫程序 ...

  5. 实用c语言函数源码,C语言编写简单朗读小工具(有源码)

    原标题:C语言编写简单朗读小工具(有源码) 最近不少人在后台留言说学C都是面对枯燥的控制台程序,能不能体现一下C语言的实际用途,今天我们就理论结合实践一把:C语言结合VBS脚本编写一个简单的朗读小工具 ...

  6. 使用 Node.js 开发简单的脚手架工具

    前言 像我们熟悉的 vue-cli,react-native-cli 等脚手架,只需要输入简单的命令 vue init webpack project,即可快速帮我们生成一个初始项目.在实际工作中,我 ...

  7. 用python画太阳花-python 简单的绘图工具turtle使用详解

    目录 1. 画布(canvas) 1.1 设置画布大小 2. 画笔 2.1 画笔的状态 2.2 画笔的属性 2.3 绘图命令 3. 命令详解 4. 绘图举例 4.1 太阳花 4.2 绘制小蟒蛇 4.3 ...

  8. 数据可视化----我在寻找一款类似vfp或是access这样自带可视化风格的数据库或是键盘数据库...

    我在寻找一款类似vfp或是access这样自带可视化风格的数据库或是键盘数据库 影响redis,mongodb今后发展的我也认为是一些可视化工具的支持 http://blog.xiqiao.info/ ...

  9. html 浮动窗口置顶,jQuery简单实现页面元素置顶时悬浮效果示例

    本文实例讲述了jQuery简单实现页面元素置顶时悬浮效果的方法.分享给大家供大家参考,具体如下: 一.JS Code: $.fn.smartFloat = function () { var posi ...

最新文章

  1. 5G NGC — NEF PFD Management Service
  2. Mac OS 如何连接windows 文件共享
  3. 洛谷【算法1-4】递推与递归
  4. iOS中的应用启动原理
  5. 入行十年,总结出了数据仓库、数据集市、数据库的精华,你一定不能错过
  6. Excellent Service
  7. GCP发布Kaniko:在非特权容器和Kubernetes中构建容器镜像的工具
  8. 简单的动态网站java,Java实践:一个简单的动态数组实现
  9. python多线程图像识别_Python 多线程抓取图片效率对比
  10. 2x麦克劳林公式_极限求解-泰勒公式理解
  11. Spring字段注入
  12. py4j.java gateway_python 2.7-为什么PySpark无法找到py4j.java_gateway?
  13. 在TCP三次握手后插入伪造的TCP包
  14. LeetCode之猜数字大小
  15. springsecurity实现记住我的功能,将用户的登录信息保存到本地浏览器,即使关闭浏览器也不用登录
  16. C-Free注册码,密钥,到期解决办法
  17. ubuntu16.04 安装为知笔记
  18. Java面试宝典(2018版)
  19. 绕过CDN查询真实IP
  20. springmvc500错误

热门文章

  1. 软件工程---1.概述
  2. 每日一题:leetcode456.132模式
  3. SPOJ - QTREE3Query on a tree again!——树链剖分
  4. 比较ArrayList和数组的区别
  5. mysql-视图、事物等
  6. pdf.js插件使用记录,在线打开pdf
  7. 数据结构 | 链表:1097 删除重复元素
  8. Leetcode: Counting Bits
  9. spring集合的注入
  10. 【iOS开发每日小笔记(一)】UIPickerView 自动选择某个component的某个row