matplotlib(4)-- 數據覆蓋坐標軸時的相關處理、散點圖scatter、條形圖bar


 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 
 7 #figure 1
 8 plt.figure()
 9 plt.plot(x, y1, linewidth = 10,zorder=1)
10 
11 
12 #橫縱坐標軸顯示范圍設置
13 plt.xlim((-2, 2))
14 plt.ylim((-10, 10))
15 
16 #坐標軸的移動 gca = “get current axis”
17 ax = plt.gca()
18 ax.spines["right"].set_color("none")
19 ax.spines["top"].set_color("none")
20 ax.xaxis.set_ticks_position("bottom")
21 ax.yaxis.set_ticks_position("left")
22 ax.spines["bottom"].set_position(("data", 0))   #Set the X and Y coordinates of the sprite simultaneously
23 ax.spines["left"].set_position(("data", 0))
24 
25 #當出現數據覆蓋坐標值的情況:
26 
27 #方法一:直接將plt.plot(x, y1, linewidth = 10)修改為plt.plot(x, y1, linewidth = 10,zorder=1)即可
28 #方法二:將每一個坐標值取出來,作相關處理,使其覆蓋在數據上,以達到可視化處理
29 for label in ax.get_xticklabels() + ax.get_yticklabels():
30     label.set_fontsize(10)
31     label.set_zorder(1)
32     label.set_bbox(dict(facecolor = "White", edgecolor = "None", alpha = 0.1))
33 
34 plt.show()

 

 1 import matplotlib.pyplot as plt
 2 import numpy as np
 3 
 4 n = 1024
 5 X = np.random.normal(0, 1, n)
 6 Y = np.random.normal(0, 1, n)
 7 T = np.arctan2(Y, X)
 8 
 9 #關於scatter的相關參數介紹,參考博文 https://blog.csdn.net/qiu931110/article/details/68130199
10 plt.scatter(X, Y, s = 75, c = T, alpha = 0.5)
11 
12 plt.xlim((-1.5, +1.5))
13 plt.ylim((-1.5, +1.5))
14 
15 plt.show()

 

 1 import matplotlib.pyplot as plt
 2 import numpy as np
 3 
 4 n = 1024
 5 X = np.random.normal(0, 1, n)
 6 Y = np.random.normal(0, 1, n)
 7 T = np.arctan2(Y, X)
 8 
 9 #關於scatter的相關參數介紹,參考博文 https://blog.csdn.net/qiu931110/article/details/68130199
10 plt.scatter(X, Y, s = 75, c = T, alpha = 0.5)
11 plt.xlim((-1.5, +1.5))
12 plt.ylim((-1.5, +1.5))
13 
14 #將坐標標度隱藏
15 plt.xticks(())
16 plt.yticks(())
17 
18 plt.show()

 

 1 import matplotlib.pyplot as plt
 2 import numpy as np
 3 
 4 n = 12
 5 X = np.arange(n)
 6 Y1= (1 - X/float(n)) * np.random.uniform(0.5, 1.0, n)
 7 Y2= (1 - X/float(n)) * np.random.uniform(0.5, 1.0, n)
 8 
 9 #plt.bar()參數解釋,參考博文 https://www.cnblogs.com/shine-rainbow/p/10742952.html
10 #繪制條形圖
11 plt.bar(X, +Y1, facecolor = "#9999ff", edgecolor = "white")
12 plt.bar(X, -Y2, facecolor = "#ff9999", edgecolor = "white")
13 
14 #text()參數解釋
15 #####################
16 # plt.text(x, y, string, fontsize=15, verticalalignment="top", horizontalalignment="right")
17 # 參數:
18 # x,y:表示坐標值上的值
19 # string:表示說明文字
20 # fontsize:表示字體大小
21 # verticalalignment:垂直對齊方式 ,參數:[ ‘center’ | ‘top’ | ‘bottom’ | ‘baseline’ ]
22 # horizontalalignment:水平對齊方式 ,參數:[ ‘center’ | ‘right’ | ‘left’ ]
23 
24 for x,y in zip(X, Y1):
25     plt.text(x, y, "%.2f"%y, ha = "center", va = "bottom")
26 
27 for x,y in zip(X, -Y2):
28     plt.text(x, y, "%.2f"%y, ha = "center", va = "top")
29 
30 plt.xticks(())
31 plt.yticks(())
32 
33 plt.show()

 


免責聲明!

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



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