otmezger
otmezger

Reputation: 10794

python + matplotlib: use locale to format y axis

I want to format my y axis using matplotlib in python 2.7. This is what I tried:

ax.yaxis.get_major_formatter().set_useLocale()

to format my y axis using . as thousands separator. Instead of having 10000, I'd like to have 10.000, and so on... but I can't find any example on how this work...

I could not find the documentation, on this page here there is no example or further documentation: http://matplotlib.org/api/ticker_api.html#matplotlib.ticker.ScalarFormatter.set_useLocale

Or any other idea on how to format my axis?

thanks

Upvotes: 5

Views: 2599

Answers (1)

cosmosis
cosmosis

Reputation: 6277

I believe that you are looking for more control than perhaps set_useLocale() can offer. Therefore, drawing upon the example given here, I've used FuncFormatter with a simple function. The comma_format function inserts the y-axis labels with a comma as a thousands separator and then replaces the commas with periods. In this way, the y-axis labels can be formatted rather easily.

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

enter image description here

Upvotes: 7

Related Questions