matplotlib千分号
时间: 2023-09-11 11:06:39 浏览: 92
要在 matplotlib 中使用千分号(千位分隔符),您可以使用 `FuncFormatter` 函数来自定义轴标签的格式化方式。下面是一个示例代码:
```python
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter
# 定义千分号格式化函数
def format_func(value, tick_number):
value = '{:,.0f}'.format(value) # 将数字格式化为带有千分号的字符串
return value
# 创建示例数据
x = [1, 2, 3, 4, 5]
y = [1000, 2000, 3000, 4000, 5000]
# 绘制图表
plt.plot(x, y)
# 设置 y 轴标签格式化函数
formatter = FuncFormatter(format_func)
plt.gca().yaxis.set_major_formatter(formatter)
# 显示图表
plt.show()
```
这段代码会绘制一条简单的折线图,并将 y 轴标签格式化为带有千分号的形式。您可以根据需要进行修改和调整。希望对您有所帮助!