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

博文

[转载]关于python:如何将xticks更改为特定范围-设置坐标轴刻度的位置和样式

已有 4112 次阅读 2022-2-25 22:01 |个人分类:Python|系统分类:科研笔记|文章来源:转载

refer to : https://blog.csdn.net/weixin_43702663/article/details/92757156

https://www.codenong.com/56713197/

how to change the xticks to a specific range

本问题已经有最佳答案,请猛点这里访问。

我绘制了一个计数图,如下所示:

1
2

ax, fig = plt.subplots()
sns.countplot(user_id_count[:100])

(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]), )

enter image description here

但是我想将xticks更改为仅显示10, 20, 30, 40这4个数字,因此我检查了文档并将其重新编码如下:

1
2
3

ax, fig = plt.subplots()
sns.countplot(user_id_count[:100])
plt.xticks(range(10, 41, 10))

enter image description here

但是xticks并不是我想要的。
我已经搜索了相关问题,但没有得到我想要的。
因此,如果不介意有人可以帮助我吗?

相关讨论

  • plt.xticks(range(9, 40, 10), range(10, 41, 10))呢? 第一个范围是刻度线的x值,第二个范围是它们的标签。

  • @嘿,你是对的,这有效。 但是,如果将范围设置为超出数据,它将成为图中空白的一部分。


一种方法是在x轴上定义标签。 matplotlib模块中的set_xticklabels方法执行该作业(doc)。
通过定义自己的标签,可以通过将标签设置为''来隐藏它们。

通过定义自己的标签,您需要注意它们仍与数据一致。

这是一个例子:

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

# import modules
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

#Init seaborn
sns.set()

# Your data to count
y = np.random.randint(0,41,1000)

# Create the new x-axis labels
x_labels = ['' if i%10 != 0 else str(i) for i in range(len(np.unique(y)))]
print(x_labels)
# ['0', '', '', '', '', '', '', '', '', '',
# '10', '', '', '', '', '', '', '', '', '',
# '20', '', '', '', '', '', '', '', '', '',
# '30', '', '', '', '', '', '', '', '', '', '40']

# Create plot
fig, ax = plt.subplots()
sns.countplot(y)

# Set the new x axis labels
ax.set_xticklabels(x_labels)
# Show graph
plt.show()

enter image description here

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



https://blog.sciencenet.cn/blog-587102-1327035.html

上一篇:[转载]matplotlib修改坐标轴刻度值,刻度个数
下一篇:[转载]Python Matplotlib.ticker.LogLocator用法及代码示例
收藏 IP: 119.78.226.*| 热度|

0

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

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

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

GMT+8, 2024-4-25 12:50

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部