简体   繁体   中英

Python: Legend disappears when using latex in plot

I have a plot in Python that works fine using the normal python text, but when I try to use latex in the plot, the legend disappears and I am unable to save the plot at all. I'm not sure if this matters, but I am using a Macbook Pro from 2011 with OS Sierra 10.12.4

Here is the code before I add the latex:

import matplotlib.pyplot as pl
import pylab

x = [0., 20., 40.]
y = [1.07e-4, 1.0e-4, 8.94e-5]
y_error = [5.74e-6, 2.46e-6, 3.11e-6]

pl.plot(x, y, "ro", label = "FZ200N")
pl.errorbar(x, y, yerr = y_error, linestyle = "None")

pl.title("Change in the Leakage Current with Annealing Time at 0C" + "\n")
pl.xlabel("Annealing Time (min)")#x label
pl.ylabel("Leakage Current (A)")#y label
pl.ticklabel_format(style='sci', axis='y', scilimits=(0,0))
pylab.xlim([-1,42])
pylab.legend(bbox_to_anchor=(1.0, 1), loc=2, borderaxespad=0.)
pl.show()

And now after I add the latex:

import matplotlib.pyplot as pl
import pylab

x = [0., 20., 40.]
y = [1.07e-4, 1.0e-4, 8.94e-5]
y_error = [5.74e-6, 2.46e-6, 3.11e-6]

pl.rc('text', usetex=True)
pl.rc('font', family='serif')
pylab.plot(x, y, "ro", label = r"FZ200N")
pl.errorbar(x, y, yerr = y_error, linestyle = "None")
pl.title(r"Change in the Depletion Voltage with Annealing Time at 0$^\circ$C" + "\n", fontsize = 14)
pl.xlabel(r"Annealing Time (min)", fontsize = 14)#x label
pl.ylabel(r"Depletion Voltage (V)", fontsize = 14)#y label
pl.ticklabel_format(style='sci', axis='y', scilimits=(0,0))
pylab.xlim([-1,42])
pylab.legend(bbox_to_anchor=(1.0, 1), loc=2, borderaxespad=0.)
pl.show()

删除情节标题中的换行符应该可以完成这项工作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM