亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python利用matplotlib繪制散點(diǎn)圖的新手教程

 更新時(shí)間:2020年11月05日 10:04:18   作者:Python碎片  
這篇文章主要給大家介紹了關(guān)于Python利用matplotlib繪制散點(diǎn)圖的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

前言

上篇文章介紹了使用matplotlib繪制折線圖,參考:http://chabaoo.cn/article/198991.htm,本篇文章繼續(xù)介紹使用matplotlib繪制散點(diǎn)圖。

一、matplotlib繪制散點(diǎn)圖

# coding=utf-8
import matplotlib.pyplot as plt
 
 
years = [2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019]
turnovers = [0.5, 9.36, 52, 191, 350, 571, 912, 1027, 1682, 2135, 2684]
plt.figure(figsize=(10, 10), dpi=100)
plt.scatter(years, turnovers)
plt.show()

運(yùn)行結(jié)果:

scatter(): matplotlib中繪制散點(diǎn)圖的函數(shù)??梢詡魅牒芏鄥?shù),一般傳入兩個(gè)列表,分別是散點(diǎn)圖中的x值和y值。上面的例子中使用2009年至2019年這十一年天貓雙11的總成交額數(shù)據(jù)。

散點(diǎn)圖根據(jù)提供的兩組數(shù)據(jù),構(gòu)成圖形中的多個(gè)坐標(biāo)點(diǎn)。根據(jù)坐標(biāo)點(diǎn)的分布,分析兩個(gè)變量之間是否存在某種關(guān)聯(lián),或總結(jié)坐標(biāo)點(diǎn)的分布趨勢(shì),用于預(yù)測(cè)數(shù)據(jù)的走勢(shì)。

上面的代碼已經(jīng)實(shí)現(xiàn)了簡(jiǎn)單的散點(diǎn)圖,但只把點(diǎn)繪制出來(lái)了,很多信息都不完整,所以需要進(jìn)行優(yōu)化。

二、matplotlib優(yōu)化散點(diǎn)圖

import matplotlib.pyplot as plt
 
 
years = [2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019]
turnovers = [0.5, 9.36, 52, 191, 350, 571, 912, 1027, 1682, 2135, 2684]
plt.figure(figsize=(10, 15), dpi=100)
plt.scatter(years, turnovers, c='red', s=100, label='成交額')
plt.xticks(range(2008, 2020, 1))
plt.yticks(range(0, 3200, 200))
plt.xlabel("年份", fontdict={'size': 16})
plt.ylabel("成交額", fontdict={'size': 16})
plt.title("歷年天貓雙11總成交額", fontdict={'size': 20})
plt.legend(loc='best')
plt.show()

運(yùn)行結(jié)果:

在第一次繪制的散點(diǎn)圖中,已經(jīng)看出了點(diǎn)的大概分布情況,所以在使用figure()函數(shù)創(chuàng)建圖像時(shí),可以修改figsize參數(shù)調(diào)整圖像尺寸,設(shè)置更好的圖像比例。

在調(diào)用scatter()函數(shù)繪制散點(diǎn)圖時(shí),使用c='顏色'來(lái)設(shè)置點(diǎn)的顏色,使用s='大小'來(lái)設(shè)置點(diǎn)的大小,并設(shè)置label用于圖例展示。

第一次的散點(diǎn)圖中,x軸上沒(méi)有顯示所有的年份刻度,最后一個(gè)點(diǎn)已經(jīng)分布到了圖形的右上角,所以使用xticks()和yticks()來(lái)設(shè)置x軸和y軸的刻度標(biāo)簽和范圍。

使用xlabel()和ylabel()設(shè)置x軸和y軸的標(biāo)簽,說(shuō)明x軸和y軸的含義。使用title()設(shè)置散點(diǎn)圖的標(biāo)題,說(shuō)明散點(diǎn)圖展示的數(shù)據(jù)。使用legend()將圖例展示出來(lái)。

這樣一張基本功能完整,信息完整的散點(diǎn)圖就完成了。

三、matplotlib散點(diǎn)圖區(qū)分點(diǎn)的顏色和大小

import matplotlib.pyplot as plt
import numpy as np
 
 
years = [2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019]
turnovers = [0.5, 9.36, 52, 191, 350, 571, 912, 1027, 1682, 2135, 2684]
plt.figure(figsize=(10, 15), dpi=100)
size = list()
for tur in turnovers:
 size.append(tur) if tur > 100 else size.append(100)
plt.xticks(range(2008, 2020, 1))
plt.yticks(range(0, 3200, 200))
plt.scatter(years, turnovers, c=np.random.randint(0, 50, 11), s=size)
plt.xlabel("年份", fontdict={'size': 16})
plt.ylabel("成交額", fontdict={'size': 16})
plt.title("歷年天貓雙11總成交額", fontdict={'size': 20})
plt.show()

運(yùn)行結(jié)果:

上一張散點(diǎn)圖中已經(jīng)對(duì)數(shù)據(jù)作了基本的展示,為了使數(shù)據(jù)展示效果更好,可以對(duì)散點(diǎn)圖進(jìn)行美化。

數(shù)據(jù)是歷年雙11的總成交額,每年的數(shù)據(jù)是獨(dú)立的,可以用不同的顏色來(lái)區(qū)分。這里使用numpy中的random.randint()隨機(jī)生成11個(gè)值,將這11個(gè)隨機(jī)的值傳給scatter()函數(shù)中的c參數(shù),使每一個(gè)點(diǎn)的顏色不一樣,可以更好地表示每個(gè)點(diǎn)的獨(dú)立性。

pip install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple

成交額是逐年變化的,為了從散點(diǎn)圖中體現(xiàn)出大小的差異,可以根據(jù)成交額的大小設(shè)置點(diǎn)的大小。這里直接將成交額作為點(diǎn)的大小(成交額很小的設(shè)置一個(gè)值,圖形中的點(diǎn)不小于這個(gè)值),得到由11個(gè)值組成的列表,傳給scatter()函數(shù)中的s參數(shù),可以體現(xiàn)每個(gè)點(diǎn)的大小差異(成交額越大點(diǎn)越大)。

四、matplotlib散點(diǎn)圖的趨勢(shì)簡(jiǎn)單分析

import matplotlib.pyplot as plt
import numpy as np
import math
 
 
years = [2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019]
turnovers = [0.5, 9.36, 52, 191, 350, 571, 912, 1027, 1682, 2135, 2684]
squares = [math.pow(year-2008, 3.3) for year in years]
powers = [math.pow(2, year-2008) for year in years]
plt.figure(figsize=(10, 15), dpi=100)
size = list()
for tur in turnovers:
 size.append(tur) if tur > 100 else size.append(100)
plt.xticks(range(2008, 2020, 1))
plt.yticks(range(0, 3200, 200))
plt.scatter(years, turnovers, c=np.random.randint(0, 50, 11), s=size, label='成交額')
plt.plot(years, squares, color='red', label='x^3.4')
plt.plot(years, powers, color='blue', label='2^n')
plt.legend(loc='best', fontsize=16, markerscale=0.5)
plt.xlabel("年份", fontdict={'size': 16})
plt.ylabel("成交額", fontdict={'size': 16})
plt.title("歷年天貓雙11總成交額", fontdict={'size': 20})
plt.show()

運(yùn)行結(jié)果:

散點(diǎn)圖的作用主要是用于分析數(shù)據(jù)的趨勢(shì),用于預(yù)測(cè)未來(lái)的數(shù)據(jù)。比如我想預(yù)測(cè)2020年天貓雙11的總成交額,通過(guò)對(duì)比的方式,簡(jiǎn)單分析一下這個(gè)趨勢(shì)更接近指數(shù)函數(shù)還是更接近多次函數(shù)。

在散點(diǎn)圖中,我繪制了兩條曲線,y=2^x和y=x^(3.4),一條是2為底的指數(shù)函數(shù),一條是x的3.4次方(三次函數(shù)ax^3+bx^2+cx+d),可以看到雙11總成交額的變化趨勢(shì)更接近三次函數(shù)。

這里我只是簡(jiǎn)單對(duì)比一下,三次函數(shù)還有二次項(xiàng)、一次項(xiàng)和常數(shù)項(xiàng),所以x^(3.4)中的0.4可以通過(guò)二次項(xiàng)、一次項(xiàng)和常數(shù)項(xiàng)來(lái)補(bǔ)充,指數(shù)函數(shù)的變化趨勢(shì)太快,與雙11總成交額的變化趨勢(shì)差異很大。這種簡(jiǎn)單對(duì)比是很粗糙的,只是為了說(shuō)明散點(diǎn)圖可以用于分析趨勢(shì)。真實(shí)的分析不能簡(jiǎn)單看每年的數(shù)據(jù),需要考慮很多因素(甚至因?yàn)槟硞€(gè)因素的加入,成交額已經(jīng)快到天花板了,很可能后面會(huì)下降)。

總結(jié)

到此這篇關(guān)于Python利用matplotlib繪制散點(diǎn)圖的文章就介紹到這了,更多相關(guān)Python matplotlib繪制散點(diǎn)圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論