zsh2270365028的个人博客分享 http://blog.sciencenet.cn/u/zsh2270365028

博文

[转载]设置主副刻度对象属性

已有 1032 次阅读 2022-7-19 22:53 |个人分类:Python|系统分类:科研笔记|文章来源:转载

refer to: https://www.cnblogs.com/zhouzetian/p/11609777.html

设置主副刻度对象属性

# encoding:utf-8
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator, FormatStrFormatter
import numpy as np

def test():
   ax = plt.subplot(111)  # 注意:一般都在ax中设置,不再plot中设置
   t = np.arange(0.0, 100.0, 1)
   s = np.sin(0.1 * np.pi * t) * np.exp(-t * 0.01)
   plt.plot(t, s, '--r*')
   # 修改主刻度
   xmajorLocator = MultipleLocator(20)  # 将x主刻度标签设置为20的倍数
   xmajorFormatter = FormatStrFormatter('%5.1f')  # 设置x轴标签文本的格式
   ymajorLocator = MultipleLocator(0.5)  # 将y轴主刻度标签设置为0.5的倍数
   ymajorFormatter = FormatStrFormatter('%1.1f')  # 设置y轴标签文本的格式
   # 设置主刻度标签的位置,标签文本的格式
   ax.xaxis.set_major_locator(xmajorLocator)
   ax.xaxis.set_major_formatter(xmajorFormatter)
   ax.yaxis.set_major_locator(ymajorLocator)
   ax.yaxis.set_major_formatter(ymajorFormatter)
   # 修改次刻度
   xminorLocator = MultipleLocator(5)  # 将x轴次刻度标签设置为5的倍数
   yminorLocator = MultipleLocator(0.1)  # 将此y轴次刻度标签设置为0.1的倍数
   # 设置次刻度标签的位置,没有标签文本格式
   ax.xaxis.set_minor_locator(xminorLocator)
   ax.yaxis.set_minor_locator(yminorLocator)
   # 打开网格
   ax.xaxis.grid(True, which='major')  # x坐标轴的网格使用主刻度
   ax.yaxis.grid(True, which='minor')  # y坐标轴的网格使用次刻度
   # 删除坐标轴的刻度显示
   # ax.yaxis.set_major_locator(plt.NullLocator())
   # ax.xaxis.set_major_formatter(plt.NullFormatter())
   plt.savefig("D:\脚本项目\工具\服务器监控\est.png")
   plt.show()
   
test()

 




https://m.sciencenet.cn/blog-587102-1348002.html

上一篇:[转载]核密度估计( Density Estimation)
下一篇:[转载]The default tick formatter

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-5-1 09:13

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部