Skip to content

Instantly share code, notes, and snippets.

@arnaldorusso
Last active August 29, 2015 14:14
Show Gist options
  • Save arnaldorusso/e542b04245560300d4f7 to your computer and use it in GitHub Desktop.
Save arnaldorusso/e542b04245560300d4f7 to your computer and use it in GitHub Desktop.
Render table inside figure area
############################################################
## Fig 1
## Latex is ploting wrong font but including \hline on table
############################################################
import matplotlib.pyplot as plt
import matplotlib as mpl
simple_latex = {
"text.latex.unicode" : True,
"text.usetex": True,
"font.family": "sans-serif",
"font.serif": [],
"font.sans-serif": ["Arial"],
"font.monospace": [],
"axes.labelsize": 9,
"xtick.labelsize": 9,
"ytick.labelsize": 9,
"xtick.direction": "out",
"ytick.direction": "out",
"axes.axisbelow": True,
"axes.facecolor": "white",
"axes.linewidth": 1.25,
"xtick.major.size": 0,
"ytick.major.size": 0,
"xtick.minor.size": 0,
"ytick.minor.size": 0,
"mathtext.fontset" : "stixsans",
"mathtext.default": "regular",
"pgf.preamble": [
r"\usepackage{mathspec}",
r"\setmathsfont(Digits,Latin,Greek){Arial}"
]
}
mpl.rcParams.update(simple_latex)
fig = plt.figure(figsize=(5.5, 3.4))
ax = fig.add_subplot(111)
ax.plot([1, 2, 3, 4, 5, 6])
ax.set_ylabel('Unit ('+u'μ'+r'velocity $\cdot$ m$^{-2}$ s$^{-1}$)', size=10)
table = r'''\begin{tabular}{lccccccc}\hline& t0& t1 & t2 & t3 & t4 & t5 & t6 \\\hline DLI & 35 & 38 & 10 & 22 & 25 & 85 & 22 \\\hline\end{tabular}'''
plt.text(1, 5, table, size=9)
plt.savefig('Latex_without_Arial.pdf')
plt.show()
####################################################################################
## [Fig 2]
## Latex is ploting right font (Arial) but not including spaces and \hline on table
####################################################################################
import matplotlib as mpl
mpl.use('pgf')
pgf_with_xelatex = {
"pgf.texsystem": "xelatex",
"text.usetex": True,
"font.family": "sans-serif",
"font.serif": [],
"font.sans-serif": ["Arial"],
"font.monospace": [],
"axes.labelsize": 9,
"xtick.labelsize": 9,
"ytick.labelsize": 9,
"xtick.direction": "out",
"ytick.direction": "out",
"axes.axisbelow": True,
"axes.facecolor": "white",
"axes.linewidth": 1.25,
"xtick.major.size": 0,
"ytick.major.size": 0,
"xtick.minor.size": 0,
"ytick.minor.size": 0,
"mathtext.fontset" : "stixsans",
"mathtext.default": "regular",
"pgf.preamble": [
r"\usepackage{mathspec}",
r"\setmathsfont(Digits,Latin,Greek){Arial}"
]
}
mpl.rcParams.update(pgf_with_xelatex)
import matplotlib.pyplot as plt
plt.subplots_adjust(left=0.12, right=0.97, top=0.95, bottom=0.1)
fig = plt.figure(figsize=(5.5, 3.4))
ax = fig.add_subplot(111)
ax.plot([1, 2, 3, 4, 5, 6])
ax.set_xlabel('X Label')
ax.set_ylabel('EMA x$^{2}$'+u' μ '+r'1, 2, 3, 9') # +u'μ'+
table = r'''\begin{tabular}{lccccccc}\hline& t0& t1 & t2 & t3 & t4 & t5 & t6 \\\hline DLI & 35 & 38 & 10 & 22 & 25 & 85 & 22 \\\hline\end{tabular}'''
plt.text(1, 5, table, size=9)
plt.savefig('Latex+Arial.pdf')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment