Python 圖形繪制詳細(xì)代碼(二)
更新時(shí)間:2021年12月22日 17:09:34 作者:來(lái)西瓜
這篇文章主要介紹了Python 圖形繪制詳細(xì)代碼,本文接著上文介紹介紹條形圖的畫(huà)法,同樣附有詳細(xì)的代碼,需要的小伙伴可以參考一下,希望對(duì)你的學(xué)習(xí)有所幫助
4、條形圖
下面介紹條形圖的畫(huà)法。
4.1 代碼
import matplotlib.pyplot as plt # x-coordinates of left sides of bars left = [1, 2, 3, 4, 5] # heights of bars height = [10, 24, 36, 40, 5] # labels for bars tick_label = ['one', 'two', 'three', 'four', 'five'] # plotting a bar chart plt.bar(left, height, tick_label = tick_label, width = 0.8, color = ['red', 'green']) # naming the x-axis plt.xlabel('x - axis') # naming the y-axis plt.ylabel('y - axis') # plot title plt.title('My bar chart!') # function to show the plot plt.show()
4.2 輸出
4.3 代碼的部分解釋
- 1)使用
plt.bar()
函數(shù)來(lái)繪制條形圖。 - 2)x軸與
height
兩個(gè)參數(shù)必須有。 - 3)可以通過(guò)定義
tick_labels
為 x 軸坐標(biāo)指定另外的名稱(chēng)。
5、直方圖
5.1 代碼
import matplotlib.pyplot as plt # frequencies ages = [2,5,70,40,30,45,50,45,43,40,44, 60,7,13,57,18,90,77,32,21,20,40] # setting the ranges and no. of intervals range = (0, 100) bins = 10 # plotting a histogram plt.hist(ages, bins, range, color = 'green', histtype = 'bar', rwidth = 0.8) # x-axis label plt.xlabel('age') # frequency label plt.ylabel('No. of people') # plot title plt.title('My histogram') # function to show the plot plt.show()
5.2 輸出
5.3 代碼的部分解釋
- 1)使用 plt.hist() 函數(shù)繪制直方圖。
- 2)age列表作為頻率傳入函數(shù)。
- 3)可以通過(guò)定義包含最小值和最大值的元組來(lái)設(shè)置范圍。
- 4)下一步是對(duì)值的范圍進(jìn)行“裝箱”——即將整個(gè)值范圍劃分為一系列區(qū)間——然后計(jì)算落入每個(gè)區(qū)間的值的數(shù)量。 這里我們定義了
bins = 10
。所以,總共有100/10 = 10
個(gè)區(qū)間。
6、散點(diǎn)圖
6.1 代碼
import matplotlib.pyplot as plt # x-axis values x = [1,2,3,4,5,6,7,8,9,10] # y-axis values y = [2,4,5,7,6,8,9,11,12,12] # plotting points as a scatter plot plt.scatter(x, y, label= "stars", color= "green", marker= "*", s=30) # x-axis label plt.xlabel('x - axis') # frequency label plt.ylabel('y - axis') # plot title plt.title('My scatter plot!') # showing legend plt.legend() # function to show the plot plt.show()
6.2 輸出
6.3 代碼的部分解釋
- 1)使用
plt.scatter()
函數(shù)繪制散點(diǎn)圖。 - 2)作為一條線,我們?cè)谶@里也定義了 x 和相應(yīng)的 y 軸值。
- 3)標(biāo)記參數(shù)用于設(shè)置用作標(biāo)記的字符。 它的大小可以使用 s 參數(shù)定義。
到此這篇關(guān)于Python 圖形繪制詳細(xì)代碼的文章就介紹到這了,更多相關(guān)Python 圖形繪制內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python3 requests庫(kù)實(shí)現(xiàn)多圖片爬取教程
今天小編就為大家分享一篇python3 requests庫(kù)實(shí)現(xiàn)多圖片爬取教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12Python+Plotly繪制精美的數(shù)據(jù)分析圖
Plotly?是目前已知的Python最強(qiáng)繪圖庫(kù),比Echarts還強(qiáng)大許多。它的繪制通過(guò)生成一個(gè)web頁(yè)面完成,并且支持調(diào)整圖像大小,動(dòng)態(tài)調(diào)節(jié)參數(shù)。本文將利用Plotly繪制精美的數(shù)據(jù)分析圖,感興趣的可以了解一下2022-05-05使用BeeWare實(shí)現(xiàn)iOS調(diào)用Python方式
這篇文章主要介紹了使用BeeWare實(shí)現(xiàn)iOS調(diào)用Python方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12