Matplotlib是一個Python工具箱,用於科學計算的數據視覺化。借助它,Python可以繪製如Matlab和Octave多種多樣的資料圖形。以下這篇文章主要介紹了python使用matplotlib如何繪製折線圖的方法教程,需要的朋友可以參考借鑒。
matplotlib簡介
matplotlib 是python最著名的繪圖庫,它提供了一整套和matlab相似的命令API,十分適合交互式地行製圖。而且也可以方便地將它作為繪圖控件,嵌入GUI應用程式中。
它的文件相當完備,並且Gallery頁面中有上百幅縮圖,開啟之後都有原始程式。因此如果你需要繪製某種類型的圖,只需要在這個頁面中瀏覽/複製/貼上一下,基本上都能搞定。
在Linux下比較著名的資料圖工具還有gnuplot,這個是免費的,Python有一個套件可以呼叫gnuplot,但是語法比較不習慣,而且畫圖品質不高。
而 Matplotlib則比較強:Matlab的語法、python語言、latex的畫圖品質(還可以使用內嵌的latex引擎繪製的數學公式)。
繪圖庫Matplotlib的安裝方法:點這裡
matplotlib畫出折線圖
2. 圖例
在plot的時候指定label,然後呼叫legend方法可以繪製圖例。例如:
import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 2 * np.pi, 100) y1, y2 = np.sin(x), np.cos(x) plt.plot(x, y1) plt.plot(x, y2) plt.title('line chart') plt.xlabel('x') plt.ylabel('y') plt.show()
legend方法可接受一個loc關鍵字參數來設定圖例的位置,可取值為數字或字串:
'upper right'
2: 'upper left' 3: 'lower left'
6: 'center left'
7: 'center right'
8: 'lower center'
3. 線的樣式
(1)顏色
plot方法的關鍵字參數color(或c)用來設定線的顏色。可取值為:
m: magenta
y: yellow
k: black
w: white2、#rrggbb
3、(r, g, b, br. ]之間
4、[0, 1]之間的浮點數的字串形式,表示灰階值。 0表示黑色,1表示白色
(2)樣式
plot方法的關鍵字參數linestyle(或ls)用來設定線的樣式。可取值為:
-, solid
-., dashdot
(3)粗細
設定plot方法的關鍵字參數linewidth(或lw)可以改變線的粗細,其值為浮點數。
import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 2 * np.pi, 100) y1, y2 = np.sin(x), np.cos(x) plt.plot(x, y1, label='y = sin(x)') plt.plot(x, y2, label='y = cos(x)') plt.legend() plt.show()
4. marker
- 以下關鍵字參數可以用來設定marker的樣式: markeredgewidth 或mew
- markerfacecolor 或mfc
- markerfacecoloralt 或mfcalt
- markersize 或ms
- : markersize 或ms
'v': triangle_down marker
'^': triangle_le_le '>': 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': hexagon1 marker 'H': hexagon2 marker '+': plus marker 'x': x marker 'D': diamond marker 'd': thin_diamond marker '|': vline marker '_': hline marker 例如: 另外,marker关键字参数可以和color以及linestyle这两个关键字参数合并为一个字符串。例如: 更多python使用matplotlib繪製折線圖教程相关文章请关注PHP中文网!import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 2 * np.pi, 10)
y1, y2 = np.sin(x), np.cos(x)
plt.plot(x, y1, marker='o', mec='r', mfc='w')
plt.plot(x, y2, marker='*', ms=10)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 2 * np.pi, 10)
y1, y2 = np.sin(x), np.cos(x)
plt.plot(x, y1, 'ro-')
plt.plot(x, y2, 'g*:', ms=10)
plt.show()

Python腳本在Unix系統上無法運行的原因包括:1)權限不足,使用chmod xyour_script.py賦予執行權限;2)Shebang行錯誤或缺失,應使用#!/usr/bin/envpython;3)環境變量設置不當,可打印os.environ調試;4)使用錯誤的Python版本,可在Shebang行或命令行指定版本;5)依賴問題,使用虛擬環境隔離依賴;6)語法錯誤,使用python-mpy_compileyour_script.py檢測。

使用Python數組比列表更適合處理大量數值數據。 1)數組更節省內存,2)數組對數值運算更快,3)數組強制類型一致性,4)數組與C語言數組兼容,但在靈活性和便捷性上不如列表。

列表列表更好的forflexibility andmixDatatatypes,何時出色的Sumerical Computitation sand larged數據集。 1)不可使用的列表xbilese xibility xibility xibility xibility xibility xibility xibility xibility xibility xibility xibles and comply offrequent elementChanges.2)

numpymanagesmemoryforlargearraysefefticefticefipedlyuseviews,副本和內存模擬文件.1)viewsAllowSinglicingWithOutCopying,直接modifytheoriginalArray.2)copiesCanbecopy canbecreatedwitheDedwithTheceDwithThecevithThece()methodervingdata.3)metservingdata.3)memore memore-mappingfileShessandAstaStaStstbassbassbassbassbassbassbassbassbassbassbb

Listsinpythondonotrequireimportingamodule,helilearraysfomthearraymoduledoneedanimport.1)列表列表,列表,多功能和canholdMixedDatatatepes.2)arraysaremoremoremoremoremoremoremoremoremoremoremoremoremoremoremoremoremeremeremeremericdatabuteffeftlessdatabutlessdatabutlessfiblesible suriplyElsilesteletselementEltecteSemeTemeSemeSemeSemeTypysemeTypysemeTysemeTypysemeTypepe。

pythonlistscanStoryDatatepe,ArrayModulearRaysStoreOneType,and numpyArraySareSareAraysareSareAraysareSareComputations.1)列出sareversArversAtileButlessMemory-Felide.2)arraymoduleareareMogeMogeNareSaremogeNormogeNoreSoustAta.3)

WhenyouattempttostoreavalueofthewrongdatatypeinaPythonarray,you'llencounteraTypeError.Thisisduetothearraymodule'sstricttypeenforcement,whichrequiresallelementstobeofthesametypeasspecifiedbythetypecode.Forperformancereasons,arraysaremoreefficientthanl

pythonlistsarepartofthestAndArdLibrary,herilearRaysarenot.listsarebuilt-In,多功能,和Rused ForStoringCollections,而EasaraySaraySaraySaraysaraySaraySaraysaraySaraysarrayModuleandleandleandlesscommonlyusedDduetolimitedFunctionalityFunctionalityFunctionality。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

記事本++7.3.1
好用且免費的程式碼編輯器