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

博文

[转载]matplotlib修改坐标轴刻度值,刻度个数

已有 1766 次阅读 2022-2-24 15:30 |个人分类:Python|系统分类:科研笔记|文章来源:转载

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

Matplotlib 中的 ticker 模块用于支持坐标轴刻度的修改,调用下列命令可以初步查看主副坐标轴刻度的数值定位方式(locator)与具体格式(formatter)等。

详情:http://matplotlib.org/api/axis_api.html

ax.xaxis.get_major_ticks()
ax.xaxis.get_minor_ticks()
ax.xaxis.get_major_locator()
ax.xaxis.get_minor_locator()
ax.xaxis.get_major_formatter()
ax.xaxis.get_minor_formatter()

设置主副刻度对象属性

# 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-1326855.html

上一篇:[转载]python Log Axis
下一篇:[转载]Matplotlib基础--自定义刻度

0

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

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

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

GMT+8, 2024-5-16 02:52

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部