matplotlib庫疑難問題---7、去掉刻度和邊框


matplotlib庫疑難問題---7、去掉刻度和邊框

一、總結

一句話總結:

去掉x軸刻度:將x軸的刻度置為空列表即可:plt.xticks([])
去掉上邊框:ax.spines['top'].set_visible(False)

 

 

二、matplotlib庫去掉刻度和邊框

博客對應課程的視頻位置:7、去掉刻度和邊框-范仁義-讀書編程筆記
https://www.fanrenyi.com/video/43/383

 

import numpy as np import matplotlib.pyplot as plt from scipy.interpolate import make_interp_spline # 設置matplotlib庫字體的非襯線字體為黑體 plt.rcParams["font.sans-serif"]=["SimHei"] # 設置matplotlib庫字體族為非襯線字體 plt.rcParams["font.family"]="sans-serif" fig, ax = plt.subplots() # 取消邊框 for key, spine in ax.spines.items(): # 'left', 'right', 'bottom', 'top' if(key == 'left' or key == 'right'): spine.set_visible(False) plt.xticks([]) plt.yticks([]) x=np.array([1,2,3,4,5]) y=np.array([4,9,6,8,3]) y_mean=np.mean(y).repeat(5) plt.plot(x,y,color='red', marker='o', linestyle='dashed',linewidth=0, markersize=12) plt.plot(x,y_mean,'k--') x_smooth = np.linspace(x.min(),x.max(),300) #300 represents number of points to make between T.min and T.max y_smooth = make_interp_spline(x, y)(x_smooth) plt.plot(x_smooth,y_smooth,'r--') plt.text(0.1,6,r'x均值'+r'$:\mu_x$', fontdict={'size':16,'color':'r'}) plt.show() 

1、去掉刻度

將x軸和y軸的刻度置為空列表即可

In [ ]:
# plt.xticks([])
# plt.yticks([]) 
In [ ]:
# 如果想把刻度改成需要的,也可以直接這樣改

# plt.xticks([1,2,3,4,5]) 

2、去掉邊框

In [8]:
import numpy as np import matplotlib.pyplot as plt fig, ax = plt.subplots() # 把上面邊框弄沒 ax.spines['top'].set_visible(False) # 下面 ax.spines['bottom'].set_visible(False) # 左邊 ax.spines['left'].set_visible(False) # 右邊 ax.spines['right'].set_visible(False) x=np.array([1,2,3,4,5]) y=np.array([4,9,6,8,3]) plt.plot(x,y) plt.show() 
In [11]:
for key, spine in ax.spines.items(): print(key,spine) # dir(spine) 
left Spine
right Spine
bottom Spine
top Spine
In [13]:
import numpy as np import matplotlib.pyplot as plt fig, ax = plt.subplots() for key, spine in ax.spines.items(): # 'left', 'right', 'bottom', 'top' if(key == 'left' or key == 'right'): spine.set_visible(False) x=np.array([1,2,3,4,5]) y=np.array([4,9,6,8,3]) plt.plot(x,y) plt.show() 
In [ ]:
 

本系列博客對應課程位置:
1、解決中文亂碼問題-范仁義-讀書編程筆記
https://www.fanrenyi.com/video/43/371
2、將曲線平滑-范仁義-讀書編程筆記
https://www.fanrenyi.com/video/43/372
3、matplotlib繪圖核心原理-范仁義-讀書編程筆記
https://www.fanrenyi.com/video/43/373
4、畫動態圖-范仁義-讀書編程筆記
https://www.fanrenyi.com/video/43/374
5、保存動態圖-范仁義-讀書編程筆記
https://www.fanrenyi.com/video/43/375
6、顯示圖片-范仁義-讀書編程筆記
https://www.fanrenyi.com/video/43/376

7、去掉刻度和邊框-范仁義-讀書編程筆記
https://www.fanrenyi.com/video/43/383

8、幾個點畫曲線-范仁義-讀書編程筆記
https://www.fanrenyi.com/video/43/384

9、畫箭頭(綜合實例)-1-范仁義-讀書編程筆記
https://www.fanrenyi.com/video/43/391

9、畫箭頭(綜合實例)-2-范仁義-讀書編程筆記
https://www.fanrenyi.com/video/43/392

10、畫直方圖-范仁義-讀書編程筆記
https://www.fanrenyi.com/video/43/393

11、畫動態直方圖-范仁義-讀書編程筆記
https://www.fanrenyi.com/video/43/394


免責聲明!

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



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