matplotlib minus sign in tick labels has bad formatting

When using the standard tick labels, the minus signs are converted automatically to unicode minus signs. When changing the tick labels, the numbers are converted to strings. Such strings don’t get the automatic replacement to unicode minus signs. A solution is to do it explicitly:

import matplotlib.pyplot as plt
import numpy as np

plt.plot(np.arange(-50, 51, 10), np.arange(11))
plt.xticks(np.arange(-50, 51, 10), [f'{x}'.replace('-', '\N{MINUS SIGN}') for x in np.arange(-50, 51, 10)])
plt.show()

comparison plot

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top