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

Python中plt.plot()、plt.scatter()和plt.legend函數(shù)的用法示例

 更新時間:2022年03月29日 10:49:55   作者:Sunny.T  
今天想要用matplotlib中的plt函數(shù)繪制圖表,將多個數(shù)據(jù)曲線在一個圖表中進(jìn)行呈現(xiàn),下面這篇文章主要給大家介紹了關(guān)于Python中plt.plot()、plt.scatter()和plt.legend函數(shù)用法的相關(guān)資料,需要的朋友可以參考下

plt.plot()函數(shù)

plt.plot(x, y, format_string, **kwargs)
參數(shù)說明
xX軸數(shù)據(jù),列表或數(shù)組,可選
yY軸數(shù)據(jù),列表或數(shù)組
format_string控制曲線的格式字符串,可選
**kwargs第二組或更多(x,y,format_string),可畫多條曲線

format_string 由顏色字符、風(fēng)格字符、標(biāo)記字符組成

  • 顏色字符
    • 'b' 藍(lán)色 'm' 洋紅色 magenta
    • 'g' 綠色 'y' 黃色
    • 'r' 紅色 'k' 黑色
    • 'w' 白色 'c' 青綠色 cyan
    • '#008000' RGB某顏色 '0.8' 灰度值字符串
    • 多條曲線不指定顏色時,會自動選擇不同顏色
  • 風(fēng)格字符
    • '‐' 實線
    • '‐‐' 破折線
    • '‐.' 點劃線
    • ':' 虛線
    • '' ' ' 無線條
  • 標(biāo)記字符
    • '.' 點標(biāo)記
    • ',' 像素標(biāo)記(極小點)
    • 'o' 實心圈標(biāo)記
    • 'v' 倒三角標(biāo)記
    • '^' 上三角標(biāo)記
    • '>' 右三角標(biāo)記
    • '<' 左三角標(biāo)記…等等

**kwargs : 第二組或更多(x,y,format_string)

color : 控制顏色, color=‘green’

linestyle : 線條風(fēng)格, linestyle=‘dashed’

marker : 標(biāo)記風(fēng)格, marker=‘o’

markerfacecolor: 標(biāo)記顏色, markerfacecolor=‘blue’

markersize: 標(biāo)記尺寸, markersize=20

b = np.arange(5)
plt.plot(b,b*1.0,'g.-',b,b*1.5,'rx',b,b*2.0, 'b')
plt.show()

img

plt.scatter()函數(shù)

plt.scatter()函數(shù)用于生成一個scatter散點圖。

matplotlib.pyplot.scatter(x, y, s=20, c='b', marker='o', cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, verts=None, hold=None, **kwargs
參數(shù)解釋說明
x,y表示的是shape大小為(n,)的數(shù)組,也就是我們即將繪制散點圖的數(shù)據(jù)點,輸入數(shù)據(jù)。
s表示的是大小,是一個標(biāo)量或者是一個shape大小為(n,)的數(shù)組,可選,默認(rèn)20。
c表示的是色彩或顏色序列,可選,默認(rèn)藍(lán)色’b’。但是c不應(yīng)該是一個單一的RGB數(shù)字,也不應(yīng)該是一個RGBA的序列,因為不便區(qū)分。c可以是一個RGB或RGBA二維行數(shù)組。
markerMarkerStyle,表示的是標(biāo)記的樣式,可選,默認(rèn)’o’。
cmapColormap,標(biāo)量或者是一個colormap的名字,cmap僅僅當(dāng)c是一個浮點數(shù)數(shù)組的時候才使用。如果沒有申明就是image.cmap,可選,默認(rèn)None。
normNormalize,數(shù)據(jù)亮度在0-1之間,也是只有c是一個浮點數(shù)的數(shù)組的時候才使用。如果沒有申明,就是默認(rèn)None。
vmin,vmax標(biāo)量,當(dāng)norm存在的時候忽略。用來進(jìn)行亮度數(shù)據(jù)的歸一化,可選,默認(rèn)None。
alpha標(biāo)量,0-1之間,可選,默認(rèn)None。
linewidths標(biāo)記點的長度,默認(rèn)None。

例子

import numpy as np
import matplotlib.pyplot as plt
 
np.random.seed(0)
x=np.random.rand(20)
y=np.random.rand(20)

area=(50*np.random.rand(20))**2
 
plt.scatter(x,y,s=area,alpha=0.5)
plt.show()

plt.legend()函數(shù)

1.設(shè)置圖例的位置

plt.legend(loc=' ')

2.設(shè)置圖例字體大小

fontsize : int or float or {‘xx-small', ‘x-small', ‘small', ‘medium', ‘large', ‘x-large', ‘xx-large'}

3.設(shè)置圖例邊框及背景

plt.legend(loc='best',frameon=False) #去掉圖例邊框
plt.legend(loc='best',edgecolor='blue') #設(shè)置圖例邊框顏色
plt.legend(loc='best',facecolor='blue') #設(shè)置圖例背景顏色,若無邊框,參數(shù)無效

4.設(shè)置圖例標(biāo)題

legend = plt.legend(["BJ", "SH"], title='Beijing VS Shanghai')
#或者 
plt.plot(["BJ", "SH"],loc='upper left',title='Beijing VS Shanghai')

5.設(shè)置圖例名字及對應(yīng)關(guān)系

legend = plt.legend([p1, p2], ["BJ", "SH"])

示例

import matplotlib.pyplot as plt
import numpy as np   
x = np.arange(0,10,1)
plt.plot(x,x,'r--',x,np.cos(x),'g--',marker='*')
plt.xlabel('row')
plt.ylabel('cow')
plt.legend(["BJ","SH"],loc='upper left',loc='upper left')
plt.show()

運行結(jié)果

總結(jié)

到此這篇關(guān)于Python中plt.plot()、plt.scatter()和plt.legend函數(shù)用法的文章就介紹到這了,更多相關(guān)plt.plot()、plt.scatter()和plt.legend函數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論