Python 圖形繪制詳細代碼(二)
更新時間:2021年12月22日 17:09:34 作者:來西瓜
這篇文章主要介紹了Python 圖形繪制詳細代碼,本文接著上文介紹介紹條形圖的畫法,同樣附有詳細的代碼,需要的小伙伴可以參考一下,希望對你的學習有所幫助
4、條形圖
下面介紹條形圖的畫法。
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ù)來繪制條形圖。 - 2)x軸與
height
兩個參數(shù)必須有。 - 3)可以通過定義
tick_labels
為 x 軸坐標指定另外的名稱。
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)可以通過定義包含最小值和最大值的元組來設置范圍。
- 4)下一步是對值的范圍進行“裝箱”——即將整個值范圍劃分為一系列區(qū)間——然后計算落入每個區(qū)間的值的數(shù)量。 這里我們定義了
bins = 10
。所以,總共有100/10 = 10
個區(qū)間。
6、散點圖
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ù)繪制散點圖。 - 2)作為一條線,我們在這里也定義了 x 和相應的 y 軸值。
- 3)標記參數(shù)用于設置用作標記的字符。 它的大小可以使用 s 參數(shù)定義。
到此這篇關于Python 圖形繪制詳細代碼的文章就介紹到這了,更多相關Python 圖形繪制內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
python3 requests庫實現(xiàn)多圖片爬取教程
今天小編就為大家分享一篇python3 requests庫實現(xiàn)多圖片爬取教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12Python+Plotly繪制精美的數(shù)據(jù)分析圖
Plotly?是目前已知的Python最強繪圖庫,比Echarts還強大許多。它的繪制通過生成一個web頁面完成,并且支持調整圖像大小,動態(tài)調節(jié)參數(shù)。本文將利用Plotly繪制精美的數(shù)據(jù)分析圖,感興趣的可以了解一下2022-05-05使用BeeWare實現(xiàn)iOS調用Python方式
這篇文章主要介紹了使用BeeWare實現(xiàn)iOS調用Python方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12