Latex font not showing in pyplot


#1

Dear all,
I’m trying to use pyplot to build some plots for my presentations, but fail to make it understand that latex text must use standard latex fonts. If you run the following code to generate the plot, look at the very last ‘text(…)’ line, and you’ll see that the text is not written with the standard math font

E_r12r6_a60 = [
    128    3.17163262103601652E-004  3.04754234834055342E-005
    256    3.55329500041287063E-004  5.43034214474548817E-005
    384    2.08699801081502499E-004  4.84644066530111190E-005
   512    4.68851824394179912E-005  3.76658372340429136E-005
   640   -6.66964350400179120E-005  1.91205674836690072E-005
   768   -5.74604545388544808E-005  4.75236802203928354E-005
   896   -1.97918380296343270E-004  5.59914036193412843E-005
]

# V = r9 - r6, as=60 
# 
E_r9r6_a60 = [
     384   1.21280095783323673E-004  1.15049519218257177E-005
     512   3.56601262113847618E-005  1.72789313480468575E-005
    640   8.86063423189557728E-005  2.55024026484808185E-005
    768  -1.12896944851750277E-004  2.82911400460744625E-005
    898  -1.04664038748627936E-004  4.26086017518738101E-005
   1024  -2.56311096809557603E-005  9.35155139386825067E-005
   1280  -3.23258472327046711E-004  9.07936725792574752E-005
]

# V = r12, as=60 
# 
E_r12_a60 = [
    256   1.81480315775224983E-004  9.73734651855710067E-006
    384   1.36113406511794972E-004  1.55201725330955719E-005
    512   8.09446597061414430E-005  2.06775684668961166E-005
    640   5.89765443188127326E-005  2.88503761209600540E-005
    768  -7.78756532543384224E-005  3.87389923780498535E-005
    898  -2.51162620852888826E-004  8.09722706303436795E-005
    1024 -2.07016022574518994E-004  9.01942302802278807E-005
    1280 -1.56478388392424709E-004  1.05340438481837134E-004
    1536 -2.39733040213125124E-004  1.41151925834172352E-004
]

# columns to plot

x1 = E_r12r6_a60[:,1];
y1 = E_r12r6_a60[:,2];
e1 = E_r12r6_a60[:,3];

x2 = E_r9r6_a60[:,1];
y2 = E_r9r6_a60[:,2];
e2 = E_r9r6_a60[:,3];

x3 = E_r12_a60[:,1];
y3 = E_r12_a60[:,2];
e3 = E_r12_a60[:,3];

# ----- max and min X to plot a 0 line

XX   = vcat(x1,x2,x3)
xmin = minimum(XX)
xmax = maximum(XX)

ax = gca() # Get the handle of the current axis
ax = gca()

# log scale on the Y axis
# ax[:set_xscale]("log") # Set the x axis to a logarithmic scaleå
# 
ax[:set_yscale]("linear") # Set the x axis to a logarithmic scale
ax[:set_ylim]([-0.0004,0.0004])

ax[:set_xlim]([300.,950.])
ax[:set_ylim]([-0.0003,0.0002])

# Title of the plot
# 
# rc("font",family="serif")           # "monospace" "sans"
font_size_title = 16
#titlefont = font("Times",font_size_title)
#titlefont = font("Times", 14)
title("Energy of the Droplets",fontsize=font_size_title)

# First the isolated points, with the color. s=20 is the size of the point
#
point_size = 30
line_width = 1
line_style = "dashed"  #solid"

scatter(x1,y1,s=point_size,color="red",label="r12-r6")
plot(x1, y1, linestyle=line_style, linewidth=line_width, color="red")
errorbar(x1,y1,yerr=e1,fmt="r ")

scatter(x2,y2,s=point_size,color="blue",label="r9-r6")
plot(x2, y2, linestyle=line_style, linewidth=line_width, color="blue")
errorbar(x2,y2,yerr=e2,fmt="b ")

scatter(x3,y3,s=point_size,color="green",label="r12")
plot(x3, y3, linestyle=line_style, linewidth=line_width,color="green")
errorbar(x3,y3,yerr=e3,fmt="b ")

# plot the zero line
# 
plot([xmin,xmax],[0.,0.])

font_labels_size = 14;
setp(ax[:get_xticklabels](),fontsize=font_labels_size,color="black") # X Axis font formatting
setp(ax[:get_yticklabels](),fontsize=font_labels_size,color="black") # Y Axis font formatting

font1 = Dict("family"=>"family",#family",
    "color"=>"black",
    "weight"=>"normal",
    "size"=>16)
xlabel("N",fontdict=font1)
ylabel(L"E(N)-E_{trap}",fontdict=font1)

# handletextpad controls the sapce between the symbol and the label in the legend
location = "upper right"
location = "lower left"
legend(fontsize=14,handletextpad=0.5,loc=location,frameon=false) # Create a legend of all the existing plots using their labels as names

text(730,0.00012,L"$a=60a_0$",fontsize=24)

How can I make it understand that the text in the L"…" form must use the latex standard fonts? I tried installing and using the LaTeXStrings package to no avail…

Thanks in advance,

Ferran.


#2
import PyPlot.rc
rc("text", usetex=true)

#3

In particular, note that Matplotlib by default uses its own mathtext equation renderer, not LaTeX. However, with some Matplotlib backends, you can tell it to use TeX instead with the usetex setting. You can also use the mathtext.fontset to change the default mathtext font.


#4

Thank you for the tips :slight_smile:
However, if I include this command, I do not get the plot to plot:

using PyPlot
import PyPlot.rc
rc("text",usetex=true)
plot(rand(20),rand(20)) 

produces

1-element Array{PyCall.PyObject,1}:
 PyObject <matplotlib.lines.Line2D object at 0x7f52f97738d0>

and no plot whatsoever. I tried to add pygui(true) just before the using PyPlot line, but that doe snot solve the problem of not showing up, either…


#5

If you read the page I linked, you’ll see that usetex doesn’t work with any GUI backends.