專欄導讀
? 作者簡介:i阿極,CSDN Python領域新星創(chuàng)作者,專注于分享python領域知識。
? 本文錄入于《數(shù)據(jù)分析之道》,本專欄針對大學生、初級數(shù)據(jù)分析工程師精心打造,對python基礎知識點逐一擊破,不斷學習,提升自我。
? 訂閱后,可以閱讀《數(shù)據(jù)分析之道》中全部文章內(nèi)容,包含python基礎語法、數(shù)據(jù)結(jié)構(gòu)和文件操作,科學計算,實現(xiàn)文件內(nèi)容操作,實現(xiàn)數(shù)據(jù)可視化等等。
? 其他專欄:《數(shù)據(jù)分析案例》 ,《機器學習案例》??????如果覺得文章不錯或能幫助到你學習,可以點贊??收藏??評論??+關(guān)注哦!??????
??????如果有小伙伴需要數(shù)據(jù)集和學習交流,文章下方有交流學習區(qū)!一起學習進步!??
1、前言
Matplotlib 是一個強大的 Python 繪圖庫,它提供了多種繪圖工具和樣式。在 Matplotlib 中,我們可以使用標記來標識數(shù)據(jù)點、線條和其他形狀,以便更好地展示圖形。
Matplotlib 支持多種標記,包括圓圈、方形、三角形等形狀,每種標記都可以自定義顏色和大小。在下面的實例中,我們將演示如何使用 Matplotlib 繪制散點圖,并使用不同的標記和顏色。
2、標記(Markers)
2.1關(guān)鍵詞參數(shù)marker
可以使用關(guān)鍵字參數(shù)marker用指定的標記強調(diào)每個點。
用圓圈標記每個點:
import matplotlib.pyplot as plt
import numpy as np
# 創(chuàng)建數(shù)據(jù)
x = np.linspace(0, 10)
y = np.cos(x)
# 繪制折線圖并設置標記樣式
plt.plot(x,y,marker = 'o')
# 顯示圖形
plt.show()
用星號標記每個點:
import matplotlib.pyplot as plt
import numpy as np
# 創(chuàng)建數(shù)據(jù)
x = np.linspace(0, 10)
y = np.cos(x)
# 繪制折線圖并設置標記樣式
plt.plot(x,y,marker = '*')
# 顯示圖形
plt.show()
2.2標記參考(Marker Reference)
可以選擇以下任一標記:
標記 | 描述 |
---|---|
‘o’ | Circle |
‘*’ | Star |
‘.’ | Point |
‘,’ | Pixel |
‘x’ | X |
‘X’ | X (filled) |
‘+’ | Plus |
‘P’ | Plus (filled) |
‘s’ | Square |
‘D’ | Diamond |
‘d’ | Diamond (thin) |
‘p’ | Pentagon |
‘H’ | Hexagon |
‘h’ | Hexagon |
‘v’ | Triangle Down |
‘^’ | Triangle Up |
‘<’ | Triangle Left |
‘>’ | Triangle Right |
‘1’ | Tri Down |
‘2’ | Tri Up |
‘3’ | Tri Left |
‘4’ | Tri Right |
‘l’ | Vline |
‘_’ | Hline |
3、Format Strings fmt
3.1fmt參數(shù)
fmt 參數(shù)定義了基本格式,如標記、線條樣式和顏色。
fmt = '[marker][line][color]'
例如 o:r,o 表示實心圓標記,: 表示虛線,r 表示顏色為紅色。
import matplotlib.pyplot as plt
import numpy as np
# 創(chuàng)建數(shù)據(jù)
x = np.linspace(0, 10)
y = np.cos(x)
# 繪制折線圖并設置標記樣式
plt.plot(x,y,'o:r')
# 顯示圖形
plt.show()
例如 o-r, o表示實心圓標記,-表示虛線,r表示顏色為紅色。
import matplotlib.pyplot as plt
import numpy as np
# 創(chuàng)建數(shù)據(jù)
x = np.linspace(0, 10)
y = np.cos(x)
# 繪制折線圖并設置標記樣式
plt.plot(x,y,'o:r')
# 顯示圖形
plt.show()
3.2線參考(Line Reference)
線語法 | 描述 |
---|---|
‘-’ | 實線 |
‘:’ | 虛線 |
‘–’ | 破折線 |
‘-.’ | 點劃線 |
如果在fmt參數(shù)中省略了線值,則不會繪制任何線。
4、標記顏色(Marker Color)
4.1關(guān)鍵字參數(shù)mec
可以使用關(guān)鍵字參數(shù)markeredgecolor或更短的mec設置標記邊框的顏色。
將邊框顏色設置為紅色:
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y, marker='o',ms = 20,mec='r')
4.2關(guān)鍵字參數(shù)mfc
可以使用關(guān)鍵字參數(shù)markerfacecolor或更短的mfc設置標記邊緣內(nèi)的顏色。
將內(nèi)部顏色標記為紅色:
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y, marker='o',ms = 20,mfc='r')
4.3mfc和mec結(jié)合
自定義標記內(nèi)部與邊框的顏色:
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y, marker='o',ms = 20,mec='r',mfc='r')
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y, marker='o',ms = 20,mec='r',mfc='w')
用美麗的綠色標記每個點:
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y, marker='o',ms = 20,mec='g',mfc='g')
4.4顏色參考(Color Reference)
顏色語法 | 描述 |
---|---|
‘r’ | Red |
‘g’ | Green |
‘b’ | Blue |
‘c’ | Cyan |
‘m’ | Magenta |
‘y’ | Yellow |
‘k’ | Black |
‘w’ | White |
5、標記大小(Marker Size)
可以使用關(guān)鍵字參數(shù)markersize或更短的版本ms來設置標記的大小。
將標記的大小設置為20:
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y, marker='o',ms = 20)
文章來源:http://www.zghlxwxcb.cn/news/detail-435056.html
??文章下方有交流學習區(qū)!一起學習進步!??????
??首發(fā)CSDN博客,創(chuàng)作不易,如果覺得文章不錯,可以點贊??收藏??評論??
??你的支持和鼓勵是我創(chuàng)作的動力???文章來源地址http://www.zghlxwxcb.cn/news/detail-435056.html
到了這里,關(guān)于【數(shù)據(jù)分析之道-Matplotlib(二)】Matplotlib 繪圖標記的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!