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

python?matplotlib庫(kù)繪圖實(shí)戰(zhàn)之繪制散點(diǎn)圖

 更新時(shí)間:2022年07月12日 16:44:26   作者:不見月光見星光  
Python有著強(qiáng)大的繪圖庫(kù) matplotlib,該庫(kù)集成了大量的繪制函數(shù),可以滿足我們平時(shí)絕大多數(shù)的繪圖要求,這篇文章主要給大家介紹了關(guān)于python?matplotlib庫(kù)繪圖實(shí)戰(zhàn)之繪制散點(diǎn)圖的相關(guān)資料,需要的朋友可以參考下

一.導(dǎo)入庫(kù)

import matplotlib.pyplot as plt

二.設(shè)置文字

plt.title("double number", fontsize=24)
plt.xlabel("number", fontsize=14)
plt.ylabel("double", fontsize=14)

三.設(shè)置坐標(biāo)軸參數(shù)

plt.axis([0, 15, 0, 30])

q前面兩個(gè)數(shù)為x軸的始末,后面則為y

四.繪制點(diǎn)

plt.scatter(2, 4, s=20) #s為點(diǎn)的大小
plt.show()

得到這樣子的圖

畢竟繪圖時(shí)我們不可能只畫一個(gè)點(diǎn)

所以還是應(yīng)該引入數(shù)組來解決問題

x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]

將原來的2,4分別替換為x,y

為了在數(shù)據(jù)量較大時(shí),簡(jiǎn)化(偷懶)代碼,可以使用range等函數(shù)或者列表解析的方法,這里就不贅述了,詳見補(bǔ)充1

五.對(duì)點(diǎn)的繼續(xù)處理

1.自定義顏色

plt.scatter(x, y, c='red', edgecolors='none', s=20)

用參數(shù)c設(shè)置點(diǎn)的顏色,用edgecolor設(shè)置邊緣的顏色(在較新的matpoltlib版本中,edgecolors默認(rèn)為none)有關(guān)顏色的詳細(xì)資料,可見補(bǔ)充2

ps:將c設(shè)置為green,edgecolors設(shè)置為black,將引起極度生理不適,特別是點(diǎn)極度密集的曲線中,你懂的

言歸正傳,python的顏色設(shè)置通常為RGB,所以在scatter內(nèi)也可以用RGB參數(shù)來設(shè)置顏色,格式如下:

plt.scatter(x, y, c=(0, 0, 0.6), edgecolors='none', s=20)

RGB小數(shù)模式詳見補(bǔ)充3

2.顏色映射

plt.scatter(x, y, c=y, edgecolors='none', cmap=plt.cm.Blues, s=20)

通過使用cmap,將c設(shè)置為一個(gè)數(shù)組,則可以達(dá)到顏色漸變的效果:

事實(shí)上并非所有的顏色都可以使用,僅有RGB三原色和Oranges等少數(shù)顏色可以…感覺官方文檔上說的十個(gè)好像不太行,格式必須為頭文字大寫結(jié)尾加s

one of {‘tab:blue’, ‘tab:orange’, ‘tab:green’, ‘tab:red’, ‘tab:purple’, ‘tab:brown’, ‘tab:pink’, ‘tab:gray’, ‘tab:olive’, ‘tab:cyan’} which are the Tableau Colors from the ‘tab10’ categorical palette (which is the default color cycle);

That’s all,接下來為補(bǔ)充部分

補(bǔ)充1

偷懶法1:用list函數(shù)加range函數(shù)

x = list(range(1, 100, 2))

先用range(start, end, step)等到一組數(shù),再用list轉(zhuǎn)化為數(shù)組

偷懶法2:解析列表

y = [value * 2 for value in x]

其實(shí)就是把for循環(huán)寫到了里面去

補(bǔ)充2

Matplotlib識(shí)別以下格式以指定顏色:

an RGB or RGBA tuple of float values in [0, 1] (e.g. (0.1, 0.2, 0.5)
or (0.1, 0.2, 0.5, 0.3)). RGBA is short for Red, Green, Blue, Alpha; a
hex RGB or RGBA string (e.g., ‘#0F0F0F’ or ‘#0F0F0F0F’);
速記十六進(jìn)制RGB或RGBA字符串,相當(dāng)于通過復(fù)制每個(gè)字符獲得的十六進(jìn)制RGB或RGBA字符串(例如,’#abc’,相當(dāng)于’#aabbcc’,或’#abcd’,相當(dāng)于’#aabbccdd’);
a string representation of a float value in [0, 1] inclusive for gray
level (e.g., ‘0.5’);
單個(gè)字母字符串,即{‘b’,‘g’,‘r’,‘c’,‘m’,‘y’,‘k’,‘w’}之一,它們是藍(lán)色、綠色、紅色、青色、品紅色、黃色、黑色和白色陰影的速記號(hào);
a X11/CSS4 (“html”) color name, e.g. “blue”; a name from the xkcd
color survey, prefixed with ‘xkcd:’ (e.g., ‘xkcd:sky blue’); a “Cn”
color spec, i.e. ‘C’ followed by a number, which is an index into the
default property cycle (rcParams[“axes.prop_cycle”] (default:
cycler(‘color’, [’#1f77b4’, ‘#ff7f0e’, ‘#2ca02c’, ‘#d62728’,
‘#9467bd’, ‘#8c564b’, ‘#e377c2’, ‘#7f7f7f’, ‘#bcbd22’, ‘#17becf’])));
the indexing is intended to occur at rendering time, and defaults to
black if the cycle does not include color. one of {‘tab:blue’,
‘tab:orange’, ‘tab:green’, ‘tab:red’, ‘tab:purple’, ‘tab:brown’,
‘tab:pink’, ‘tab:gray’, ‘tab:olive’, ‘tab:cyan’} which are the Tableau
Colors from the ‘tab10’ categorical palette (which is the default
color cycle);

補(bǔ)充3

RGB分為浮點(diǎn)數(shù)和整數(shù)兩種,在Matplotlib中,使用的是浮點(diǎn)數(shù),即范圍在[0,1],而整數(shù)則是[0,255],若是要轉(zhuǎn)化,將整數(shù)除以255即浮點(diǎn)數(shù)形式。

總結(jié)

到此這篇關(guān)于python matplotlib庫(kù)繪圖實(shí)戰(zhàn)之繪制散點(diǎn)圖的文章就介紹到這了,更多相關(guān)python matplotlib繪制散點(diǎn)圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論