簡體   English   中英

ipython notebook 中的繪圖寬度設置

[英]Plot width settings in ipython notebook

我有以下情節:

聲音信號

如果它們具有相同的寬度會更好看。 當我使用%matplotlib inline時,你知道如何在 ipython notebook 中做到這一點嗎?

更新:

為了生成這兩個數字,我使用了以下函數:

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

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

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

這會將文檔中的所有圖形(除非另有說明)設置為(10, 6)大小,其中第一個條目是寬度,第二個條目是高度。

有關更多詳細信息,請參閱此 SO 帖子。 https://stackoverflow.com/a/17231361/1419668

我是這樣做的:

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

您可以定義自己的尺​​寸。

如果您不在 ipython 筆記本中(如 OP),您也可以在聲明圖形時聲明大小:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM