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

Python中使用matplotlib庫(kù)繪制各種圖

 更新時(shí)間:2023年08月18日 09:23:43   作者:鹿上的程序媛  
這篇文章主要介紹了Python中使用matplotlib庫(kù)繪制各種圖方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

1.繪制折線圖(pyplot.plot(x,y))

在這里插入圖片描述

from matplotlib import pyplot
x = range(2,28,2)
y = [15,13,14,17,20,25,26,26,24,22,18,15,1]
//設(shè)置圖片大小
fig = pyplot.figure(figsize=(5,5),dpi=80)
//繪圖
pyplot.plot(x,y)
//保存圖片
pyplot.savefig("./sig_size.png")
//展示圖片
pyplot.show()

繪制兩條折線

# import matplotlib
from matplotlib import pyplot
x = range(11,31)
y1 = [1,0,1,1,2,4,3,2,3,4,4,5,6,5,4,3,3,1,1,1]
y2 = [1,0,3,1,2,2,3,3,2,1,2,1,1,1,1,1,1,1,1,2]
#set the pic size
pyplot.figure(figsize=(10,5),dpi=80)
#ploting
pyplot.plot(x,y1,label="Luna",color="pink",linestyle='--',linewidth=1)
pyplot.plot(x,y2,label="Jim",color="tomato",linestyle='--',linewidth=1)
# set x/y-axis step
_xtick_labels = ["{}".format(i) for i in x]
pyplot.xticks(x,_xtick_labels)
pyplot.yticks(range(0,7))
# set x-asix desciption
pyplot.xlabel("Age")
# set y-asix description
pyplot.ylabel("Num")
# add legend
pyplot.legend()
# plot grid
pyplot.grid(alpha=0.2,linestyle=':')
# show
pyplot.show()

2.繪制散點(diǎn)圖(pyplot.scatter(x,y))

在這里插入圖片描述

from matplotlib import pyplot
from matplotlib import font_manager
y_3 = [11,12,12,13,14,11,12,17,20,25,22,27,21,29,23,22,24,12,11,21,10,12,11,13,34,34,23,22,24,12,11]
y_10 = [34,34,33,33,34,32,31,31,30,32,21,23,23,13,22,21,24,25,23,23,11,12,12,13,34,34,23,22,24,12,11]
x_3 = range(1,32)
x_10 = range(41,72)
pyplot.figure(figsize=(20,10),dpi=80)
pyplot.scatter(x_3,y_3,color="blue",label="March")
pyplot.scatter(x_10,y_10,color="cyan",label="Noverber")
_x = list(x_3)+list(x_10)
_xticks_label = ["March{}".format(i) for i in x_3]
_xticks_label += ["Noverber{}".format(i-40) for i in x_10]
pyplot.xticks(_x[::10],_xticks_label[::10])
pyplot.xlabel("date")
pyplot.ylabel("temperature")
pyplot.legend()
pyplot.show()

3.繪制條形圖(pyplot.bar())

在這里插入圖片描述

from matplotlib import pyplot
from matplotlib import font_manager
my_font = font_manager.FontProperties(fname="")
a = ["Friends","My Heart go on","kongfu"]
b = [57.1,25,12]
pyplot.figure(figsize=(4,6),dpi=80)
pyplot.bar(a,b,width=0.3)
pyplot.xlabel("movie")
pyplot.ylabel("score")
pyplot.xticks(range(len(a)),a,rotation=15)
pyplot.show()

pyplot.barh()=>繪制橫著的條形圖

在這里插入圖片描述

from matplotlib import pyplot
from matplotlib import font_manager
my_font = font_manager.FontProperties(fname="")
a = ["Friends","My Heart go on","kongfu"]
b = [57.1,25,12]
pyplot.figure(figsize=(8,6),dpi=80)
pyplot.barh(a,b,height=0.3)
pyplot.ylabel("movie")
pyplot.xlabel("score")
pyplot.yticks(range(len(a)),a)
pyplot.grid(alpha=0.2)
pyplot.show()

繪制對(duì)比條形圖(繪制三次)

在這里插入圖片描述

from matplotlib import pyplot
a = ["movie1","movie2","movie3","movie4","movie5"]
b_1 = [3124,123,5431,3411,2344]
b_2 = [3456,2123,1455,8764,2323]
b_3 = [213,431,124,56,120]
bar_width=0.2
x_1 = list(range(len(a)))
x_2 = [i+bar_width for i in x_1]
x_3 = [i+bar_width*2 for i in x_1]
pyplot.figure(figsize=(10,4),dpi=80)
pyplot.bar(range(len(a)),b_1,width=bar_width,color="blue",label="1")
pyplot.bar(x_2,b_2,width=bar_width,color="pink",label="2")
pyplot.bar(x_3,b_3,width=bar_width,label="3")
pyplot.xticks(x_2,a)
pyplot.legend()
pyplot.show()

4.繪制直方圖(pyplot.hist())

頻數(shù)分布直方圖pyplot.hist(a,num_bins)

在這里插入圖片描述

from matplotlib import pyplot
a = [9,34,13,73,44,34,76,34,72,17,96,46,84,52,72,26,81,64,79,45,99]
d = 10
num_bins = (max(a)-min(a))//d
pyplot.figure(figsize=(10,6),dpi=80)
pyplot.hist(a,num_bins)
pyplot.xlabel("time")
pyplot.ylabel("num")
pyplot.xticks(range(min(a),max(a)+d,d))
pyplot.grid()
pyplot.show()

頻率分布直方圖pyplot.hist(a,num_bins,density=True)

在這里插入圖片描述

from matplotlib import pyplot
a = [9,34,13,73,44,34,76,34,72,17,96,46,84,52,72,26,81,64,79,45,99]
d = 10
num_bins = (max(a)-min(a))//d
pyplot.figure(figsize=(10,6),dpi=80)
pyplot.hist(a,num_bins,density=True)
pyplot.xlabel("time")
pyplot.ylabel("percentage")
pyplot.xticks(range(min(a),max(a)+d,d))
pyplot.grid()
pyplot.show()

繪制組距變化的直方圖(pyplot.bar())

這里的組距為一個(gè)數(shù)組

在這里插入圖片描述

from matplotlib import pyplot
interval = [0,5,10,15,20,25,30,35,40,45,60,90]
width = [5,5,5,5,5,5,5,5,5,15,30,60]
quality = [836,2737,3723,3926,3596,1438,3273,642,824,613,215,47]
pyplot.bar(range(12),quality,width=1)
_x = [i-0.5 for i in range(13)]
_xtick_label = interval+[150]
pyplot.xticks(_x,_xtick_label)
pyplot.grid()
pyplot.show()

總結(jié)

1.如何選擇哪種圖來(lái)呈現(xiàn)數(shù)據(jù)?

2.matplotlib.plot(x,y)

繪制的是折線圖,x為代表x軸的list,y為代表y軸的值的list,這里的x和y的元素個(gè)數(shù)必須是一致的

在這里插入圖片描述

3.matplotlib.bar(x,height,width)

繪制的是條形圖,x為代表x軸的list,height為對(duì)應(yīng)的x的值,width為條形圖的寬度

在這里插入圖片描述

4.matplotlib.barh(y,width,height)

繪制橫著的條形圖,x和y的含義相反

在這里插入圖片描述

5.matplotlib.scatter(x,y)

繪制散點(diǎn)圖

在這里插入圖片描述

6.matplotlib.hist(x,bin,density)

繪制直方圖,這里的x為源數(shù)據(jù)的數(shù)組,bin為分多少組顯示,這里的圖的y值代表在某個(gè)范圍內(nèi)的頻率或頻數(shù),通過(guò)參數(shù)density可以繪制頻數(shù)直方圖或頻率直方圖,默認(rèn)為頻數(shù)直方圖一般設(shè)置一個(gè)組距dbin = (max(a)-min(x))//d

在這里插入圖片描述

7.xticks和yticks的設(shè)置

設(shè)置x軸和y軸的坐標(biāo)

8.label和title,grid的設(shè)置

9.繪圖的大小(figure)和保存圖片(savefig)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論