x,y
color
linstyle
縮寫方式
marker, markersize
label
一次性繪制三個線條圖
用法:
matplot.pyplot.plot(*args, scalex=True, scaley=True, data=None, **kwargs)
參數解釋:
x,yimport numpy as npimport matplotlib.pyplot as pltx = np.arange(0.2, 2.0, 0.01)y1 = np.sin(2*np.pi*x)y2 = np.sin(4*np.pi*x)plt.figure(1)plt.subplot(211)plt.plot(x,y1)plt.subplot(212)plt.plot(x,y2)plt.show()
colorColors的值:
import numpy as npimport matplotlib.pyplot as plt# 需要解釋下,下面兩行代碼是防止出現中文時,會報警告# 因為我們的title裡面寫的是中文plt.rcParams['font.family'] = 'SimHei'plt.rcParams['axes.unicode_minus']=Falsex = np.arange(0.2, 2.0, 0.01)y1 = np.sin(2*np.pi*x)y2 = np.sin(4*np.pi*x)plt.figure(1)plt.subplot(211)plt.title('不添加顏色')plt.plot(x,y1)plt.subplot(212)plt.title('添加顏色')plt.plot(x,y2,color='c')plt.show()
linstyle'b' # blue markers with default shape'or' # red circles'-g' # green solid line'--' # dashed line with default color'^k:' # black triangle_up markers connected by a dotted line
import numpy as npimport matplotlib.pyplot as pltplt.figsize=((10,8))plt.rcParams['font.family'] = 'SimHei'plt.rcParams['axes.unicode_minus']=Falsex = [1, 2, 3, 4]y = [1, 4, 9, 16]plt.subplot(221)plt.title('樣式: -')plt.plot(x,y,'-')plt.subplot(222)plt.title('樣式: --')plt.plot(x,y,'--')plt.subplot(223)plt.title('樣式: -.')plt.plot(x, y, '-.')plt.subplot(224)plt.title('樣式: :')plt.plot(x, y, ':')plt.show()
縮寫方式import numpy as npimport matplotlib.pyplot as pltx = [1, 2, 3, 4]y = [1, 4, 9, 16]plt.subplot()# 線形狀 '-',顏色'g'plt.plot(x, y, '-g')plt.show()
marker, markersizemarker在scatter裡面我已經有所解釋過了,有好多種情況,可以在scatter散點圖這裡會將顏色和marker連接起來,可以有個很清楚的了解,並且較為清楚,也是縮寫
import matplotlib.pyplot as pltplt.figsize=((12,6))plt.rcParams['font.family'] = 'SimHei'plt.rcParams['axes.unicode_minus']=Falsex = [1, 2, 3, 4]y = [1, 4, 9, 16]plt.subplot(131)plt.title('默認情況')plt.plot(x, y)plt.subplot(132)plt.title('紅色圓圈')# marker為o 顏色rplt.plot(x, y, 'or')plt.subplot(133)plt.title('正三角黑色')# marker為^ 顏色k->blackplt.plot(x, y, '^k')plt.show()
label標簽,這個在所有圖形中都可以使用,在這裡展示下,包括之前的alpha也是,都所屬**kwargs裡面,在任何繪圖中都可以添加,legend為圖例
import matplotlib.pyplot as pltimport numpy as npx = np.linspace(-np.pi/2, np.pi/2, 31)y = np.cos(x)**3# 1) remove points where y > 0.7x2 = x[y <= 0.7]y2 = y[y <= 0.7]# 2) mask points where y > 0.7y3 = np.ma.masked_where(y > 0.7, y)# 3) set to NaN where y > 0.7y4 = y.copy()y4[y3 > 0.7] = np.nanplt.plot(x*0.1, y, 'o-', color='lightgrey', label='No mask')plt.plot(x2*0.4, y2, 'o-', label='Points removed')plt.plot(x*0.7, y3, 'o-', label='Masked values')plt.plot(x*1.0, y4, 'o-', label='NaN values')plt.legend()plt.show()
下面就是一些案例
一次性繪制三個線條圖import numpy as npimport matplotlib.pyplot as pltt = np.arange(0., 5., 0.2)# 紅色虛線,藍色方塊,淺藍六邊形plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'cH')plt.show()
import numpy as npimport matplotlib.pyplot as pltx1 = np.linspace(0.0, 5.0)y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)x2 = np.linspace(0.0, 2.0)y2 = np.cos(2 * np.pi * x2)plt.subplot(211)plt.plot(x1, y1, 'o-')plt.subplot(212)plt.plot(x1, y1, '.-')plt.show()
到此這篇關於利用python繪制線型圖的文章就介紹到這了,更多相關python線型圖內容請搜索軟件開發網以前的文章或繼續浏覽下面的相關文章希望大家以後多多支持軟件開發網!