Enregistrer une figure

fig = plt.figure()
plt.plot(x,y)
fig.set_size_inches(13,10) #largeur,hauteur
fig.savefig('path/nameFig'+'.png',dpi=100)

Enregistrer une fichier texte

f = open('name.txt','w')
f.write('line1'+'\n')
f.close()

Fonction cumulative (CDF)

plt.plot(np.sort(vec),np.linspace(1,len(vec),len(vec))/len(np.linspace(1,len(vec),len(vec))),color='black')
f = open('name.rdb','w')
shutil.move('name.rdb','path')

Échelle Log en scalaire

from matplotlib.ticker import ScalarFormatter
for axis in [ax.xaxis, ax.yaxis]:
axis.set_major_formatter(ScalarFormatter())

ax = plt.subplot(111)
ax.set_xscale("log")
ax.set_xticks([30,50,100])
ax.set_xticklabels([30,50,100])

 

Liste

eltMultiples = [x for x in list if list.count(x)>1]

Nombre d’occurrences :

compte = {}.fromkeys(set(liste),0)
for valeur in liste:
     compte[valeur] += 1

compte = dict([(k, liste.count(k)) for k in set(liste)])

supprimer les doublons en conservant l’ordre :

sorted(set(aa), key=lambda x: aa.index(x))

Annoter un plot

types = ['apple', 'orange', 'apple', 'pear', 'apple', 'orange', 'apple', 'pear']
x_coords = [10, 10, 5, 4, 3, 20, 19, 21]
y_coords = [21, 23, 12, 21, 10, 20, 14, 2]

for i,type in enumerate(types):
    x = x_coords[i]
    y = y_coords[i]
    plt.scatter(x, y, marker='x', color='red')
    plt.text(x+0.3, y+0.3, type, fontsize=9)
plt.show()

Histogrammes

bin = plt.hist(nmesSB1,histtype='step')[1]
plt.hist(nmesPlanets,bin,histtype='step',color='orange',label='Planets')
plt.hist(nmesBD,bin,histtype='step',color='blue',label='BD')
plt.hist(nmesSB1,bin,histtype='step',color='green',label='SB1')
plt.legend()
plt.show()