簡體   English   中英

將 1-dim 子圖的軸作為 2-d

[英]Acessing Axes of 1-dim subplots as 2-d

我有一個函數可以繪制一個帶有 2 個參數的分布函數的 3x3 圖表。 如果我只給我的分布函數 1 個參數,我想繪制一個 1x3 圖表。 如何正確操作? (就像我可以做一個 if 語句,如果我需要 1x3 然后調用 ax[j].plot() 但也許有更奇特的方法來做到這一點)

def plot_distribution(distr = normal, params = {'\u03BC' : [20, 50, 100], '\u03C3\u00B2' : [0.5, 0.6, 0.8]}, name='Normal Distribution', rows=3, columns=3): 
    fig, ax = plt.subplots(rows, columns, figsize=(15, 15))
    fig.suptitle(name, fontsize=45, y=0.94)

    for i in range(rows):
        for j in range(columns):
            x = np.arange(101, dtype='i')
            arg = params.values()
            ax[i][j].plot(distr(x, args(i, j, params)), color=get_color(i, j, rows), label=create_label([i, j], params))
            ax[i][j].set_xlim([0, 100])
#             ax[i][j].set_ylim([0, 1])
            ax[i][j].legend(loc='upper left')

我們可以在我們的軸數組上使用np.reshape()ax = np.reshape(ax, (-1, 3))

def plot_distribution(distr = normal, params = {'\u03BC' : [20, 50, 100], '\u03C3\u00B2' : [0.5, 0.6, 0.8]}, name='Normal Distribution', rows=3, columns=3): 
rows = 3 ** (len(params.values()) - 1)
fig, ax = plt.subplots(rows, columns, figsize=(15, 5 * rows))
fig.suptitle(name, fontsize=45, y=0.99)
ax = np.reshape(ax, (-1, 3))

for i in range(rows):
    for j in range(columns):
        x = np.arange(101, dtype='i')
        arg = params.values()
        ax[i][j].plot(distr(x, args(i, j, params)), color=get_color(i, j, columns), label=create_label([i, j], params))
        ax[i][j].set_xlim([0, 100])
        # ax[i][j].set_ylim([0, 1])
        ax[i][j].legend(loc='upper left')

暫無
暫無

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

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