IPython Notebook中的图表宽度设置

87

我已经有了以下绘图:

声音信号

如果它们具有相同的宽度,看起来会更好。你有任何想法吗?当我使用%matplotlib inline时,你知道如何在ipython笔记本中做到这一点吗?

更新:

我使用以下函数生成两个图:

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()

%matplotlib notebook - william_grisaitis
3个回答

82
如果您使用%pylab inline,则可以(在新行上)插入以下命令:
%pylab inline
pylab.rcParams['figure.figsize'] = (10, 6)

这将设置您文档中的所有图表(除非另有规定)为尺寸(10, 6),其中第一个条目是宽度,第二个是高度。

查看此Stack Overflow帖子以了解更多详细信息。https://dev59.com/BmQm5IYBdhLWcg3w6SlR#17231361


12
只需使用figsize(10, 6)即可完成任务,且更易于记忆。 - Alleo
NameError: 名称 'figsize' 未定义 - Arrow_Raider

71

这是我完成它的方式:

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

您可以定义自己的尺寸。


4
plt.rcParams["figure.figsize"] =(12,9) 这行代码可以设置 Matplotlib 图形的大小。 - Roman Kazakov

67

如果您不在IPython Notebook中(例如OP),您也可以在声明图形时同时声明大小:

如果您不在IPython Notebook中(例如OP),您也可以在声明图形时同时声明大小:

width = 12
height = 12
plt.figure(figsize=(width, height))

4
你知道这是用哪种单位来衡量的吗? - Martin Thoma
1
单位为英寸,但你的显示器 DPI 可能与 matplotlib 的假设不匹配。此外,在 OP 的环境中,这将无法工作,因为那是一个 iPython 笔记本! - hobs
3
你正在使用哪个版本的iPython笔记本?在Jupyter中它对我起作用了。 - Heberto Mayorquin
在我的电脑上无法运行。笔记本服务器的版本是4.2.1。Python 3.5.2,Anaconda 4.1.1(x86_64)。 - arun
1
在我的emacs/ein 20161228.741中可以工作。 - Ott Toomet
3
这在我使用的IPython中可行。虽然我的网站上的IPython稍微有些修改,但如果在通用的IPython中不起作用,我会非常惊讶。 - Jonathan Mayer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接