網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
python?matplotlib庫(kù)繪圖實(shí)戰(zhàn)之繪制散點(diǎn)圖_python
作者:不見(jiàn)月光見(jiàn)星光 ? 更新時(shí)間: 2022-09-04 編程語(yǔ)言一.導(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ù)組來(lái)解決問(wèn)題
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
將原來(lái)的2,4分別替換為x,y
為了在數(shù)據(jù)量較大時(shí),簡(jiǎn)化(偷懶)代碼,可以使用range等函數(shù)或者列表解析的方法,這里就不贅述了,詳見(jiàn)補(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ì)資料,可見(jiàn)補(bǔ)充2
ps:將c設(shè)置為green,edgecolors設(shè)置為black,將引起極度生理不適,特別是點(diǎn)極度密集的曲線中,你懂的
言歸正傳,python的顏色設(shè)置通常為RGB,所以在scatter內(nèi)也可以用RGB參數(shù)來(lái)設(shè)置顏色,格式如下:
plt.scatter(x, y, c=(0, 0, 0.6), edgecolors='none', s=20)
RGB小數(shù)模式詳見(jiàn)補(bǔ)充3
2.顏色映射
plt.scatter(x, y, c=y, edgecolors='none', cmap=plt.cm.Blues, s=20)
通過(guò)使用cmap,將c設(shè)置為一個(gè)數(shù)組,則可以達(dá)到顏色漸變的效果:
事實(shí)上并非所有的顏色都可以使用,僅有RGB三原色和Oranges等少數(shù)顏色可以…感覺(jué)官方文檔上說(shuō)的十個(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,接下來(lái)為補(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)于通過(guò)復(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é)
原文鏈接:https://blog.csdn.net/weixin_50187215/article/details/113541848
相關(guān)推薦
- 2023-06-13 python調(diào)試過(guò)程中多顏色輸出方式_python
- 2022-05-11 SpringBoot整合RabbitMq與高級(jí)特性
- 2023-01-21 flask中響應(yīng)錯(cuò)誤的處理及errorhandler的應(yīng)用方式_python
- 2023-04-09 python?密碼加密與解密的實(shí)現(xiàn)_python
- 2024-03-04 新版ECharts實(shí)現(xiàn)“暫無(wú)數(shù)據(jù)”的完美解決方案
- 2024-02-29 UNI-APP開(kāi)發(fā)之插件安裝失敗,離線安裝
- 2023-02-14 C#實(shí)現(xiàn)ComboBox變色的示例代碼_C#教程
- 2022-07-12 windows11下的dockerDesktop4.8.2資源目錄掛載
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支