熊猫在线压缩图

数据可视化 (Data Visualization)

I like the plotting facilities that come with Pandas. Yes, there are many other plotting libraries such as Seaborn, Bokeh and Plotly but for most purposes, I am very happy with the simplicity of Pandas plotting.

我喜欢熊猫随附的绘图设备。 是的,还有许多其他的绘图库,例如Seaborn,Bokeh和Plotly,但对于大多数用途,我对Pandas绘图的简单性感到非常满意。

But there is one thing missing that I would like and that is the ability to plot a regression line over a complex line or scatter plot.

但是我想缺少一件事,那就是能够在复杂线或散点图上绘制回归线。

But, as I have discovered, this is very easily solved. With the Numpy library you can generate regression data in a couple of lines of code and plot it in the same figure as your original line or scatter plot.

但是,正如我发现的那样,这很容易解决。 使用Numpy库,您可以在几行代码中生成回归数据,并将其绘制在与原始线图或散点图相同的图中。

So that is what we are going to do in this article.

这就是我们在本文中要做的。

First, let’s get some data. If you’ve read any of my previous articles on data visualization, you know what’s coming next. I’m going to use a set of weather data that you can download from my Github account. It records the temperatures, sunshine levels and rainfall over several decades for London in the UK and is stored as a CSV file. This file has been created from public domain data recorded by the UK Met Office.

首先,让我们获取一些数据。 如果您阅读过我以前有关数据可视化的任何文章,那么您将了解接下来的内容。 我将使用一组可以从我的Github帐户下载的天气数据。 它记录了英国伦敦数十年来的温度,日照水平和降雨量,并以CSV文件存储。 该文件是根据UK Met Office记录的公共领域数据创建的。

伦敦夏天变热吗 (Are London summers getting hotter)

We are going to check whether the temperatures in London are rising over time. It’s not obvious from the raw data but by plotting a regression line over that data we will be better able to see the trend.

我们将检查伦敦的温度是否随着时间升高。 从原始数据来看并不明显,但是通过在该数据上绘制一条回归线,我们将能够更好地看到趋势。

So to begin we need to import the libraries that we will need.

因此,我们首先需要导入所需的库。

import pandas as pdimport numpy as npimport matplotlib.pyplot as plt

Nothing very unusual there, we are importing Pandas to help with data analysis and visualization, Numpy will give us the routines we need to create the regression data and Matplotlib is used by Pandas to create the plots.

那里没有什么异常的,我们正在导入Pandas以帮助进行数据分析和可视化,Numpy将为我们提供创建回归数据所需的例程,而Matplotlib被Pandas用于创建图。

Next, we download the data.

接下来,我们下载数据。

weather = pd.read_csv(‘https://raw.githubusercontent.com/alanjones2/dataviz/master/londonweather.csv')

(As you probably guessed, that’s all supposed to be on one line.)

(您可能已经猜到了,这些都应该放在一行上。)

We have read the CSV file into a Pandas DataFrame and this is what it looks like — a table containing monthly data that records the maximum and minimum temperatures, the rainfall and the number of hours of sunshine, starting in 1957 and ending part way through 2019.

我们已经将CSV文件读入了Pandas DataFrame,它的样子是这样的-该表包含每月数据,记录最高和最低温度,降雨量和日照小时数,始于1957年,直到2019年结束。

I posed the question about whether summers were getting hotter, so I’m going to filter the data to give me only the data for the month of July when the hottest temperatures are normally recorded. And, for convenience, I’m going to add a column that numbers the years starting at year 0 (you’ll see how this is used later).

我提出了一个关于夏天是否变热的问题,所以我将过滤数据以仅提供通常记录最热温度的7月的数据。 并且,为方便起见,我将添加一列以数字表示从0年开始的年份(您将在稍后看到如何使用它)。

july = weather.query(‘Month == 7’)july.insert(0,’Yr’,range(0,len(july)))

The code above applies a query to the weather dataframe which returns only the rows where the Month is equal to 7 (i.e.July) and creates a new dataframe called july from the result.

上面的代码对天气数据框应用查询,该查询仅返回Month等于7(即7月)的行,并从结果中创建一个称为july的新数据框。

Next, we insert a new column called Yr which numbers the rows from 0 to the length of the table.

接下来,我们插入一个称为Yr的新列,该列对从0到表的长度的行进行编号。

july looks like this:

七月看起来像这样:

Now we can plot the maximum temperatures for July since 1957.

现在,我们可以绘制1957年以来7月份的最高温度。

july.plot(y=’Tmax’,x=’Yr’)

There is a lot of variation there and high temperatures are not limited to recent years. But there does seem to be a trend, temperatures do seem to be rising a little, over time.

那里有很多变化,高温不仅限于近年来。 但似乎确实存在趋势,随着时间的流逝,温度似乎确实有所上升。

We can try and make this a bit more obvious by doing a linear regression where we attempt to find a straight line graph that represents the trend in the rise in temperature. To do this we use the polyfit function from Numpy. Polyfit does a least squares polynomial fit over the data that it is given. We want a linear regression over the data in columns Yr and Tmax so we pass these as parameters. The final parameter is the degree of the polynomial. For linear regression the degree is 1.

我们可以通过进行线性回归来尝试使这一点更加明显,在线性回归中我们试图找到一个代表温度上升趋势的直线图。 为此,我们使用Numpy中的polyfit函数。 Polyfit对给出的数据进行最小二乘多项式拟合。 我们希望对YrTmax列中的数据进行线性回归,因此我们将它们作为参数传递。 最终参数是多项式的次数。 对于线性回归,度为1。

We then use the convenience function poly1d to provide us with a function that will do the fitting.

然后,我们使用便利函数poly1d为我们提供将进行拟合的函数。

d = np.polyfit(july[‘Yr’],july[‘Tmax’],1)f = np.poly1d(d)

We now use the function f to produce our linear regression data and inserting that into a new column called Treg.

现在,我们使用函数f生成线性回归数据,并将其插入到名为Treg的新列中。

july.insert(6,’Treg’,f(july[‘Yr’]))

Next, we create a line plot of Yr against Tmax (the wiggly plot we saw above) and another of Yr against Treg which will be our straight line regression plot. We combine the two plot by assigning the first plot to the variable ax and then passing that to the second plot as an additional axis.

接下来,我们创建一个YrTmax的折线图(我们在上面看到的摆动曲线),以及另一个YrTreg的折线图,这将是我们的直线回归图。 我们通过将第一个图分配给变量ax ,然后将其作为附加轴传递给第二个图,来组合这两个图。

ax = july.plot(x = ‘Yr’,y=’Tmax’)july.plot(x=’Yr’, y=’Treg’,color=’Red’,ax=ax)

That’s it, done!

就这样,完成了!

We can now see much more clearly the upward trend of temperature over the years.

现在,我们可以更清楚地看到多年来温度的上升趋势。

And here is the same thing done with a scatter chart.

这就是散点图所做的相同的事情。

ax=july.plot.scatter(x=’Yr’, y=’Tmax’)july.plot(x=’Yr’,y=’Treg’,color=’Red’,legend=False,ax=ax)

That was fairly straightforward, I think, and I hope you found it useful.

我认为那非常简单,希望您发现它有用。

For an introduction to plotting with Pandas see this:

有关使用Pandas进行绘图的介绍,请参见:

翻译自: https://towardsdatascience.com/regression-plots-with-pandas-and-numpy-faf2edbfad4f

熊猫在线压缩图


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

相关文章:

  • RecyclerView 之notifyDataSetChanged的暴躁
  • 谁挡道就骂谁,这个暴躁版扫地机器人火了,东北话也可以,网友:想劈了它...
  • 在线暴躁:script /问题
  • 暴躁的一天从Ubuntu搜狗拼音开始
  • 弱者都自大暴躁,强者都低调温柔
  • 脾气暴躁的 Linus 不大可能开喷修改 master
  • 【HBUOJ】暴躁的阿生
  • [摘录] 暴躁程序员的产生
  • 当3A射击游戏遇上Play to Earn,暴躁兔带你了解MetalCore
  • 暴躁兔让Web3与Crypto之路不再迷茫
  • 世界如此美好,我却如此暴躁,这样不好,不好
  • 1.17暴躁学习日
  • 暴躁兔melody避坑记
  • 暴躁蒟蒻在线水题er日记
  • Python-脾气暴躁
  • 暴躁算法(剑指系列)-每日一练
  • 为什么软件工程师的脾气都这么暴躁
  • 暴躁是企业家的性格?
  • 如何让自己不再暴躁易怒
  • 备份文件后缀
  • Navicat还原nb3备份文件
  • Navicat定期备份MySQL数据库,定期清理备份文件
  • 计算机桌面备份在哪里,电脑备份文件在哪里
  • 备份文件泄漏
  • 怎样快速备份电脑文件?
  • 电脑如何备份重要文件?教你两种简单方法
  • sql server导入备份文件
  • Centos备份文件
  • 第二章:minio单机版,使用客户端备份文件
  • 小米(MUUI)备份文件使用MT管理器打开

熊猫在线压缩图_回归图与熊猫和脾气暴躁相关推荐

  1. html 甘特图_甘特图怎么画?甘特图基础教程,小白快速入门简单易懂

    甘特图是什么?可能你是第一次听到,甘特图是通过活动顺序和时间间隔表示某一特定项目其顺序与时间的关系.不同于时间表,或日程规划表,甘特图可以使使用者更直观的知道在某一时间的工作内容和进度. 甘特图常见用 ...

  2. matlab怎么画两个自变量的图_关系图怎么画?一款实用的绘制关系图设计软件

    关系图是指实体-联系图,是用来描述现实世界的概念模型.关系图应用范围很广.比如人物关系图.零件关系图等等.关系图主要由三部分构成:矩形框里写实体名.椭圆符号表示属性.菱形框中标明何种联系.同时还用线条 ...

  3. 密度图+回归线,相关图这样画?seaborn中 joinplot 结合核密度图和回归图(KDE+regplot)

    因为jointplot就是联合绘图,通常边缘上绘制分布图,中间绘制其它的(比如核密度图),所以如何去除边缘的分布图,再叠加一条回归线呢,可以用于替换常规散点图表示相关图的方式,如下图? 代码汇总 im ...

  4. 项目进度计划甘特图_甘特图做项目进度计划的技巧?

    原标题:甘特图做项目进度计划的技巧? 甘特图怎么做项目进度计划?首先我们先了解一下,什么是甘特图. 甘特图(Gantt chart)又称为横道图.条状图(Bar chart),是由提出者亨利·L·甘特 ...

  5. wps表格l制作甘特图_甘特图是什么?-如何用WPS表格做甘特图

    甘特图是什么?-如何用WPS表格做甘特图 甘特图(Gantt Chart)又称横道图,由亨利.甘特于1910年开发的,它通过图示形象地表示特定项目的活动顺序与持续时间.其中横轴表示时间,纵轴表示活动( ...

  6. java类关系图_类图和对象图

    类图的概念 一.概述 类图(Class Diagram)是描述类.接口.协作以及它们之间关系的图,用来显示系统中各个类的静态结构.类图是定义其他图的基础,在类图基础上,可以使用状态图.协作图.组件图和 ...

  7. 主胰管和副胰管解剖图_【图】胰腺手术应用解剖(结构图解) - 外科手术学 - 天山医学院...

    胰腺是位于腹膜后的一个狭长的器官,从右向左横跨第1-2腰椎的前方,长12.5-15cm,宽3-4cm,厚1.5-2.5cm,重60-100g,与周围重要脏器及血管的关系密切.胰尾延伸至脾门处,常位于脾 ...

  8. java盒图_箱形图/盒图(转)

    http://zh.wikipedia.org/wiki/%E7%AE%B1%E5%BD%A2%E5%9C%96 http://www.blogjava.net/norvid/articles/317 ...

  9. sip 时序图_时序图怎么看_教你如何看懂时序图 - 什么是时序图_时序图怎么看_教你如何看懂时序图...

    时序图怎么看_教你如何看懂时序图 操作时序永远使用是任何一片IC芯片的最主要的内容.一个芯片的所有使用细节都会在它的官方器件手册上包含.所以使用一个器件事情,要充分做好的第一件事就是要把它的器件手册上 ...

  10. android 照片拼接长图_长图拼接app下载 长图拼接制作 for Android v2.6.1 安卓版 下载-脚本之家...

    长图拼接制作app是一款操作很方便的长截图拼接软件.长图拼接app能够支持对截图进行裁剪和编辑,长图拼接制作app还有多种拼接模板和框架供大家选择,有需要的用户赶快下载体验吧! 软件介绍 长图拼接制作 ...

最新文章

  1. 上海电信计划2015年用户带宽提高12.5倍
  2. 互联网金融产品做第三方支付平台托管需要注意什么?
  3. 贺TDSQL喜提286万QPS!本文回顾了它的十年锻造之路
  4. 最全三大框架整合(使用映射)——Dept.hbm.xml
  5. Nginx的平滑升级记录---适用于编译安装的Nginx
  6. JavaWeb项目实战(1)数据库环境搭载
  7. C语言:用条件运算符的嵌套完成此题。学习成绩=90分的学生用A表示,70-89的学生用B表示,60-79的学生用表示,低于60分的学生用D表示
  8. Java多线程-while死循环
  9. java put请求_计算机毕业设计中用java实现小程序推送(springboot实现)
  10. linux fdisk ntfs,2014.1.2 学习记录(fdisk、ntfs)
  11. MODIS,Himwari-8遥感数据介绍
  12. CAD批量输入坐标生成红线
  13. python桌面程序自动化教程_桌面应用自动化python
  14. chrome浏览器清理缓存也没有用,每次必须重启怎么办?
  15. 苹果html向上滑动不流畅,苹果手机Safari浏览器下滑动卡顿的问题
  16. HTTP接口测试代码,HTTP GET/POST模拟请求测试工具
  17. 批量下载文件,打包成zip压缩包
  18. 高空核爆与雷电电磁脉冲特征及能量吸收技术(摘要)
  19. 拉取 trace.txt 进行 anr 分析
  20. Linux安装mysql(mysql-5.7.23-1.el7.x86_64.rpm-bundle.tar)(万能解决登录问题,最详细教程)

热门文章

  1. flappy bird c语言,Flappy Bird C语言实现
  2. InfoPath 2007/2010 Helper Tool
  3. 股权投资模型-CAPM模型和PEG模型(内附示例数据)
  4. 计算机系统最主要的弱点,计算机安全弱点及其对应关键技术研究
  5. ZenCart商店 OpenzcTPL模版安装教程
  6. 老徐WEB:CSS伪类和伪元素详解
  7. 读v_JULY_v整理笔试题博客有感,整理些答案。
  8. SDH(标准DH)和MDH(改进DH)
  9. Verilog语言要素(三)
  10. JVM 语言的兴衰 【The Rise and Fall of JVM Languages】