【问题标题】:python + matplotlib: use locale to format y axispython + matplotlib:使用语言环境来格式化 y 轴
【发布时间】:2013-11-22 00:31:57
【问题描述】:

我想在 python 2.7 中使用 matplotlib 格式化我的 y 轴。这是我尝试过的:

ax.yaxis.get_major_formatter().set_useLocale()

使用. 作为千位分隔符来格式化我的 y 轴。我不想拥有10000,而是拥有10.000,等等......但我找不到任何关于它如何工作的例子......

我找不到文档,在此页面上没有示例或进一步的文档:http://matplotlib.org/api/ticker_api.html#matplotlib.ticker.ScalarFormatter.set_useLocale

或者关于如何格式化我的轴的任何其他想法?

谢谢

【问题讨论】:

标签: python matplotlib locale axis


【解决方案1】:

我相信您正在寻找比set_useLocale() 所能提供的更多控制权。因此,根据here 给出的示例,我使用了FuncFormatter 和一个简单的函数。 comma_format 函数使用逗号插入 y 轴标签作为千位分隔符,然后用句点替换逗号。通过这种方式,可以很容易地格式化 y 轴标签。

from pylab import *
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker

def comma_format(x, p):
    return format(x, "6,.0f").replace(",", ".")

ax = subplot(111)
xx = np.arange(0,20,1)
yy = np.arange(1000,10000,450)
ax.get_yaxis().set_major_formatter(ticker.FuncFormatter(comma_format))
plt.scatter(xx,yy)
plt.show()

【讨论】:

    猜你喜欢
    • 2022-07-28
    • 2016-03-29
    • 1970-01-01
    • 1970-01-01
    • 2015-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-25
    相关资源
    最近更新 更多