Matplotlib:轴的逗号分隔数字格式 [英] Matplotlib : Comma separated number format for axis

查看:134
本文介绍了Matplotlib:轴的逗号分隔数字格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将轴的格式更改为在Python 2.7下运行的Matplotlib中以逗号分隔,但无法这样做.

I am attempting to change the format of my axis to be comma seperated in Matplotlib running under Python 2.7 but am unable to do so.

我怀疑我需要使用FuncFormatter,但我有点茫然.

I suspect that I need to use a FuncFormatter but I am at a bit of a loss.

任何人都可以帮忙吗?

推荐答案

是的,您可以使用matplotlib.ticker.FuncFormatter来做到这一点.

Yes, you can use matplotlib.ticker.FuncFormatter to do this.

这里是示例:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as tkr

def func(x, pos):  # formatter function takes tick label and tick position
    s = str(x)
    ind = s.index('.')
    return s[:ind] + ',' + s[ind+1:]   # change dot to comma

y_format = tkr.FuncFormatter(func)  # make formatter

x = np.linspace(0,10,501)
y = np.sin(x)
ax = plt.subplot(111)
ax.plot(x,y)
ax.yaxis.set_major_formatter(y_format)  # set formatter to needed axis

plt.show()

这将导致以下绘图:

这篇关于Matplotlib:轴的逗号分隔数字格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆