Issue
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.
I suspect that I need to use a FuncFormatter but I am at a bit of a loss.
Can anyone help?
Solution
Yes, you can use matplotlib.ticker.FuncFormatter
to do this.
Here is the example:
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()
This results in the following plot:
Answered By - Andrey Sobolev
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.