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