使用函數繪製matplotlib的圖標組成元素

1.函數plot()——展現變量的趨勢變化

函數功能:展現變量的趨勢變化
調用簽名:plt.plot(x,y,ls="-",lw=2,label=“plot fogure”
參數說明

  • x:x軸上的數值
  • y:y軸上的數值
  • ls:折線圖的線條風格
  • lw:折線圖的線條寬度
  • label:標記圖形內容的標籤文本

調用展示
(1)代碼實現

import matplotlib.pyplot as plt
import numpy as np
x=np.linspace(0.05,10,1000)
y=np.cos(x)
plt.plot(x,y,ls="-",lw=2,label="plot figure")
plt.legend()
plt.show()

(2)運行結果,如下圖
在這裏插入圖片描述

2.函數scatter()——尋找變量之間的關係

函數功能:尋找變量之間的關係
調用簽名:plt.scatter(x,y,c=“b”,label=“scatter figure”
參數說明

  • x:x軸上的數值
  • y:y軸上的數值
  • c:散點圖中的標記的顏色
  • label:標記圖形內容的標籤文本

調用展示
(1)代碼實現

import matplotlib.pyplot as plt
import numpy as np
x=np.linspace(0.05,10,1000)
y=np.random.rand(1000)
plt.scatter(x,y,label="scatter figure")
plt.legend()
plt.show()

(2)運行結果
在這裏插入圖片描述

3.函數xlim()——設置X軸的數值顯示範圍

函數功能:設置X軸的數值顯示範圍
調用簽名:plt.xlim(xmin,xmax)
參數說明

  • xmin:x軸上的最小值
  • xmax:x軸上的最大值
  • 平移性:上面的函數功能,調用簽名和參數說明同樣可以平移到函數ylim()上

調用展示
(1)代碼實現

import matplotlib.pyplot as plt
import numpy as np
x=np.linspace(0.05,10,1000)
y=np.random.rand(1000)
plt.scatter(x,y,label="scatter figure")
plt.legend()
plt.xlim(0.05,10)
plt.ylim(0,1)
plt.show()

(2)運行結果
在這裏插入圖片描述

4.函數xlabel()——設置X軸的標籤文本

函數功能:設置X軸的標籤文本
調用簽名:plt.xlabel(string)
參數說明

  • string:標籤文本內容,引號括
  • 平移性:上面的函數功能,調用簽名和參數說明同樣可以平移到函數ylabel()上

調用展示
(1)代碼實現

import matplotlib.pyplot as plt
import numpy as np
x=np.linspace(0.05,10,1000)
y=np.sin(x)
plt.plot(x,y,ls="-",lw=2,c="c",label="plot figure")
plt.legend()
plt.xlabel("x-axis")
plt.ylabel("y-axis")
plt.show()

(2)運行結果
在這裏插入圖片描述

5.函數grid()——繪製刻度線的網格線

函數功能:繪製刻度線的網格線
調用簽名:plt.grid(linestyle=":",color=“r”
參數說明

  • linestyle:網格線的線條風格
  • color:網格線的線條顏色

調用展示
(1)代碼實現

import matplotlib.pyplot as plt
import numpy as np
x=np.linspace(0.05,10,1000)
y=np.sin(x)
plt.plot(x,y,ls="-",lw=2,c="c",label="plot figure")
plt.legend()
plt.grid(linestyle=":",color="r")
plt.show()

(2)運行結果
在這裏插入圖片描述

6.函數axhline()——繪製平行於X軸的水平參考線

函數功能:繪製平行於x軸的水平參考線
調用簽名:plt.axhline(y=0.0,c=“r”,ls="–",lw=2)
參數說明

  • y:水平參考線的出發點
  • c:參考線的線條顏色
  • ls:參考線的線條風格
  • lw:參考線的線條寬度
  • 平移性:上面的函數功能,調用簽名和參數說明同樣可以平移到函數axvline()上

調用展示
(1)代碼實現

import matplotlib.pyplot as plt
import numpy as np
x=np.linspace(0.05,10,1000)
y=np.sin(x)
plt.plot(x,y,ls="-.",lw=2,c="c",label="plot figure")
plt.legend()
plt.axhline(y=0.0,c="r",ls="--",lw=2)
plt.axvline(x=4.0,c="r",ls="--",lw=2)
plt.show()

(2)運行結果
在這裏插入圖片描述

7.函數axvspan()——繪製垂直於X軸的參考區域

函數功能:繪製垂直於X軸的參考區域
調用簽名:
plt.axvspan(xmin=1.0,xmax=2.0,facecolor=“y”,alpha=0.3)
參數說明

  • xmin:參考區域的起始位置
  • xmax:參考區域的終止位置
  • facecolor:參考區域的填充顏色
  • alpha:參考區域填充顏色透明度
  • 平移性:上面的函數功能,調用簽名和參數說明同樣可以平移到函數axhspan()上

調用展示
(1)代碼實現

import matplotlib.pyplot as plt
import numpy as np
x=np.linspace(0.05,10,1000)
y=np.sin(x)
plt.plot(x,y,ls="-.",lw=2,c="c",label="plot figure")
plt.legend()
plt.axhspan(ymin=0.0,ymax=0.5,facecolor="y",alpha=0.3)
plt.axvspan(xmin=4.0,xmax=6.0,facecolor="y",alpha=0.3)
plt.show()

(2)運行結果
在這裏插入圖片描述

8.函數annotate()——添加圖形內容細節的指向型註釋文本

函數功能:添加圖形內容細節的指向型註釋文本
調用簽名
plt.annotate(string,xy=(np.pi/2,1.0),xytext=((np.pi/2)+1.0,0.8),weight=“bold”,color=“b”,arrowprops=dict(arrowstyle=">",connectionstyle=“arc3”,color=“b”)
參數說明

  • string:圖形內容的註釋文本
  • xy:被註釋圖形內容的位置座標
  • xytext:註釋文本的位置座標
  • weight:註釋文本的字體粗細風格
  • color:註釋文本的字體顏色
  • arrowprops:指示被註釋內容的箭頭的屬性字典

調用展示
(1.1)代碼實現

import matplotlib.pyplot as plt
import numpy as np
x=np.linspace(0.05,10,1000)
y=np.sin(x)
plt.plot(x,y,ls="-.",lw=2,c="c",label="plot figure")
plt.legend()
plt.annotate("maximum",
			 xy=(np.pi/2,1.0),
			 xytext=((np.pi/2)+1.0,0.8),
			 weight="bold",
			 color="b",
			 arrowprops=dict(arrowstyle="->",connectionstyle="arc3",color="b")
			 )
plt.show()

(1.2)運行結果
在這裏插入圖片描述
(2.1)代碼實現

import matplotlib.pyplot as plt
import numpy as np
x=np.arange(0,6)
y=x*x
plt.plot(x,y,marker="o")
for xy in zip(x,y):
	plt.annotate("(%s,%s"%xy,xy=xy,xytext=(-20,10),textcoords='offset points')
plt.show()

(2.2)運行結果
在這裏插入圖片描述
(3.1)代碼實現

import matplotlib.pyplot as plt
import numpy as np
x=np.arange(0,6)
y=x*x
plt.plot(x,y,marker="o")
for xy in zip(x,y):
	plt.annotate("(%s,%s"%xy,xy=xy,xytext=(-20,10),textcoords='offset points',
	bbox=dict(boxstyle='round,pad=0.5',fc="yellow",ec="k",lw=1,alpha=0.5))
plt.show()

(3.2)運行結果
在這裏插入圖片描述

9.函數text()——添加圖形內容細節的無指向型註釋文本

函數功能:添加圖形內容細節的無指向型註釋文本
調用簽名:plt.text(x,y,string,weight=“bold”,color="b)
參數說明

  • x:註釋文本內容所在位置的橫座標
  • y:註釋文本內容所在位置的縱座標
  • string:註釋文本內容
  • weight:註釋文本內容的粗細風格
  • color:註釋文本內容的字體風格

調用展示
(1)代碼實現

import matplotlib.pyplot as plt
import numpy as np
x=np.linspace(0.05,10,1000)
y=np.sin(x)
plt.plot(x,y,ls="-.",lw=2,c="c",label="plot figure")
plt.legend()
plt.text(3.10,0.09,"y=sin(x)",weight="bold",color="b")
plt.show()

(2)結果展示
在這裏插入圖片描述

10.函數title()——添加圖形內容的標題

函數功能:添加函數內容的標題
調用簽名:plt.title(string)
參數說明

  • string:圖形內容的標題文本

調用展示
(1)代碼實現

import matplotlib.pyplot as plt
import numpy as np
x=np.linspace(0.05,10,1000)
y=np.sin(x)
plt.plot(x,y,ls="-.",lw=2,c="c",label="plot figure")
plt.legend()
plt.title("y=sin(x)")
plt.show()

(2)運行結果
在這裏插入圖片描述

11.函數legend()——標示不同圖形的文本標籤圖例

函數功能:標示不同圖形的文本標籤圖例
調用簽名:plt.legend(loc="lower left)
參數說明

  • loc:圖例在圖中的地理位置(lower upper center 與left right 的組合)

調用展示
(1)代碼實現

import matplotlib.pyplot as plt
import numpy as np
x=np.linspace(0.05,10,1000)
y=np.sin(x)
plt.plot(x,y,ls="-",lw=2,c="c",label="plot figure")
plt.legend(loc="upper center")
plt.show()

(2)運行結果
在這裏插入圖片描述

12.函數組合應用

代碼展示

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm as cm

#define data
x=np.linspace(0.5,3.5,100)
y=np.sin(x)
y1=np.random.randn(100)

#scatter figure
plt.scatter(x,y1,c="0.25",label="scatter figure")

#plot figure
plt.plot(x,y,ls="--",lw=2,label="plot figure")

#some clean up(removing chartjunk)
#turn the top spine and the right spine off
for spine in plt.gca().spines.keys():
	if spine=="top"or spine=="right":
		plt.gca().spines[spine].set_color("none")
#turn bottom tick for x-axis on
plt.gca().xaxis.set_ticks_position("bottom")
#set tick_line position of bottom

#turn left ticks for y-axis on
plt.gca().yaxis.set_ticks_position("left")
#set tick_line position of left

#set x,yaxis limit
plt.xlim(0.0,4.0)
plt.ylim(-3.0,3.0)

#set axes labels
plt.xlabel("x-axis")
plt.ylabel("y-axis")

#set x,yaxis grid
plt.grid(True,ls=":",c="r")

#set a horizontal line across the axis
plt.axhline(y=0.0,c="r",ls="--",lw=2)

#set a vertical span across the axis
plt.axvspan(xmin=1.0,xmax=2.0,facecolor="y",alpha=0.2)

#set annotating info
plt.annotate("maximum",
			 xy=(np.pi/2,1.0),
			 xytext=((np.pi/2)+0.15,1.5),
			 weight="bold",
			 color="r",
			 arrowprops=dict(arrowstyle="->",connectionstyle="arc3",color="r")
			 )
plt.annotate("spines",
			 xy=(0.75,-3),
			 xytext=(0.35,-2.25),
			 weight="bold",
			 color="b",
			 arrowprops=dict(arrowstyle="->",connectionstyle="arc3",color="b")
			 )
plt.annotate("",
			 xy=(0,-2.78),
			 xytext=(0.4,-2.32),
			 arrowprops=dict(arrowstyle="->",connectionstyle="arc3",color="b")
			 )
plt.annotate("",
			 xy=(3.5,-2.98),
			 xytext=(3.6,-2.70),
			 arrowprops=dict(arrowstyle="->",connectionstyle="arc3",color="b")
			 )

#set text info
plt.text(3.6,-2.70,"'|' is tickline",weight='bold',c="b")
plt.text(3.6,-2.95,"3.5 is ticklabel",weight='bold',c="b")

#set title
plt.title("structrue of matplotlib")

#set legend
plt.legend()

plt.show()

運行結果
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章