前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >设置坐标轴刻度的位置和样式

设置坐标轴刻度的位置和样式

作者头像
生信修炼手册
发布2020-09-23 15:00:00
3.1K0
发布2020-09-23 15:00:00
举报
文章被收录于专栏:生信修炼手册

在matplotlib中,通过子模块ticker可以对坐标轴刻度的位置和样式进行设置。刻度线分为major和minor ticks, 通过以下4个函数可以对其位置和样式进行设置

1. set_major_locator

2. set_minor_locator

3. set_major_formatter

4. set_minor_formatter

1. locator

ticker模块中提供了多种locator类,部分列表如下

1. AutoLocator, 默认值,自动对刻度线的位置进行设置

2. MaxNLocator, 根据提供的刻度线的最大个数,自动设置

3. IndexLocator, 根据起始位置和间隔来设置刻度线

4. MultipleLocator, 根据指定的间隔来设置刻度线

5. FixedLocator, 根据提供的列表元素来设置刻度线

6. NullLocator,不显示刻度线

通过对以下所示的图,设置不同的Locator来看下其作用,代码如下

代码语言:javascript
复制
>>> import matplotlib.pyplot as plt
>>> import numpy as np
>>> x = np.linspace(0, 2 * np.pi, 50)
>>> plt.plot(x, np.sin(x), label='sin')
>>> plt.show()

输出结果如下

MaxNLocator的用法如下

代码语言:javascript
复制
>>> import matplotlib.ticker as ticker
>>> plt.plot(x, np.sin(x), label='sin')
>>> ax = plt.gca()
>>> ax.xaxis.set_major_locator(ticker.MaxNLocator(5))
>>> plt.show()

输出结果如下

IndexLocator的用法如下

代码语言:javascript
复制
>>> plt.plot(x, np.sin(x), label='sin')
>>> ax = plt.gca()
>>> ax.xaxis.set_major_locator(ticker.IndexLocator(2,0))
>>> plt.show()

输出结果如下

MultipleLocator的用法如下

代码语言:javascript
复制
>>> plt.plot(x, np.sin(x), label='sin')
>>> ax = plt.gca()
>>> ax.xaxis.set_major_locator(ticker.MultipleLocator(1.5))
>>> plt.show()

输出结果如下

FixedLocator的用法如下

代码语言:javascript
复制
>>> plt.plot(x, np.sin(x), label='sin')
>>> ax = plt.gca()
>>> ax.xaxis.set_major_locator(ticker.FixedLocator([0, 2, 4, 6]))
>>> plt.show()

输出结果如下

NullLocator的用法如下

代码语言:javascript
复制
>>> plt.plot(x, np.sin(x), label='sin')
>>> ax = plt.gca()
>>> ax.xaxis.set_major_locator(ticker.NullLocator())
>>> plt.show()

输出结果如下

2. formatter

和locator类相似,formatter也是有很多的类,部分列表如下

1. PercentFormatter,标签显示成百分比

2. StrMethodFormatter,根据字符串格式化语法进行格式化

3. FormatStrFormatter,根据字符串格式化语法进行格式化

4. MultipleLocator, 根据指定的间隔来设置刻度线

5. NullFormatter,不显示标签

PercentFormatter的用法如下

代码语言:javascript
复制
>>> plt.plot(x, np.sin(x), label='sin')
>>> ax = plt.gca()
>>> ax.xaxis.set_major_formatter(ticker.PercentFormatter())
>>> plt.show()

输出结果如下

StrMethodFormatter的用法如下

代码语言:javascript
复制
>>> plt.plot(x, np.sin(x), label='sin')
[<matplotlib.lines.Line2D object at 0x092D4CE8>]
>>> ax = plt.gca()
>>> ax.xaxis.set_major_formatter(ticker.StrMethodFormatter('{x:.2f}'))
>>> plt.show()

输出结果如下

FormatStrFormatter的用法如下

代码语言:javascript
复制
>>> plt.plot(x, np.sin(x), label='sin')
>>> ax = plt.gca()
>>> ax.xaxis.set_major_formatter(ticker.FormatStrFormatter('%.2f'))
>>> plt.show()

输出结果如下

NullFormatter的用法如下

代码语言:javascript
复制
>>> plt.plot(x, np.sin(x), label='sin')
>>> ax = plt.gca()
>>> ax.xaxis.set_major_formatter(ticker.NullFormatter())
>>> plt.show()

输出结果如下

通过ticker子模块,可以更加个性化的对刻度线位置和标签进行个性化设置。

·end·

—如果喜欢,快分享给你的朋友们吧—

原创不易,欢迎收藏,点赞,转发!生信知识浩瀚如海,在生信学习的道路上,让我们一起并肩作战!

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-09-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 生信修炼手册 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档