Skip to content

Instantly share code, notes, and snippets.

@JeanOlivier
Created September 9, 2020 22:21
Show Gist options
  • Save JeanOlivier/89d30ebc65ad7ceb22db020bca8b404e to your computer and use it in GitHub Desktop.
Save JeanOlivier/89d30ebc65ad7ceb22db020bca8b404e to your computer and use it in GitHub Desktop.
Exemple figures python compilées avec math latex et packages personels
#!/usr/bin/env python
# -*- coding: utf-8 -*-
try:
__ISINTERACTIVE__ = __IPYTHON__
except:
__ISINTERACTIVE__ = False
print "IS NOT INTERACTIVE"
if not __ISINTERACTIVE__:
import matplotlib as matplotlib
matplotlib.use('pgf')
matplotlib.rc('pgf', texsystem='pdflatex') # from running latex -v
preamble = matplotlib.rcParams.setdefault('pgf.preamble', [])
preamble.append(r'\usepackage[usenames,dvipsnames,svgnames,x11names]{xcolor}')
preamble.append(r'\usepackage{newcent, millennial}')
preamble.append(r'\usepackage[squaren,Gray]{SIunits}')
preamble.append('\definecolor{solblue}{HTML}{268bd2}')
preamble.append('\definecolor{solred}{HTML}{dc322f}')
preamble.append('\definecolor{solviolet}{HTML}{6c71c4}')
preamble.append('\definecolor{solmagenta}{HTML}{d33682}')
preamble.append('\definecolor{solcyan}{HTML}{2aa198}')
preamble.append('\definecolor{solorange}{HTML}{cb4b16}')
preamble.append('\definecolor{solyellow}{HTML}{b58900}')
preamble.append('\definecolor{solgreen}{HTML}{859900}')
matplotlib.rcParams['axes.unicode_minus'] = False
from pylab import *
# Charger les données ici
ymax=10*pi
x = linspace(0, ymax, 201)
y = sin(x)
def plot_mafigure(save=False):
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, sin(x), 'o-', label='Sinus', alpha=.5)
ax.plot(x, sin(x)**2, 'o-', label=ur'Sinus carré', alpha=.5) # ur nécessaire pour accent
ax.set_xlabel('$x$')
ax.set_ylabel('Fonction de $x$')
ax.set_ylim(-1,1.25)
ax.set_xlim(0,ymax)
ax.legend(loc='upper left', ncol=2)
if save:
fig.savefig('ma_figure.pdf', bbox_inches='tight')
return ax
def plot_all(save=False):
plot_mafigure(save=save)
# Ajouter d'autres fonctions éventuellement
if __name__ == "__main__":
if not __ISINTERACTIVE__:
plot_all(True) # Si exécuter PAS en mode interactif, on sauve en PDF en passant par latex.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment