Python?matplotlib之折線圖的各種樣式與畫法總結(jié)
上述圖的完整代碼如下:
from numpy import * import numpy as np import pandas as pd import matplotlib.pyplot as plt import pylab as pl from mpl_toolkits.axes_grid1.inset_locator import inset_axes y1 = [0.92787363, 0.92436059, 0.92380563, 0.92169979, 0.92215771] y2 = [0.87410227, 0.85341922, 0.84874221, 0.83406411, 0.83610876] y3 = [0.77410227, 0.75341922, 0.74874221, 0.73406411, 0.73610876] y4 = [0.67410227, 0.65341922, 0.64874221, 0.63406411, 0.63610876] y5 = [0.57410227, 0.55341922, 0.54874221, 0.53406411, 0.53610876] y6 = [0.47410227, 0.45341922, 0.44874221, 0.43406411, 0.43610876] y7 = [0.37410227, 0.35341922, 0.34874221, 0.33406411, 0.33610876] y8 = [0.27410227, 0.25341922, 0.24874221, 0.23406411, 0.23610876] y9 = [0.17410227, 0.15341922, 0.14874221, 0.13406411, 0.13610876] y10 = [0.07410227, 0.05341922, 0.04874221, 0.03406411, 0.03610876] y11 = [-0.07410227, -0.05341922, -0.04874221, -0.03406411, -0.03610876] y12 = [-0.17410227, -0.15341922, -0.14874221, -0.13406411, -0.13610876] x = ['1','2','3','4','5'] plt.figure() #figsize是圖片的大小`fig = plt.figure(figsize = (7,5)) ax=plt.axes() # plt.grid(zorder=0, linewidth = "0.5", linestyle = "-.") #顯示網(wǎng)格,zorder控制網(wǎng)格顯示的前后, color='#738CBC' , color='#DFD478' ax.plot(x, y1, marker='s', linestyle = '-.', lw=2, label='q=1') #‘s' : 方塊狀 ax.plot(x, y2, marker='o', linestyle = '-.', lw=2, label='q=2') #o' : 實(shí)心圓 ax.plot(x, y3, marker='^', linestyle = 'dotted', lw=2, label='q=3') #‘^' : 正三角形 ax.plot(x, y4, marker='v', linestyle = 'dotted', lw=2, label='q=4') #‘v' : 反正三角形 ax.plot(x, y5, marker='+', linestyle = 'dotted', lw=2, label='q=5') #‘+' : 加號(hào) ax.plot(x, y6, marker='*', linestyle = 'dotted', lw=2, label='q=6') #‘*' : 星號(hào) ax.plot(x, y7, marker=',', linestyle = 'dotted', lw=2, label='q=7') #‘,':點(diǎn) ax.plot(x, y8, marker='x', linestyle = ':', lw=2, label='q=8') #‘x' : x號(hào) ax.plot(x, y9, marker='p', linestyle = 'dashed', lw=2, label='q=9') #‘p' : 五角星 ax.plot(x, y10, marker='1', linestyle = 'dotted', lw=2, label='q=10') #‘1' : 三腳架標(biāo)記 ax.plot(x, y11, marker='2', linestyle = 'dotted', lw=2, label='q=11') #‘2' : 三腳架標(biāo)記 ax.plot(x, y12, marker='o', linestyle = '-', lw=2, label='q=12', markerfacecolor='white') #空心圓 plt.legend(bbox_to_anchor=(1.20, 1), loc=1, borderaxespad=0) #顯示標(biāo)簽,并放在外側(cè) plt.xlabel('p',fontsize=20) #設(shè)置y軸的標(biāo)簽 plt.ylabel('values',fontsize=20) #設(shè)置y軸的標(biāo)簽 plt.savefig("values",dpi=500, bbox_inches='tight') # 保存圖片
1. 折線形狀
- 直線: linestyle = ‘-’ 或 省略
- 破折線: linestyle = ‘–’ 或 linestyle = ‘dashed’
- 點(diǎn)劃線: linestyle = ‘-.’ 或 linestyle = ‘dashdot’
- 虛線: linestyle = ‘:’ 或 linestyle = ‘dotted’
2. 數(shù)據(jù)點(diǎn)形狀
- ‘s’ : 方塊狀
- ‘o’ : 實(shí)心圓
- ‘^’ : 正三角形
- ‘v’ : 反正三角形
- ‘+’ : 加號(hào)
- ‘*’ : 星號(hào)
- ‘,’:點(diǎn)
- ‘x’ : x號(hào)
- ‘p’ : 五角星
- ‘1’ : 三腳架標(biāo)記
- ‘2’ : 三腳架標(biāo)記
若是想設(shè)置空心圓則使用如下參數(shù):
markeredgecolor # 圓邊緣的顏色
markeredgewidth # 圓的線寬
ax.plot(x, y12, marker='o', linestyle = '-', lw=2, label='q=12', markerfacecolor='white') #空心圓
3. 折線顏色
若是想修改折線的顏色,使用color:表示的折線的顏色,如:red:紅色,blue:藍(lán)色等;
y1 = [0.92787363, 0.92436059, 0.92380563, 0.92169979, 0.92215771] x = ['1','2','3','4','5'] plt.figure(figsize = (13,10)) # 設(shè)置畫布 大小為13*10 ax=plt.axes() plt.grid(zorder=0, linewidth = "0.5", linestyle = "-.") #顯示網(wǎng)格,zorder控制網(wǎng)格顯示的前后 ax.plot(x, y1, marker='o', ls='-', lw=2, label='q=1', color='#738CBC') plt.legend() #顯示標(biāo)簽 plt.xlabel('p',fontsize=20) plt.ylabel('ACC',fontsize=20)
4. 添加網(wǎng)格
其中,plt.grid(zorder=0, linewidth = "0.5", linestyle = "-.") #顯示網(wǎng)格,zorder控制網(wǎng)格顯示的前后
是顯示網(wǎng)格的語句。
- 添加網(wǎng)格: plt.grid()
- 只顯示垂直網(wǎng)格線: plt.grid(axis=‘x’)
- 只顯示水平網(wǎng)格線: plt.grid(axis=‘y’)
- 網(wǎng)格樣式: linestyle=‘-’ 和 折線樣式相同常用這四種:
- 直線: linestyle = ‘-’ 或 省略
- 破折線: linestyle = ‘–’ 或 linestyle = ‘dashed’
- 點(diǎn)劃線: linestyle = ‘-.’ 或 linestyle = ‘dashdot’
- 虛線: linestyle = ‘:’ 或 linestyle = ‘dotted’
- 網(wǎng)格線寬度:linewidth=num
上述圖的完整代碼如下:
from matplotlib import pyplot as plt # 添加網(wǎng)格 # 選擇字體顯示中文 plt.rcParams['font.family'] = ['Microsoft YaHei'] # 定義坐標(biāo)數(shù)據(jù) y = [0.92787363, 0.92436059, 0.92380563, 0.92169979, 0.92215771] x = ['1','2','3','4','5'] # 第一張表 plt.subplot(2, 2, 1) # 傳入數(shù)據(jù) plt.plot(x, y, color='#738CBC', marker='o') # 添加標(biāo)題 plt.title("第一張表,加坐標(biāo)和顏色", color='blue', fontsize=10) # 添加網(wǎng)格 plt.grid() # 第二張表 plt.subplot(2, 2, 2) plt.plot(x, y) plt.title("垂直網(wǎng)格線,1寬", color='blue', fontsize=10) plt.grid(axis='x', linewidth=1) # 第三張表 plt.subplot(2, 2, 3) plt.plot(x, y) plt.title("水平網(wǎng)格線,破折線,0.5寬", color='blue', fontsize=10) plt.grid(axis='y', linestyle='--', linewidth=0.5) # 第四張表 plt.subplot(2, 2, 4) plt.plot(x, y) plt.title("藍(lán)色虛線網(wǎng)格線", color='blue', fontsize=10) plt.grid(color='b', linestyle=':') # 總標(biāo)題 plt.suptitle("網(wǎng)格對(duì)比", color='blue', fontsize=15) plt.tight_layout() # 自動(dòng)調(diào)整子圖參數(shù),使之自動(dòng)填充整個(gè)圖像區(qū)域 # 繪圖 plt.show()
總結(jié)
到此這篇關(guān)于Python matplotlib之折線圖的各種樣式與畫法的文章就介紹到這了,更多相關(guān)matplotlib折線圖畫法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python openCV實(shí)現(xiàn)攝像頭獲取人臉圖片
這篇文章主要為大家詳細(xì)介紹了python openCV實(shí)現(xiàn)攝像頭獲取人臉圖片,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08python解決OpenCV在讀取顯示圖片的時(shí)候閃退的問題
這篇文章主要介紹了python解決OpenCV在讀取顯示圖片的時(shí)候閃退的問題,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02用實(shí)例詳解Python中的Django框架中prefetch_related()函數(shù)對(duì)數(shù)據(jù)庫查詢的優(yōu)化
這篇文章主要介紹了用實(shí)例詳解Python中的Django框架中prefetch_related()函數(shù)對(duì)數(shù)據(jù)庫查詢的優(yōu)化,可減少對(duì)數(shù)據(jù)庫的查詢次數(shù)從而優(yōu)化性能,需要的朋友可以參考下2015-04-04Python wxpython模塊響應(yīng)鼠標(biāo)拖動(dòng)事件操作示例
這篇文章主要介紹了Python wxpython模塊響應(yīng)鼠標(biāo)拖動(dòng)事件操作,結(jié)合實(shí)例形式分析了Python使用wxpython模塊創(chuàng)建窗口、綁定事件及相應(yīng)鼠標(biāo)事件相關(guān)操作技巧,需要的朋友可以參考下2018-08-08Python繪制多因子柱狀圖的實(shí)現(xiàn)示例
本文主要介紹了Python繪制多因子柱狀圖的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05Django中自定義admin Xadmin的實(shí)現(xiàn)代碼
這篇文章主要介紹了Django中自定義admin---Xadmin的實(shí)現(xiàn)代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-08-08python 實(shí)現(xiàn)判斷ip連通性的方法總結(jié)
下面小編就為大家分享一篇python 實(shí)現(xiàn)判斷ip連通性的方法總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-04-04