Python 圖形繪制詳細(xì)代碼(二)
更新時間:2021年12月22日 17:09:34 作者:來西瓜
這篇文章主要介紹了Python 圖形繪制詳細(xì)代碼,本文接著上文介紹介紹條形圖的畫法,同樣附有詳細(xì)的代碼,需要的小伙伴可以參考一下,希望對你的學(xué)習(xí)有所幫助
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 軸坐標(biāo)指定另外的名稱。
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)可以通過定義包含最小值和最大值的元組來設(shè)置范圍。
- 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īng)的 y 軸值。
- 3)標(biāo)記參數(shù)用于設(shè)置用作標(biāo)記的字符。 它的大小可以使用 s 參數(shù)定義。
到此這篇關(guān)于Python 圖形繪制詳細(xì)代碼的文章就介紹到這了,更多相關(guān)Python 圖形繪制內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
跟老齊學(xué)Python之集成開發(fā)環(huán)境(IDE)
IDE的全稱是:Integrated Development Environment,簡稱IDE,也稱為Integration Design Environment、Integration Debugging Environment,翻譯成中文叫做“集成開發(fā)環(huán)境”,在臺灣那邊叫做“整合開發(fā)環(huán)境”。2014-09-09
python人工智能深度學(xué)習(xí)入門邏輯回歸限制
這篇文章主要為大家介紹了python人工智能深度學(xué)習(xí)入門之邏輯回歸限制的詳細(xì)講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2021-11-11
利用Python+Java調(diào)用Shell腳本時的死鎖陷阱詳解
這篇文章主要給大家介紹了關(guān)于利用Python+Java調(diào)用Shell腳本時的死鎖陷阱的相關(guān)資料,文章通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01

