using % range in plot labels

Viewed 28

I want to change the range in terms of %. In the attached figure along x-axis, I want to label it as -1%, -0.05%, 0, 0.05% and 1% along x-axis. Is there any way to do that directly in python using range function?enter image description here

ax.set_xlim(-0.012, 0.012, 0.2)
1 Answers

You can modify the tick labels as shown below

fig, ax = plt.subplots()

x = np.linspace(-0.01, 0.01, 10)
ax.plot(x, -x/10, '-bo')

ax.set_xlim(-0.012, 0.012, 0.2)

labels = ['{:.2f}%'.format(item*100) for item in ax.get_xticks()]

ax.set_xticklabels(labels)

enter image description here

Related