Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib ytick's absolute value

In matplotlib, how can I use absolute value in the yticks?

For example, 100, 50, 0, 50, 100, 150.

Matplotlib

like image 496
Naffi Avatar asked Sep 06 '25 03:09

Naffi


1 Answers

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

enter image description here

like image 92
plasmon360 Avatar answered Sep 07 '25 17:09

plasmon360