matplotlib FormatStrFormatter sets the coordinate axes as integers and decimals [set the number of decimal points]

FormatStrFormatter Set up with

1 is set to an integer

import matplotlib.pyplot as plt
from matplotlib.ticker import FormatStrFormatter

# 创建一个图表
fig, ax = plt.subplots()

# 生成一些示例数据
x = [1, 2, 3, 4, 5]
y = [1000, 2000, 3000, 4000, 5000]

# 在 x 轴上设置刻度标签格式化器为整数格式
ax.xaxis.set_major_formatter(FormatStrFormatter('%d'))

# 绘制数据
ax.plot(x, y)

# 显示图表
plt.show()

insert image description here

2 Set to decimal [control the number of decimal points]

import matplotlib.pyplot as plt
from matplotlib.ticker import FormatStrFormatter

# 创建一个图表
fig, ax = plt.subplots()

# 生成一些示例数据
x = [1, 2, 3, 4, 5]
y = [1.234, 2.567, 3.891, 4.123, 5.456]

# 在 y 轴上设置刻度标签格式化器为两位小数
ax.yaxis.set_major_formatter(FormatStrFormatter('%.2f'))

# 绘制数据
ax.plot(x, y)

# 显示图表
plt.show()

insert image description here

Guess you like

Origin blog.csdn.net/weixin_45913084/article/details/132217420