matplotlib.pyplot繪製圖像之同一圖中多條曲線對比
阿新 • • 發佈:2018-11-27
繪製sinx和cosx
# -*- coding:utf-8 -*- import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 2 * (np.pi)) #numpy.linspace(開始,終值(含終值)),個數) y1 = np.sin(x) y2 = np.cos(x) #畫圖 plt.title('Compare cosx with sinx') #標題 #plt.plot(x,y) #常見線的屬性有:color,label,linewidth,linestyle,marker等 plt.plot(x, y1, color='cyan', label='sinx') plt.plot(x, y2, 'b', label='cosx')#'b'指:color='blue' plt.legend() #顯示上面的label plt.xlabel('x') plt.ylabel('f(x)') plt.axis([0, 2*np.pi, -1, 1])#設定座標範圍axis([xmin,xmax,ymin,ymax]) #plt.ylim(-1,1)#僅設定y軸座標範圍 plt.show()
結果
python中的小tips
- np.arrage():開始值、終值和步長建立表示等差數列的一維陣列。得到的結果陣列不包含終值。
- np.linspace():開始值、終值和元素個數建立表示等差數列的一維陣列,可以通 過endpoint引數指定是否包含終值,預設值為True,即包含終值。
- π:np.pi
一些屬性
線的顏色:
‘b’ blue
‘g’ green
‘r’ red
‘c’ cyan
‘m’ magenta
‘y’ yellow
‘k’ black
‘w’ white
線的形狀:
‘-’ solid line style ‘–’ dashed line style ‘-.’ dash-dot line style ‘:’ dotted line style
點的標記:
‘.’ 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’ hexagon1 marker ‘H’ hexagon2 marker ‘+’ plus marker ‘x’ x marker ‘D’ diamond marker ‘d’ thin_diamond marker ‘|’ vline marker ‘_’ hline marker