網(wǎng)站首頁 編程語言 正文
一.導入庫
import matplotlib.pyplot as plt
二.設(shè)置文字
plt.title("double number", fontsize=24)
plt.xlabel("number", fontsize=14)
plt.ylabel("double", fontsize=14)
三.設(shè)置坐標軸參數(shù)
plt.axis([0, 15, 0, 30])
q前面兩個數(shù)為x軸的始末,后面則為y
四.繪制點
plt.scatter(2, 4, s=20) #s為點的大小
plt.show()
得到這樣子的圖
畢竟繪圖時我們不可能只畫一個點
所以還是應(yīng)該引入數(shù)組來解決問題
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
將原來的2,4分別替換為x,y
為了在數(shù)據(jù)量較大時,簡化(偷懶)代碼,可以使用range等函數(shù)或者列表解析的方法,這里就不贅述了,詳見補充1
五.對點的繼續(xù)處理
1.自定義顏色
plt.scatter(x, y, c='red', edgecolors='none', s=20)
用參數(shù)c設(shè)置點的顏色,用edgecolor設(shè)置邊緣的顏色(在較新的matpoltlib版本中,edgecolors默認為none)有關(guān)顏色的詳細資料,可見補充2
ps:將c設(shè)置為green,edgecolors設(shè)置為black,將引起極度生理不適,特別是點極度密集的曲線中,你懂的
言歸正傳,python的顏色設(shè)置通常為RGB,所以在scatter內(nèi)也可以用RGB參數(shù)來設(shè)置顏色,格式如下:
plt.scatter(x, y, c=(0, 0, 0.6), edgecolors='none', s=20)
RGB小數(shù)模式詳見補充3
2.顏色映射
plt.scatter(x, y, c=y, edgecolors='none', cmap=plt.cm.Blues, s=20)
通過使用cmap,將c設(shè)置為一個數(shù)組,則可以達到顏色漸變的效果:
事實上并非所有的顏色都可以使用,僅有RGB三原色和Oranges等少數(shù)顏色可以…感覺官方文檔上說的十個好像不太行,格式必須為頭文字大寫結(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,接下來為補充部分
補充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]
其實就是把for循環(huán)寫到了里面去
補充2
Matplotlib識別以下格式以指定顏色:
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’);
速記十六進制RGB或RGBA字符串,相當于通過復制每個字符獲得的十六進制RGB或RGBA字符串(例如,’#abc’,相當于’#aabbcc’,或’#abcd’,相當于’#aabbccdd’);
a string representation of a float value in [0, 1] inclusive for gray
level (e.g., ‘0.5’);
單個字母字符串,即{‘b’,‘g’,‘r’,‘c’,‘m’,‘y’,‘k’,‘w’}之一,它們是藍色、綠色、紅色、青色、品紅色、黃色、黑色和白色陰影的速記號;
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);
補充3
RGB分為浮點數(shù)和整數(shù)兩種,在Matplotlib中,使用的是浮點數(shù),即范圍在[0,1],而整數(shù)則是[0,255],若是要轉(zhuǎn)化,將整數(shù)除以255即浮點數(shù)形式。
總結(jié)
原文鏈接:https://blog.csdn.net/weixin_50187215/article/details/113541848
相關(guān)推薦
- 2022-06-19 介紹C語言程序中的注釋等輔助語句如何使用_C 語言
- 2022-06-22 使用Git?Bash向GitHub上傳本地項目_其它綜合
- 2022-12-11 React?state狀態(tài)屬性用法講解_React
- 2022-12-10 Flutter?狀態(tài)管理scoped?model源碼解讀_Android
- 2022-08-04 react項目優(yōu)化配置的操作詳解_React
- 2022-07-17 關(guān)于elasticsearch連接時斷時續(xù)以及Kibana出現(xiàn)server is not ready
- 2022-10-14 SpringCloud 微服務(wù)與遠程調(diào)用測試
- 2022-06-07 使用kubeadm部署多節(jié)點集群_云其它
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支