python學習筆記之資料的視覺化(三)

2021-10-24 07:20:33 字數 3381 閱讀 9837

疫情資料的視覺化

1> matplotlib

導庫命令:import matplot.pyplot as plt

2> 視覺化流程

1.繪製畫布及繪製圖形

plt.figure(figsize=(8,6),dpi =80)

plt.plot(x,y,color,linestyle)

引數含義:color表示顏色,顏色的單詞

linestyle表示線型,- -- : -.

plt.scatter(x,y,color,marker,s)

引數含義:color 同上

marker表示點的形狀 o/v/^/s/*/d/d/h/h

s表示點的大小

2.新增元素

plt.title()

plt.xlabel()

plt.ylabel()

#表示x,y軸的取值範圍

plt.xlim(元組)

plt.ylim(元組)

#表示x,y軸的刻度

plt.xticks(列表)

plt.yticks(列表)

3.顯示

3>補充一些操作

pd.set_option('display.unicode.ambiguous_as_wide', true)#設定列名對齊

pd.set_option('display.unicode.east_asian_width', true)#設定列名對齊

pd.set_option('display.max_rows',none) #顯示所有行

pd.set_option('display.max_columns',none)#顯示所有列

pd.set_option('expand_frame_repr', false)#設定不換行

%matplotlib notebook   

#功能3--資料視覺化**

#允許在notebook中顯示動畫

#動畫繪圖--animation

from matplotlib import animation #動畫繪相簿

import matplotlib.pyplot as plt #匯入視覺化庫

import numpy as np

import random

from ipython.display import html #html中顯示動畫

import pandas as pd

pd.set_option(

'display.unicode.ambiguous_as_wide'

,true

)pd.set_option(

'display.unicode.east_asian_width'

,true

)#設定列名對其

# 函式1--開啟檔案

defopen_def

(file_name)

: file_name1 =

open

(file_name,encoding=

'utf-8'

)#用open命令按照utf-8格式開啟

file_v1 = pd.read_csv(file_name1)

#然後用pandas的命令讀檔案

file_v2 = file_v1.drop(

'unnamed: 0'

,axis=1)

#刪除列,

return file_v2

file_name =

"china_hist_data.csv"

file_v1 = open_def(file_name)

file_v3 = file_v1.iloc[:,

[1,5

,6,9

,10,11

,13]]

plt.rcparams[

'font.sans-serif']=

'simhei'

#設定字型

plt.rcparams[

'axes.unicode_minus']=

false

#字元顯示,更多詳見csdn

fig = plt.figure(figsize =(8

,6),dpi=80)

#定義繪圖畫布

ax1 = fig.add_subplot(

111)

#繪製子圖

x =list

(file_v3.columns)[-

1:-5

:-1]

#找出列標題中的後四個,

y = file_v3.values[:,

3:]#找出資料值中,第3列開始的所有列

legend_v1 =

list

(file_v3[

'時間'

].values)

#以時間的值,作為圖例項

defupdate

(num)

:#要執行的函式

ax1.clear(

)#清除之前繪製的內容

color_v1=

['r'

,'b'

,'y'

,'g'

] line_v1 =

['-'

,'--'

,'-.'

,':'

] ax1.plot(x,y[num]

,color=random.choice(color_v1)

,linestyle=random.choice(line_v1)

)for i in

range

(len

(x))

: plt.annotate(xy=

(i,y[num]

[i]+10)

,s=y[num]

[i])

plt.legend(

[legend_v1[num]])

return

none

ani = animation.funcanimation(fig, update, frames=np.arange(y.shape[0]

),interval=

1000

, blit=

true

,repeat=

true

)#這裡的frames在呼叫update函式是會將frames作為實參傳遞給「n」

# animation中的frames是,要執行函式多少次。一般是乙個可迭代物件,將引數依次給了函式,

plt.show(

)# html(ani.to_jshtml())

python學習筆記之資料的分析(二)

疫情資料分析 1.匯入外部資料 pd.read csv 檔名 檢視資料屬性 columns 列名 index 行索引 shape m行n列 dtyps 各列的型別 2.查詢資料 方法1 用列名直接查詢 df 列名列表 方法2 用loc查詢 df.loc 行取值,列取值 注意 行取值可以用邏輯值來進行...

Python資料分析之pandas資料視覺化

python資料視覺化常用的是matplotlib庫,matplotlib是底層庫,今天學了pandas的資料視覺化,相對於matplotlib庫來說,簡單許多。我們也可以加入grid引數新增格網 pandas繪圖其實是對matplotlib庫繼承,而matplotlib庫預設為ascii編碼,所以...

Python學習筆記之資料型別

計算機簡而言之為進行計算的機器。計算機的所有功能的本質是計算功能,無論是網路資訊發布還是醫院的計費系統,其本質都是計算。而計算機語言則是人類與計算機互動的語言,人類通過計算機語言告訴計算機做什麼事情。python自然也不例外,它實際上就是人類與計算機溝通的語言或者說工具。然而,python天生又是為...