matplotlib(3)-- legend(圖例、說明、解釋),annotate(標注),text(標注)


 1 import matplotlib.pyplot as plt
 2 import numpy as np
 3 
 4 x = np.linspace(-3, 3, 50)
 5 y1 = 2 * x + 1
 6 y2 = x ** 2
 7 
 8 plt.figure()
 9 l1, = plt.plot(x, y1, color = "red", linewidth = 5.0, linestyle = '--', label = 'down')    #指定線的顏色, 寬度和類型
10 l2, = plt.plot(x, y2, label = "up")
11 
12 #給figure添加legend(圖例、說明、解釋)
13 plt.legend(handles = [l1, l2], labels = ["y1 = 2 * x + 1", "y2 = x ** 2"], loc = "best")
14 
15 plt.show()

 

 1 import matplotlib.pyplot as plt
 2 import numpy as np
 3 
 4 x = np.linspace(-3, 3, 50)
 5 y1 = 2 * x + 1
 6 X0 =1
 7 Y0 = 2 * X0 + 1
 8 
 9 #figure 1
10 plt.figure()
11 plt.plot(x, y1)
12 plt.scatter(X0, Y0, s = 30, color = 'black')     #在數據線上畫點
13 plt.plot([X0, X0], [Y0, 0], color = "black", linewidth = 3.0, linestyle = '--')       #過一點做垂直於X軸的垂線
14 
15 #坐標軸的移動 gca = “get current axis”
16 ax = plt.gca()
17 ax.spines["right"].set_color("none")
18 ax.spines["top"].set_color("none")
19 ax.xaxis.set_ticks_position("bottom")
20 ax.yaxis.set_ticks_position("left")
21 ax.spines["bottom"].set_position(("data", 0))   #Set the X and Y coordinates of the sprite simultaneously
22 ax.spines["left"].set_position(("data", 0))
23 
24 
25 #在figure上做標注的兩種方法
26 #方法一
27 #關於annotate的相關參數介紹,參考博文 https://blog.csdn.net/leaf_zizi/article/details/82886755
28 ##########################
29 plt.annotate(r"$2*x_0 + 1 = %s$"%Y0, xy = (X0, Y0), xytext = (+30, -30),
30              textcoords = "offset points", fontsize = 16,
31              arrowprops = dict(arrowstyle = "->", connectionstyle = "arc3, rad=.2"))
32 #方法二
33 #補充:關於下角標的添加 “_+下角標字母”
34 #關於text的相關參數介紹,參考博文 https://blog.csdn.net/TeFuirnever/article/details/88947248
35 ##########################
36 plt.text(-3.7, 3, r"$This\ is\ y1 = 2 * x + 1\ \mu\ \sigma_i\ \alpha_t$",
37          fontdict={"size":16, "color":"red"})
38 
39 plt.show()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM