0

matplotlib を使用してバージョン データのスタック プロットを生成しようとしています。その部分は機能しており、適切に表示されていますが、凡例に隅にある空の四角以外のものを表示することはできません。

ra_ys = np.asarray(ra_ys)

# Going to generate a stack plot of the version stats
fig = plt.figure()

ra_plot = fig.add_subplot(111)

# Our x axis is going to be the dates, but we need them as numbers
x = [date2num(date) for date in dates]

# Plot the data
ra_plot.stackplot(x, ra_ys)

# Setup our legends
ra_plot.legend(ra_versions) #Also tried converting to a tuple
ra_plot.set_title("blah blah words")
print(ra_versions)

# Only want x ticks on the dates we supplied, and want them to display AS dates
ra_plot.set_xticks(x)
ra_plot.set_xticklabels([date.strftime("%m-%d") for date in dates])

plt.show()

ra_ysは多次元配列です:

[[ 2  2  2  2  2  2  2  2  2  2  1]
 [ 1  1  1  1  1  1  1  1  1  1  1]
 [ 1  1  1  1  1  1  1  1  1  1  1]
 [53 52 51 50 50 49 48 48 48 48 47]
 [18 19 20 20 20 20 21 21 21 21 21]
 [ 0  0 12 15 17 18 19 19 19 19 22]
 [ 5  5  3  3  3  3  3  3  3  3  3]
 [ 4  4  3  3  2  2  2  2  2  2  2]
 [14 14  6  4  3  3  2  2  2  2  2]
 [ 1  1  1  1  1  1  1  1  1  1  1] 
 [ 1  1  1  1  1  1  1  1  1  1  1]
 [ 1  1  1  1  1  1  1  1  1  1  1]
 [ 2  2  2  2  2  2  2  2  2  2  2]
 [ 1  1  1  1  1  1  1  1  1  1  1]
 [ 1  1  1  1  1  1  1  1  1  1  1]
 [ 3  3  2  2  2  2  2  2  2  2  2]]                                           

xいくつかの日付です:[734969.0, 734970.0, 734973.0, 734974.0, 734975.0, 734976.0, 734977.0, 734978.0, 734979.0, 734980.0, 734981.0]

ra_versionsリストです:['4.5.2', '4.5.7', '4.5.8', '5.0.0', '5.0.1', '5.0.10', '5.0.7', '5.0.8', '5.0.9', '5.9.105', '5.9.26', '5.9.27', '5.9.29', '5.9.31', '5.9.32', '5.9.34']

私は何か間違ったことをしていますか?スタック プロットに凡例がないことはありますか?

編集: プロットのハンドルとラベルを印刷しようとしたところ、2 つの空のリストが表示されました ( [] []):

handles, labels = theplot.get_legend_handles_labels()
print(handles,labels)

次に、プロキシ ハンドルの次のコードを使用して同じ図をテストしたところ、機能しました。したがって、ハンドルの欠如が問題のようです。

p = plt.Rectangle((0, 0), 1, 1, fc="r")
theplot.legend([p], ['test'])

問題は、スタック プロットの色に一致する可変数のプロキシ ハンドルを生成するにはどうすればよいかということです。

4

1 に答える 1

0

これは、凡例を取得するための最終的な (よりクリーンな) アプローチです。ハンドルがないので、ラインごとにプロキシ アーティストを生成します。理論的には、色が再利用されるケースを処理できますが、混乱を招く可能性があります。

def plot_version_data(title, dates, versions, version_ys, savename=None):
    print("Prepping plot for \"{0}\"".format(title))
    fig = plt.figure()    
    theplot = fig.add_subplot(111)

    # Our x axis is going to be the dates, but we need them as numbers
    x = [date2num(date) for date in dates]

    # Use these colors
    colormap = "bgrcmy"
    theplot.stackplot(x, version_ys, colors=colormap)

    # Make some proxy artists for the legend
    p = []
    i = 0
    for _ in versions:
        p.append(plt.Rectangle((0, 0), 1, 1, fc=colormap[i]))
        i = (i + 1) % len(colormap)

    theplot.legend(p, versions)
    theplot.set_ylabel(versions) # Cheating way to handle the legend

    theplot.set_title(title)

    # Setup the X axis - rotate to keep from overlapping, display like Oct-16,
    # make sure there's no random whitespace on either end
    plt.xticks(rotation=315)
    theplot.set_xticks(x)
    theplot.set_xticklabels([date.strftime("%b-%d") for date in dates])
    plt.xlim(x[0],x[-1])

    if savename:
        print("Saving output as \"{0}\"".format(savename))
        fig.savefig(os.path.join(sys.path[0], savename))
    else:
        plt.show()
于 2013-04-24T19:31:05.887 に答える