In matplotlib, how can I use absolute value in the yticks?
For example, 100, 50, 0, 50, 100, 150.
Take the current ticks using get_yticks, modify that and then use set_yticklabels. See the example below.
%matplotlib inline
import matplotlib.pyplot as plt
from math import trunc
a = np.random.rand(100)*30-20
plt.figure()
fig,ax = plt.subplots()
ax.bar(np.arange(len(a)), a)
ticks = ax.get_yticks()
# set labels to absolute values and with integer representation
ax.set_yticklabels([int(abs(tick)) for tick in ticks])
plt.show()
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