William Denman
William Denman

Reputation: 3154

Latex tick label overlapping axis in graph generated by matplotlib

I am using the following code from the standard beginner's matplotlib tutorial.

from pylab import *

figure(figsize=(10, 6), dpi=80)
subplot(1,1,1)

X = np.linspace(-np.pi, np.pi, 256, endpoint=True)
C, S = np.cos(X), np.sin(X)

plot(X, C, color="blue", linewidth=2.5, linestyle="-",
     label=r'Cosine')
plot(X, S, color="red", linewidth=2.5, linestyle="-",
     label=r"Sine")

xlim(X.min()*1.1, X.max()*1.1)
ylim(C.min()*1.1, C.max()*1.1)

ax = gca()
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position('center')

ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data',0))

ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')

xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],
   [r'$-\pi$', r'$-\frac{\pi}{2}$',r'$0$', r'$\frac{\pi}{2}$',r'$+\pi$'])
yticks([-1, +1])

legend(loc='upper left')

for label in ax.get_xticklabels() + ax.get_yticklabels():
    label.set_fontsize(20)
    label.set_bbox(dict(facecolor='white', edgecolor='None', alpha=0.65))

show(block=False)

which results in the following graph

matplotlib result

I've tried to figure out why the latex tick labels for pi/2 and -pi/2 are intersecting the x-axis, but I cannot find anything on Google, SO or in the matplotlib documentation. Is this potentially a bug? I'm on OSX Mountain Lion, Python 2.7.6, matplotlib 1.3.1, ipython 2.1.0.

Upvotes: 1

Views: 916

Answers (1)

William Denman
William Denman

Reputation: 3154

Thanks to the comment by @Schorsch, I was able to narrow down the problem. It had to do with incompatibilities with two versions of libpng I had installed with Homebrew.

$brew info libpng 

libpng: stable 1.6.12 (bottled)
http://www.libpng.org/pub/png/libpng.html
/usr/local/Cellar/libpng/1.5.17 (15 files, 1.3M)
Built from source with: --universal
/usr/local/Cellar/libpng/1.6.12 (17 files, 1.3M) *

It seems when I installed matplotlib with

pip install matplotlib

It used libpng 1.5.17, but when running ipython --pylab it was using 1.6.12. To force pip to use the appropriate version of libpng I used the following shell variables.

export LDFLAGS="-L/usr/local/Cellar/libpng/1.6.12/lib/ -L/usr/X11/lib"
export CFLAGS="-I/usr/local/Cellar/libpng/1.6.12/include/ -I/usr/X11/include -I/usr/X11/include/freetype2"

Then to reinstall matplot lib

pip install --upgrade --force-reinstall matplotlib

Then to ensure that latex rendering is used

from matplotlib import rc
rc('text', usetex=True)

Results in the proper figure

fixed matplotlib figure

Upvotes: 1

Related Questions