¿Cómo le digo a matplotlib que he terminado con una trama?


El siguiente código traza dos archivos PostScript (.ps), pero el segundo contiene ambas líneas.

import matplotlib
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab

plt.subplot(111)
x = [1,10]
y = [30, 1000]
plt.loglog(x, y, basex=10, basey=10, ls="-")
plt.savefig("first.ps")


plt.subplot(111)
x = [10,100]
y = [10, 10000]
plt.loglog(x, y, basex=10, basey=10, ls="-")
plt.savefig("second.ps")

¿Cómo puedo decirle a matplotlib que comience de nuevo para la segunda parcela?

Author: Peter Mortensen, 2009-04-12

6 answers

Puede usar figure para crear una nueva gráfica, por ejemplo, o usar close después de la primera gráfica.

 103
Author: David Cournapeau,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-02-07 12:04:34

Hay un comando de figura clara, y debe hacerlo por usted:

plt.clf()

Si tiene múltiples subtramas en la misma figura

plt.cla()

Borra los ejes actuales.

 140
Author: randlet,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-03-19 03:05:01

Como se indica en David Cournapeau, use figure().

import matplotlib
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab

plt.figure()
x = [1,10]
y = [30, 1000]
plt.loglog(x, y, basex=10, basey=10, ls="-")
plt.savefig("first.ps")


plt.figure()
x = [10,100]
y = [10, 10000]
plt.loglog(x, y, basex=10, basey=10, ls="-")
plt.savefig("second.ps")

O subtrama(121) / subtrama(122) para la misma parcela, posición diferente.

import matplotlib
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab

plt.subplot(121)
x = [1,10]
y = [30, 1000]
plt.loglog(x, y, basex=10, basey=10, ls="-")

plt.subplot(122)
x = [10,100]
y = [10, 10000]
plt.loglog(x, y, basex=10, basey=10, ls="-")
plt.savefig("second.ps")
 28
Author: lmount,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-04-12 21:44:36

Simplemente ingrese plt.hold(False) antes del primer plt.parcela, y usted puede atenerse a su código original.

 12
Author: Dirklinux,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-03-22 10:52:09

Si está utilizando Matplotlib interactivamente, por ejemplo, en una aplicación web, (por ejemplo, ipython) tal vez esté buscando

plt.show()

En Lugar de plt.close() o plt.clf().

 6
Author: Damo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-03-19 11:17:15

Si ninguno de ellos está funcionando entonces compruebe esto.. digamos que si tiene matrices x e y de datos a lo largo del eje respectivo. Luego verifique en qué celda (jupyter) ha inicializado x e y para vaciar. Esto es porque, tal vez usted está añadiendo datos a x e y sin volver a inicializarlos. Así parcela tiene datos antiguos también. Así que comprueba eso..

 0
Author: Seenivasan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-02-10 12:45:53