2017-08-24 6 views
0

ここでは、line1とline2のディメンションがline3のディメンションと異なる場所を示すためのコードスニペットを示します。これらのいくつかの線を同じプロットにどのようにプロットしますか?それが起きると、matplotlibは例外をスローします。Python matplotlib同じプロット上の複数のプロットの等しくないディメンション

def demo(): 
    x_line1, y_line1 = np.array([1,2,3,4,5]), np.array([1,2,3,4,5]) 
    x_line2, y_line2 = np.array([1,2,3,4,5]), 2*y_line1 
    x_line3, y_line3 = np.array([1,2,3]), np.array([3,5,7]) 
    plot_list = [] 
    plot_list.append((x_line1, y_line1, "attr1")) 
    plot_list.append((x_line2, y_line2, "attr2")) 
    plot_list.append((x_line3, y_line3, "attr3")) 

    #Make Plots 
    title, x_label, y_label, legend_title = "Demo", "X-axis", "Y-axis", "Legend" 
    plt.figure() 
    for line_plt in plot_list: 
     plt.plot(line_plt[0], line_plt[1], label=line_plt[2]) 
    plt.title(title) 
    plt.xlabel(x_label) 
    plt.ylabel(y_label) 
    plt.legend(bbox_to_anchor=(1.04, 1), loc="upper left", borderaxespad=0, title=legend_title) 
    plt.show() 
+0

タラです質問に示されているようなeは正しいです。例外(エラー)が発生した場合は、完全なエラートレースバックとライブラリのバージョンを含め、コードを[mcve]にする必要があります(つまり、インポートを追加して実行方法を明示します)。 'hold = True'を使うことは推奨されず、どんな問題に対しても深刻な解決策にはなりません。 – ImportanceOfBeingErnest

答えて

0

それが取るすべてはそれが(「axes.holdは廃止され、3.0で削除されます」

warnings.warnを与えるにもかかわらず、this answerで説明するように「保留」を追加することです判明)

だから、私の未来の自己と他者への完全な解決策は

def demo(): 
    x_line1, y_line1 = np.array([1,2,3,4,5]), np.array([1,2,3,4,5]) 
    x_line2, y_line2 = np.array([1,2,3,4,5]), 2*y_line1 
    x_line3, y_line3 = np.array([1,2,3]), np.array([3,5,7]) 
    plot_list = [] 
    plot_list.append((x_line1, y_line1, "attr1")) 
    plot_list.append((x_line2, y_line2, "attr2")) 
    plot_list.append((x_line3, y_line3, "attr3")) 

    title, x_label, y_label, legend_title = "Demo", "X", "Y", "Legend" 
    plt.figure() # add this to allow for different lengths 
    plt.hold(True) 
    for line_plt in plot_list: 
     plt.plot(line_plt[0], line_plt[1], label=line_plt[2]) 
    plt.title(title) 
    plt.xlabel(x_label) 
    plt.ylabel(y_label) 
    plt.legend(bbox_to_anchor=(1.04, 1), loc="upper left", borderaxespad=0, title=legend_title) 
    plt.show() 
関連する問題