在ipython笔记本中绘制宽度设置

pt1*_*lol 74 python matplotlib ipython ipython-notebook

我有以下情节:

声音信号

如果它们具有相同的宽度,它看起来会更好.你知道在我使用ipython笔记本时怎么做%matplotlib inline吗?

更新:

为了生成这两个数字,我使用以下函数:

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()
Run Code Online (Sandbox Code Playgroud)

Wil*_*ill 78

如果您使用%pylab inline,可以(在新行上)插入以下命令:

%pylab inline
pylab.rcParams['figure.figsize'] = (10, 6)
Run Code Online (Sandbox Code Playgroud)

这会将文档中的所有数字(除非另有说明)设置为大小(10, 6),其中第一个条目是宽度,第二个条目是高度.

有关详细信息,请参阅此SO帖子./sf/answers/1206195301/

  • 简单地说`figsize(10,6)`工作和时间更容易记住. (11认同)

Ram*_*nez 64

如果你不在ipython笔记本中(比如OP),你也可以在声明图形时声明大小:

width = 12
height = 12
plt.figure(figsize=(width, height))
Run Code Online (Sandbox Code Playgroud)

  • 你知道测量哪个单位吗? (4认同)
  • @hobs这在ipython中适合我.我的网站的ipython有点被黑,但如果它也不适用于通用的ipython,我会非常惊讶. (3认同)
  • @hobs您正在运行哪个版本的iPython笔记本?它在Jupyter为我工作. (2认同)

小智 64

这是我做的方式:

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

您可以定义自己的尺​​寸.

  • plt.rcParams ["figure.figsize"] = [12,9]#的作品 (7认同)
  • `plt.rcParams ["figure.figsize"] =(12,9)`这也行 (4认同)