1. 程式人生 > >Python3繪圖庫Matplotlib(02)

Python3繪圖庫Matplotlib(02)

marker isp hex height middle int mark RR err

控制顏色

技術分享圖片
Color Color Name
b blue
c cyan
g green
k black
m magenta
r red
w white
y yellow
plt.plot(x1, y1, fmt1, x2, y2, fmt2, ...)

控制線的風格

技術分享圖片
Style Style
- solid line
-- dashed line
-. dash-dot line
: dotted line

控制標記樣式

技術分享圖片
. Point marker
, Pixel marker
o Circle marker
v Triangle down
^ Triangle up marker
< Triangle left marker
> Triangle right marker
1 Tripod down marker
2 Tripod up marker
3 Tripod left marker
4 Tripod right marker
s Square marker
p Pentagon marker
* Star marker
h Hexagon marker
H Rotated hexagon marker
+ Plus marker
x Cross marker
D Diamond marker
d Thin diamond marker
| Vertical line
_ Horizontal line
技術分享圖片

用關鍵字參數進行更好的控制

技術分享圖片

處理X和Y的ticks標簽值

技術分享圖片

畫圖的類型

技術分享圖片

直方圖圖表 = Histogram charts

技術分享圖片

Error bar charts

技術分享圖片 技術分享圖片

Bar Charts

技術分享圖片 技術分享圖片

本小結代碼示例

import matplotlib.pyplot as plt
import numpy as np
y = np.arange(1, 3)
plt.plot(y, ‘y‘)
plt.plot(y+1, ‘m‘)
plt.plot(y+2, ‘c‘)
plt.show()

import matplotlib.pyplot as plt
import numpy as np
y = np.arange(1, 3)
plt.plot(y, ‘--‘, y+1, ‘-.‘, y+2, ‘:‘)
plt.show()

import matplotlib.pyplot as plt
import numpy as np
y = np.arange(1, 3, 0.2)
plt.plot(y, ‘x‘, y+0.5, ‘o‘, y+1, ‘D‘, y+1.5, ‘^‘, y+2, ‘s‘)
plt.show()

import matplotlib.pyplot as plt
import numpy as np
y = np.arange(1, 3, 0.3)
plt.plot(y, ‘cx--‘, y+1, ‘mo:‘, y+2, ‘kp-.‘)
plt.show()

import matplotlib.pyplot as plt
import numpy as np
y = np.arange(1, 3, 0.3)
plt.plot(y, color=‘blue‘, linestyle=‘dashdot‘, linewidth=4, 
         marker=‘o‘, markerfacecolor=‘red‘, markeredgecolor=‘black‘,
        markeredgewidth=3, markersize=12)
plt.show()

import matplotlib.pyplot as plt
x = [5, 3, 7, 2, 4, 1]
plt.plot(x)
plt.xticks(range(len(x)), [‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘])
plt.yticks(range(1, 8, 2))
plt.show()

import matplotlib.pyplot as plt
import numpy as np
y = np.random.randn(1000)
plt.hist(y)
plt.show()
plt.hist(y, 25)
plt.show()

import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 4, 0.2)
y = np.exp(-x)
e1 = 0.1 * np.abs(np.random.randn(len(y)))
plt.errorbar(x, y, yerr=e1, fmt=‘.-‘)
plt.show()
e2 = 0.1 * np.abs(np.random.randn(len(y)))
plt.errorbar(x, y, yerr=e1, xerr=e2, fmt=‘.-‘, capsize=0)
plt.show()
plt.errorbar(x, y, yerr=[e1, e2], fmt=‘.-‘)
plt.show()

import matplotlib.pyplot as plt
plt.bar([1, 2, 3], [3, 2, 5])
plt.show()

import matplotlib.pyplot as plt
import numpy as np
data1 = 10*np.random.rand(5)
data2 = 10*np.random.rand(5)
data3 = 10*np.random.rand(5)
e2 = 0.5*np.abs(np.random.randn(len(data2)))
locs = np.arange(1, len(data1)+1)
width = 0.27
plt.bar(locs+width, data2, yerr=e2, width=width, color=‘red‘)
plt.bar(locs+2*width, data3, width=width, color=‘green‘)
plt.show()

知識在於點點滴滴的積累,我會在這個路上Go ahead,

有幸看到我博客的朋友們,若能學到知識,請多多關註以及討論,讓我們共同進步,揚帆起航。

後記:打油詩一首

適度鍛煉,量化指標

考量天氣,設定目標

科學鍛煉,成就體標

高效科研,實現學標


Python3繪圖庫Matplotlib(02)