Python中Matplotlib的點、線形狀、顏色以及繪制散點圖
我們在Python中經(jīng)常使用會用到matplotlib畫圖,有些曲線和點的形狀、顏色信息長時間不用就忘了,整理一下便于查找。
安裝matplotlib后可以查看官方說明(太長不貼出來了)
from matplotlib import pyplot as plt help(plt.plot)
常用顏色:
'b' 藍色
'g' 綠色
'r' 紅色
'c' 青色
'm' 品紅
'y' 黃色
'k' 黑色
'w' 白色
更多顏色:
plt.plot(x, y, marker='+', color='coral')

常用標(biāo)記點形狀:
‘.’:點(point marker)
‘,’:像素點(pixel marker)
‘o’:圓形(circle marker)
‘v’:朝下三角形(triangle_down marker)
‘^’:朝上三角形(triangle_up marker)
‘<‘:朝左三角形(triangle_left marker)
‘>’:朝右三角形(triangle_right marker)
‘1’:(tri_down marker)
‘2’:(tri_up marker)
‘3’:(tri_left marker)
‘4’:(tri_right marker)
‘s’:正方形(square marker)
‘p’:五邊星(pentagon marker)
‘*’:星型(star marker)
‘h’:1號六角形(hexagon1 marker)
‘H’:2號六角形(hexagon2 marker)
‘+’:+號標(biāo)記(plus marker)
‘x’:x號標(biāo)記(x marker)
‘D’:菱形(diamond marker)
‘d’:小型菱形(thin_diamond marker)
‘|’:垂直線形(vline marker)
‘_’:水平線形(hline marker)

常用線形:
‘-‘:實線(solid line style)
‘–‘:虛線(dashed line style)
‘-.’:點劃線(dash-dot line style)
‘:’:點線(dotted line style)
繪制散點圖
在matplotlib中使用函數(shù) matplotlib.pyplot.scatter 繪制散點圖,matplotlib.pyplot.scatter的函數(shù)細(xì)節(jié):
matplotlib.pyplot.scatter(x, y, s=None, c=None, marker=None, cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, verts=None, edgecolors=None, hold=None, data=None, **kwargs) x,y組成了散點的坐標(biāo);s為散點的面積;c為散點的顏色(默認(rèn)為藍色'b');marker為散點的標(biāo)記;alpha為散點的透明度(0與1之間的數(shù),0為完全透明,1為完全不透明);linewidths為散點邊緣的線寬;如果marker為None,則使用verts的值構(gòu)建散點標(biāo)記;edgecolors為散點邊緣顏色。
import matplotlib import matplotlib.pyplot as plt import numpy as np # 保證圖片在瀏覽器內(nèi)正常顯示 %matplotlib inline # 10個點 N = 10 x = np.random.rand(N) y = np.random.rand(N) plt.scatter(x, y) plt.show()
補充:Python散點圖教程
調(diào)整散點大小
N = 10 x = np.random.rand(N) y = np.random.rand(N) area = np.random.rand(N) * 1000 # 包含10個均勻分布的隨機值的面積數(shù)組,大小[0, 1000] fig = plt.figure() ax = plt.subplot() ax.scatter(x, y, s=area, alpha=0.5) # 繪制散點圖,面積隨機 plt.show()
調(diào)整散點顏色
N = 10 x = np.random.rand(N) y = np.random.rand(N) x2 = np.random.rand(N) y2 = np.random.rand(N) area = np.random.rand(N) * 1000 fig = plt.figure() ax = plt.subplot() ax.scatter(x, y, s=area, alpha=0.5) ax.scatter(x2, y2, s=area, c='green', alpha=0.6) # 改變顏色 plt.show()
調(diào)整散點形狀
N = 10 x = np.random.rand(N) y = np.random.rand(N) x2 = np.random.rand(N) y2 = np.random.rand(N) x3 = np.random.rand(N) y3 = np.random.rand(N) area = np.random.rand(N) * 1000 fig = plt.figure() ax = plt.subplot() ax.scatter(x, y, s=area, alpha=0.5) ax.scatter(x2, y2, s=area, c='green', alpha=0.6) ax.scatter(x3, y3, s=area, c=area, marker='v', cmap='Reds', alpha=0.7) # 更換標(biāo)記樣式,另一種顏色的樣式 plt.show()
總結(jié)
到此這篇關(guān)于Python中Matplotlib的點、線形狀、顏色以及繪制散點圖的文章就介紹到這了,更多相關(guān)Python Matplotlib繪制散點圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Windows下Anaconda和PyCharm的安裝與使用詳解
這篇文章主要介紹了Windows下Anaconda和PyCharm的安裝與使用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
Playwright元素截圖并保存至allure的實現(xiàn)示例
在UI自動化測試中,我們經(jīng)常需要獲取屏幕截圖,本文就介紹一下Playwright元素截圖并保存至allure的實現(xiàn)示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12
ruff check文件目錄檢測--exclude參數(shù)設(shè)置路徑詳解
這篇文章主要為大家介紹了ruff check文件目錄檢測exclude參數(shù)如何設(shè)置多少路徑詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10
python實戰(zhàn)之利用pygame實現(xiàn)貪吃蛇游戲(二)
這篇文章主要介紹了python實戰(zhàn)之利用pygame實現(xiàn)貪吃蛇游戲(二),文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)python的小伙伴們有很好的幫助,需要的朋友可以參考下2021-05-05
使用Python-OpenCV消除圖像中孤立的小區(qū)域操作
這篇文章主要介紹了使用Python-OpenCV消除圖像中孤立的小區(qū)域操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07

