Python matplotlib.pyplot 模块,figlegend() 实例源码

我们从Python开源项目中,提取了以下1个代码示例,用于说明如何使用matplotlib.pyplot.figlegend()

项目:SFBIStats    作者:royludo    | 项目源码 | 文件源码
def proportion_stackplot(df, output=None, xlabel='', ylabel='', title=''):
    """
    Pandas has a bug with it's plot(kind='area'). When moving the legend, the colors disappear.
    By default with pandas the legend sits on the graph, which is not a desired behavior.
    So this function imitates panda's formatting of an area plot, with a working well-placed legend.

    Parameters
    ----------
    df : pandas.Dataframe
        x must be a date series.
        y is any number of columns containing percentages that must add up to 100 for each row.
    output : string
        the complete output file name
    xlabel : string
    ylabel : string
    title : string

    Returns
    -------

    """

    column_names = df.columns.values
    x = df.index.date
    column_series_list = []
    for cname in column_names:
        column_series_list.append(pd.Series(df[cname]).tolist())
    fig, ax = plt.subplots()
    polys = ax.stackplot(x, column_series_list, alpha=0.8)
    ax.set_ylim([0, 100])
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)
    legends = []
    for poly in polys:
        legends.append(plt.Rectangle((0, 0), 1, 1, facecolor=poly.get_facecolor()[0]))
    # don't try to understand the legend displacement thing here. Believe me. Don't.
    plt.figlegend(legends, column_names, loc=7, bbox_to_anchor=(1.2 + legend_displace_factor(column_names), 0.5))
    plt.title(title, y=1.08)
    date_fmt_year = mDates.DateFormatter('%b\n%Y')
    date_fmt_month = mDates.DateFormatter('%b')
    ax.xaxis.set_major_locator(mDates.YearLocator())
    ax.xaxis.set_major_formatter(date_fmt_year)
    ax.xaxis.set_minor_locator(mDates.MonthLocator(bymonth=7))
    ax.xaxis.set_minor_formatter(date_fmt_month)
    plt.savefig(output, bbox_inches='tight')
    plt.close()