pyplot幾個(gè)常用函數(shù)介紹
- figure 創(chuàng)建一個(gè)圖猖腕,用于標(biāo)識(shí)下面的繪制在這個(gè)圖中
- axis 描述 橫坐標(biāo)和縱坐標(biāo)的顯示區(qū)間樱报, 舉例: [-20, 20, -100, 600] , 橫坐標(biāo)在[-20穷吮, 20],縱坐標(biāo)在[-100, 600]
- plot 將數(shù)據(jù)寫(xiě)到該圖中盾剩,plot的前兩個(gè)參數(shù)分別是橫坐標(biāo)數(shù)組盈简,縱坐標(biāo)數(shù)組。 舉例plot(x, y)携取,將x序列攒钳,y序列 以(x, y) 數(shù)值對(duì) ,繪制到該圖中
- show 顯示該圖
下面給出一個(gè)實(shí)例
def test_plot():
plt.figure()
draw_axis(plt, [-20, 20], [-100, 100])
x = [] # 儲(chǔ)存橫坐標(biāo)值
y = [] # 儲(chǔ)存縱坐標(biāo)值
for key in range(-20, 20):
if key <= 0:
value = key*2 + 1 # 即 y = 2x + 1的函數(shù)
else:
value = 0.25*key*key + 1 # 即 y = 0.25*x^2 + 1的函數(shù)
# 分別保存橫縱坐標(biāo)
x.append(key)
y.append(value)
plt.axis([-20, 20, -100, 100])
plt.plot(x, y)
plt.show()
運(yùn)行的效果圖如下:
實(shí)例圖
pyplot更多函數(shù)介紹
- scatter 這個(gè)函數(shù)是繪制散點(diǎn)
前兩個(gè)參數(shù)是輸入x, y的坐標(biāo)序列雷滋,后面s=定義點(diǎn)的大小不撑,c=定義顏色文兢。余下不贅述,用到再研究焕檬。
見(jiàn)下面的示例:
xx = [40920.0, 14488.0, 26052.0, 75136.0, 38344.0, 72993.0, 35948.0, 42666.0, 67497.0, ..., 43757.0]
yy = [8.326976, 7.153469, 1.441871, 13.147394, 1.669788, 10.14174, 6.830792, 13.276369, ..., 7.882601]
def test_plot():
plt.scatter(xx, yy, s=100, c='r') # 點(diǎn)的大小設(shè)為100姆坚, 顏色為紅色
plt.show()
效果圖如下:
效果圖
現(xiàn)在將大小、顏色作為參數(shù)傳入实愚,
weight = [3, 2, 1, 1, 1, 1, 3, 3, 1, ..., 3]
def test_plot():
# s和c都支持array和list的類型參數(shù)兼呵,但是array類型的更好用,因?yàn)橹С纸oweight統(tǒng)一乘以15腊敲,將點(diǎn)的大小區(qū)分開(kāi)击喂。至于顏色,貌似只要數(shù)值不同碰辅,會(huì)默認(rèn)分配幾種不同顏色
plt.scatter(xx, yy, s=15.0*array(weight), c=weight)
plt.show()
效果圖如下:
添加大小和顏色