Formatting Numbers to have a comma on plt.colorbar() in Python

Viewed 1396

I am trying to format my colorbar such the numbers are formatted with commas. Any help would be greatly appreciated

import numpy as np
import matplotlib.pyplot as plt

plt.matshow(np.array(([30000,8000],[12000,25000])))
plt.colorbar()
1 Answers

You can create and specify a custom formatter:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter

comma_fmt = FuncFormatter(lambda x, p: format(int(x), ','))

plt.matshow(np.array(([30000,8000],[12000,25000])))
plt.colorbar(format=comma_fmt)

plt.show()

enter image description here

Related