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?
MatPlotLib with Python To prevent scientific notation, we must pass style='plain' in the ticklabel_format method.
Use a FormatStrFormatter to prepend dollar signs on y axis labels. The use of the following functions, methods, classes and modules is shown in this example: matplotlib. pyplot.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With