The following code generates a simple plot, where the y axes has a label generated by a LaTeX command
import numpy as np
import matplotlib.pyplot as plt
%matplotlib notebook
plt.figure()
hatdelta = '$\hat{\Delta}$'
xlist = np.array([ 0, 1 ])
ylist = np.array([ 1, 2 ])
plt.errorbar(xlist, ylist, fmt='o', capsize=2)
ax = plt.gca()
ax.set_ylabel(hatdelta, fontsize=16)
plt.draw()
plt.show()
I am using jupyter-notebook to run the code. This is the actual result:
A close inspection of the y axes reveals that the LaTeX is incorrectly rendered:
The hat is not centered over the symbol. However, the LaTeX code should correctly center the hat. This is, for example, the output of a LaTeX source with the same command:
Is it possible to fix this incorrect LaTeX rendering?
You have two options.
import numpy as np
import matplotlib.pyplot as plt
# To use LaTeX
plt.rcParams['text.usetex'] = True
plt.figure()
hatdelta = '$\hat{\Delta}$'
xlist = np.array([ 0, 1 ])
ylist = np.array([ 1, 2 ])
plt.errorbar(xlist, ylist, fmt='o', capsize=2)
ax = plt.gca()
ax.set_ylabel(hatdelta, fontsize=16)
plt.draw()
plt.show()
Or
import numpy as np
import matplotlib.pyplot as plt
plt.figure()
# Improve \hat
hatdelta = '$\hat{\,\Delta}$'
xlist = np.array([ 0, 1 ])
ylist = np.array([ 1, 2 ])
plt.errorbar(xlist, ylist, fmt='o', capsize=2)
ax = plt.gca()
ax.set_ylabel(hatdelta, fontsize=16)
plt.draw()
plt.show()