欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python?matplotlib各種畫圖

 更新時(shí)間:2021年12月26日 10:16:13   作者:L-M-Y  
這篇文章主要介紹了python?matplotlib各種畫圖,matplotlib是一種優(yōu)秀的python數(shù)據(jù)可視化第三方庫,使用matpltlib庫畫圖時(shí),先將它引入,加載里面的pyplot,并命名為plt,然后使用plot函數(shù)畫圖<BR>,下面一起來了解更詳細(xì)內(nèi)容吧

1.引入matpltlib庫

matplotlib是一種優(yōu)秀的python數(shù)據(jù)可視化第三方庫
使用matpltlib庫畫圖時(shí),先將它引入,加載里面的pyplot,并命名為plt,然后使用plot函數(shù)畫圖

import matplotlib.pyplot as plt #plt是引入模塊的別名


2.pyplot基礎(chǔ)圖標(biāo)函數(shù)總結(jié)

3.plot函數(shù)畫圖語法規(guī)則

plot函數(shù)參數(shù):plot([x],y,[format],**kwargs)

各類語法太多啦,偷幾張MOOC的圖放上來~

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

4.折線圖

from matplotlib import pyplot as plt

#生成數(shù)據(jù)
#橫坐標(biāo)數(shù)據(jù)從2017到2022,第三個(gè)參數(shù)可控制步長,可寫可不寫
x = range(2017, 2022)
#y對應(yīng)縱坐標(biāo)的值
y1 = [49, 48, 45, 52, 50]
y2 = [60, 62, 61, 65, 63]
#生成圖形
plt.title("LMY and her mother's weight")
plt.xlabel('year')
plt.ylabel('kg')
plt.plot(x, y1, color='green', label='LMY')
plt.plot(x, y2, color='purple', label='mother')
plt.grid(alpha=0.5)
plt.legend(loc='upper right')
#顯示圖形
plt.show()

請?zhí)砑訄D片描述

4.散點(diǎn)圖

from matplotlib import pyplot as plt
import numpy as np

# 生成數(shù)據(jù)
# 橫坐標(biāo)數(shù)據(jù)從2017到2022,第三個(gè)參數(shù)可控制步長,可寫可不寫
x = range(2017, 2022)
# y對應(yīng)縱坐標(biāo)的值
y1 = [49, 48, 45, 52, 50]
y2 = [60, 62, 61, 65, 63]
# 生成圖形
plt.title("LMY and her mother's weight")
plt.xlabel('year')
plt.ylabel('kg')
# 點(diǎn)的大小
area = np.pi*4**2
plt.scatter(x, y1, s=area, c='yellow', alpha=1)
plt.scatter(x, y2, s=area, c='blue', alpha=1)
plt.legend()
plt.yticks(())
plt.show()

請?zhí)砑訄D片描述

5.直方圖

from matplotlib import pyplot as plt
import numpy as np

# 生成數(shù)據(jù)
# 橫坐標(biāo)數(shù)據(jù)從2017到2022,第三個(gè)參數(shù)可控制步長,可寫可不寫
x = [2017, 2018, 2019, 2020, 2021]
# y對應(yīng)縱坐標(biāo)的值
y1 = [49, 48, 45, 52, 50]
y2 = [60, 62, 61, 65, 63]
# 生成圖形
plt.title("LMY and her mother's weight")
plt.ylabel('frequency')
plt.xlabel('kg')
# 點(diǎn)的大小
plt.hist(y1, bottom=None, color='purple')
plt.hist(y2, bottom=None, color='pink')
plt.show()

# n, bins, patches = plt.hist(arr, bins=50, normed=1, facecolor='green', alpha=0.75)
'''
arr:需要計(jì)算直方圖的一維數(shù)組
bins:直方圖的柱數(shù),可選項(xiàng),默認(rèn)為10
normed:是否將得到的直方圖向量歸一化,默認(rèn)為0
facecolor:直方圖顏色
alpha:透明度
'''

在這里插入圖片描述

6.條形圖

縱向

from matplotlib import pyplot as plt
import numpy as np

arr = np.arange(2017, 2022)
x = [49, 48, 45, 52, 50]  # x軸
y = [2017, 2018, 2019, 2020, 2021]
rect = plt.bar(arr, x, width=0.5)
plt.title('LMY')
plt.xlabel('weight')
plt.ylabel('year')
plt.legend()

plt.show()

請?zhí)砑訄D片描述

橫向

請?zhí)砑訄D片描述

多條

from matplotlib import pyplot as plt
import numpy as np

arr = np.arange(2017, 2022)
x1 = [49, 48, 45, 52, 50]  # x軸
x2 = [60, 62, 61, 65, 63]
y = [2017, 2018, 2019, 2020, 2021]
rects1 = plt.bar(arr, x1, 0.5, color='purple', label='LMY')
rects2 = plt.bar(arr, x2, 0.5, color='yellow', label='Mother', alpha=0.3)
plt.title("LMY and her mother's weight")
plt.xlabel('weight')
plt.ylabel('year')
plt.legend()

plt.show()

請?zhí)砑訄D片描述

7.餅圖

from matplotlib import patches, pyplot as plt
import numpy as np

label_list = ['49', '48', '45', '52', '50']
size = [20, 20, 20, 20, 20]
# 各部分的顏色
color = ['red', 'pink', 'blue', 'green', 'purple']
explode = [0, 0, 0.15, 0, 0]

patches, l_text, p_text = plt.pie(size, explode=explode, colors=color, labels=label_list,
                                  labeldistance=1.2, autopct="%1.2f%%", shadow=False, startangle=90, pctdistance=0.6)
plt.axis('equal')
plt.title("LMY's weight")
plt.legend(loc='upper left')
plt.show()

請?zhí)砑訄D片描述

到此這篇關(guān)于python matplotlib各種畫圖的文章就介紹到這了,更多相關(guān)python matplotlib畫圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論