|||
refer to : https://blog.csdn.net/weixin_43702663/article/details/92757156
https://www.codenong.com/56713197/
本问题已经有最佳答案,请猛点这里访问。
我绘制了一个计数图,如下所示:
1 | ax, fig = plt.subplots() |
(array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
34, 35, 36, 37, 38, 39, 40]), )
但是我想将xticks更改为仅显示10, 20, 30, 40这4个数字,因此我检查了文档并将其重新编码如下:
1 | ax, fig = plt.subplots() |
但是xticks并不是我想要的。
我已经搜索了相关问题,但没有得到我想要的。
因此,如果不介意有人可以帮助我吗?
plt.xticks(range(9, 40, 10), range(10, 41, 10))呢? 第一个范围是刻度线的x值,第二个范围是它们的标签。
@嘿,你是对的,这有效。 但是,如果将范围设置为超出数据,它将成为图中空白的一部分。
一种方法是在x轴上定义标签。 matplotlib模块中的set_xticklabels方法执行该作业(doc)。
通过定义自己的标签,可以通过将标签设置为''来隐藏它们。
通过定义自己的标签,您需要注意它们仍与数据一致。
这是一个例子:
1 | # import modules |
refer to: https://blog.csdn.net/weixin_43569478/article/details/108675782
在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来看下其作用,代码如下
输出结果如下
MaxNLocator的用法如下
输出结果如下
IndexLocator的用法如下
输出结果如下
MultipleLocator的用法如下
输出结果如下
FixedLocator的用法如下
输出结果如下
NullLocator的用法如下
输出结果如下
2. formatter
和locator类相似,formatter也是有很多的类,部分列表如下
1. PercentFormatter,标签显示成百分比
2. StrMethodFormatter,根据字符串格式化语法进行格式化
3. FormatStrFormatter,根据字符串格式化语法进行格式化
4. MultipleLocator, 根据指定的间隔来设置刻度线
5. NullFormatter,不显示标签
PercentFormatter的用法如下
输出结果如下
StrMethodFormatter的用法如下
输出结果如下
FormatStrFormatter的用法如下
输出结果如下
NullFormatter的用法如下
输出结果如下
通过ticker子模块,可以更加个性化的对刻度线位置和标签进行个性化设置。
————————————————
版权声明:本文为CSDN博主「生信修炼手册」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_43569478/article/details/108675782
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-9-21 12:40
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社