自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

一篇文章帶你搞定 Pandas 繪圖 API

開發(fā) 后端
pandas除了數(shù)據(jù)處理還可以進(jìn)行數(shù)據(jù)可視化展示,這里我們簡單說明一下pandas繪制常見圖形的一些API,使用pandas繪圖其實(shí)并不多,這里做一個(gè)簡單展示。

圖片

對(duì)于從網(wǎng)頁上爬取下來的數(shù)據(jù)很多很雜亂,我們需要進(jìn)行數(shù)據(jù)可視化,pandas除了數(shù)據(jù)處理還可以進(jìn)行數(shù)據(jù)可視化展示,這里我們簡單說明一下pandas繪制常見圖形的一些API:由于現(xiàn)在針對(duì)數(shù)據(jù)可視化有很多庫,matplotlib、seaborn、pyecharts等等,使用pandas繪圖其實(shí)并不多,這里做一個(gè)簡單展示。

目錄:

  • 柱狀圖
  • 餅圖
  • 折線圖
  • 散點(diǎn)圖
  • 直方圖

柱狀圖

普通柱圖

首先我們打開excel數(shù)據(jù)文件,如下圖所示:

圖片

Field:專業(yè);Number:對(duì)應(yīng)專業(yè)學(xué)生數(shù)量,根據(jù)兩列數(shù)據(jù)繪制簡單柱圖:

import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_excel('Students.xlsx')
# pd繪圖
data.sort_values(by='Number',inplace=True,ascending=False)
data.plot.bar(x='Field',y='Number',title='National Students Field')

plt.tight_layout()
plt.show()

解釋說明:

  1. Students.xlsx:對(duì)應(yīng)的數(shù)據(jù)文件;
  2. data.sort_values(by='Number',inplace=True,ascending=False):按照Number數(shù)量大小排序,inplace = True:在當(dāng)前數(shù)據(jù)文件上修改,不生成新的數(shù)據(jù)文件,ascending = False:降序排序;
  3. bar() 繪制柱圖的函數(shù),x、y分別指定下,x、y軸的序列;
  4. tight_layout() 使得整個(gè)圖緊湊顯示,不然x軸的文字由于太長會(huì)被擋住;

結(jié)果如下所示:

圖片

分組柱圖

首先我們還是查看數(shù)據(jù)文件:students02.xlsx

圖片

給出了2016、2017兩年的學(xué)生數(shù)量,由此考慮繪制分組柱狀圖:

import  pandas as pd
import matplotlib.pyplot as plt
data = pd.read_excel('Students02.xlsx')
data.sort_values(by='2017',inplace=True,ascending=False)
data.plot.bar(x='Field',y=['2016','2017'],color=['red','orange'])

plt.title('National Students Number',fontsize=16,fontweight='bold')
plt.xlabel('Field',fontweight='bold')
plt.ylabel('Number',fontweight='bold')
# x軸刻度偏轉(zhuǎn)
ax = plt.gca()
ax.set_xticklabels(data['Field'],rotation=45,ha='right')
print(data)
f = plt.gcf()
f.subplots_adjust(left=0.2,bottom=0.4)
# plt.tight_layout()
plt.show()

解釋說明:

  1. bar() 繪制柱圖的函數(shù),x、y分別指定下,x、y軸的序列,但是由于分組柱圖y軸不再是一個(gè)序列,而是由兩個(gè)(或多個(gè))序列組成的列表;
  2. plt.title() 設(shè)置標(biāo)題,當(dāng)然也可以在bar() 函數(shù)里面設(shè)置;
  3. plt.gca() 獲得x軸的文字,下一列重新設(shè)置x軸的文字,并且把文字旋轉(zhuǎn)45°,ha='right':依照右點(diǎn)為中心進(jìn)行水平對(duì)齊;
  4. plt.gcf() 拿到繪制的圖形對(duì)象,設(shè)置留白區(qū)域,left=0.2(左側(cè)留白20%),bottom=0.4,底部留白40%;

結(jié)果如下:

圖片

疊加柱圖

有的時(shí)候可能不只有兩組數(shù)據(jù),要觀察多組數(shù)據(jù)的數(shù)量占比,可以采用疊加柱圖:

import matplotlib.pyplot as plt 
import pandas as pd
data = pd.read_excel('./excel文件/Users.xlsx') # 這里數(shù)據(jù)文件就不再展示
data['total'] = data['Oct'] + data['Nov'] + data['Dec']
data.sort_values(by='total',inplace=True,ascending=False)
# data.plot.bar(x='Name',y=['Oct','Nov','Dec'],stacked=True,title='Users Behavior') 豎直
data.plot.barh(x='Name',y=['Oct','Nov','Dec'],stacked=True,title='Users Behavior') #水平 如果需要改變順序,將ascending=true
plt.tight_layout()
plt.show()

結(jié)果如下圖所示:

圖片

餅圖

首先我們先查看一下數(shù)據(jù)文件,如下所示:

圖片

給出了2016,2017年來自不同國家的學(xué)生數(shù)量排名(rank);

由此繪制餅圖:

import pandas as pd
import matplotlib.pyplot as plt
plt.figure(figsize=(12,8),dpi=100)

students = pd.read_excel('./bin_Students.xlsx', index_col='From')

students['2017'].plot.pie(fontsize=8,counterclock=False)
plt.title('Source of International Students', fontsize=16, fontweight='bold')
plt.ylabel('2017', fontsize=12, fontweight='bold')
plt.show()

解釋說明:

  1. figsize:設(shè)置圖片大小,dpi:設(shè)置圖片分辨率;
  2. pie()函數(shù)繪制餅圖的的api函數(shù),counterclock=False:這個(gè)參數(shù)設(shè)置餅圖占比元素的環(huán)繞方向,false變送逆時(shí)針;

結(jié)果如下圖所示:

圖片

折線圖

由于文件數(shù)據(jù)過多,這里不做展示;簡言之就是四個(gè)區(qū)域的不同周期的銷售狀況

繪圖代碼:

import pandas as pd 
import matplotlib.pyplot as plt
data = pd.read_excel('./excel文件/Orders.xlsx',index_col='Week')
data.plot.area(y=['Accessories','Bikes','Clothing','Components']) #疊加區(qū)域圖
plt.title('Sale Week Trend',fontsize=14,fontweight='bold')
plt.ylabel('Total',fontsize=10,fontweight='bold')
plt.xticks(data.index,fontsize=5)
plt.show()

結(jié)果如下:

圖片

由于后面集中圖形使用較少,這里不再展示數(shù)據(jù)文件,直接上代碼:

散點(diǎn)圖

import matplotlib.pyplot as plt 
import pandas as pd
data = pd.read_excel('./excel文件/home_data.xlsx',index_col='id')
data.plot.scatter(x='sqft_living',y='price') # 房子面積 價(jià)位
plt.show()

結(jié)果如下:

圖片

該圖形繪制的是在某地區(qū)房子價(jià)位與房子面積的分布關(guān)系;

直方圖

import matplotlib.pyplot as plt 
import pandas as pd
data = pd.read_excel('./excel文件/home_data.xlsx')
data['sqft_living'].plot.hist(bins=60) # bins 表示柱子的數(shù)量
plt.xticks(range(0,max(data['sqft_living']),500),rotation=90,fontsize=6)
plt.show()

結(jié)果如下:

圖片

該圖表示某地區(qū)房子的價(jià)位的分布情況。

總結(jié)

以上就是使用pandas結(jié)合matplotlib繪制一些基本常用圖形的例子,當(dāng)然了例子是固定的,圖形是靈活的,我們還是要根據(jù)不同的數(shù)據(jù)表,結(jié)合不同的現(xiàn)實(shí)狀況,繪制不同的圖形達(dá)到我們的目的。

責(zé)任編輯:龐桂玉 來源: Python共享之家
相關(guān)推薦

2020-02-28 11:29:00

ElasticSear概念類比

2024-04-17 13:21:02

Python匿名函數(shù)

2021-05-15 10:16:14

Python匿名函數(shù)

2021-11-17 10:11:08

PythonLogging模塊

2022-03-30 10:51:40

JavaScript性能調(diào)優(yōu)

2021-03-06 10:05:03

Python函數(shù)變量

2021-11-10 09:19:41

PythonShutil模塊

2021-03-15 08:38:42

StringBuffeJava基礎(chǔ)Java開發(fā)

2022-02-21 09:44:45

Git開源分布式

2023-05-12 08:19:12

Netty程序框架

2021-06-30 00:20:12

Hangfire.NET平臺(tái)

2021-11-13 10:11:45

Pythonurllib庫Python基礎(chǔ)

2021-01-13 08:40:04

Go語言文件操作

2021-05-31 08:59:57

Java數(shù)據(jù)庫訪問JDBC

2021-02-20 10:06:14

語言文件操作

2021-02-27 10:20:18

Go語言flag包開發(fā)技術(shù)

2023-07-30 15:18:54

JavaScript屬性

2023-05-08 08:21:15

JavaNIO編程

2021-01-26 23:46:32

JavaScript數(shù)據(jù)結(jié)構(gòu)前端

2021-03-09 14:04:01

JavaScriptCookie數(shù)據(jù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)