Trazar la configuración de ancho en el cuaderno ipython

80

Tengo las siguientes parcelas:

señales de sonido

Se vería mejor si tuvieran el mismo ancho. ¿Tiene alguna idea de cómo hacerlo en el cuaderno ipython cuando lo estoy usando %matplotlib inline?

ACTUALIZAR:

Para generar ambas figuras estoy usando las siguientes funciones:

import numpy as np
import matplotlib.pyplot as plt

def show_plots2d(title, plots, points, xlabel = '', ylabel = ''):
    """
    Shows 2D plot.

    Arguments:
        title : string
            Title of the plot.
        plots : array_like of pairs like array_like and array_like
            List of pairs,
            where first element is x axis and the second is the y axis.
        points : array_like of pairs like integer and integer
            List of pairs,
            where first element is x coordinate
            and the second is the y coordinate.
        xlabel : string
            Label of x axis
        ylabel : string
            Label of y axis
    """
    xv, yv = zip(*plots)
    y_exclNone = [y[y != np.array(None)] for y in yv]
    y_mins, y_maxs = zip(*
        [(float(min(y)), float(max(y))) for y in y_exclNone]
    )
    y_min = min(y_mins)
    y_max = max(y_maxs)
    y_amp = y_max - y_min
    plt.figure().suptitle(title)
    plt.axis(
        [xv[0][0], xv[0][-1], y_min - 0.3 * y_amp, y_max + 0.3 * y_amp]
    )
    plt.xlabel(xlabel)
    plt.ylabel(ylabel)
    for x, y in plots:
        plt.plot(x, y)
    for x, y in points:
        plt.plot(x, y, 'bo')
    plt.show()

def show_plot3d(title, x, y, z, xlabel = '', ylabel = '', zlabel = ''):
    """
    Shows 3D plot.

    Arguments:
        title : string
            Title of the plot.
        x : array_like
            List of x coordinates
        y : array_like
            List of y coordinates
        z : array_like
            List of z coordinates
        xlabel : string
            Label of x axis
        ylabel : string
            Label of y axis
        zlabel : string
            Label of z axis
    """
    plt.figure().suptitle(title)
    plt.pcolormesh(x, y, z)
    plt.axis([x[0], x[-1], y[0], y[-1]])
    plt.xlabel(xlabel)
    plt.ylabel(ylabel)
    plt.colorbar().set_label(zlabel)
    plt.show()
pt12lol
fuente
%matplotlib notebook
grisaitis

Respuestas:

80

Si lo usa %pylab inline, puede (en una nueva línea) insertar el siguiente comando:

%pylab inline
pylab.rcParams['figure.figsize'] = (10, 6)

Esto establecerá todas las figuras en su documento (a menos que se especifique lo contrario) para que sean del tamaño (10, 6), donde la primera entrada es el ancho y la segunda es la altura.

Vea esta publicación SO para más detalles. https://stackoverflow.com/a/17231361/1419668

Será
fuente
12
simplemente figsize(10, 6)hace el trabajo y los tiempos son más fáciles de recordar.
Alleo
67

Si no está en un cuaderno ipython (como el OP), también puede declarar el tamaño cuando declara la figura:

width = 12
height = 12
plt.figure(figsize=(width, height))
Ramon Martinez
fuente
4
¿Tiene alguna idea en qué unidades se mide esto?
Martin Thoma
1
Las unidades son pulgadas, pero YMMV (el DPI de su pantalla puede no coincidir con las suposiciones de matplotlib). Además, esto no funcionará en el entorno del OP, ¡un iPython Notebook!
placas de cocción
3
@hobs ¿qué versión del portátil iPython está ejecutando? me está funcionando en Jupyter.
Ramon Martinez
1
Funciona para mí en emacs / ein 20161228.741
Ott Toomet
3
@hobs, esto me funciona en ipython. El ipython de mi sitio está ligeramente pirateado, pero me sorprendería mucho que no funcionara también para el ipython genérico.
Jonathan Mayer
67

Esta es la forma en que lo hice:

%matplotlib inline
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = (12, 9) # (w, h)

Puede definir sus propios tamaños.

aakinlalu
fuente
4
plt.rcParams["figure.figsize"] =(12,9) también esto funciona
Roman Kazakov