Stackplot熊猫groupby

时间:2018-09-15 17:29:32

标签: python pandas matplotlib

我想对数据使用stackplot。但是,需要首先将其分组。我还希望它能像使用df.plot(legend = True)

一样从groupby中获取图例。
Schema::create('services', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('type_id')->unsigned()->nullable();
            $table->integer('business_id')->unsigned();
            $table->foreign('business_id')->references('id')->on('businesses')->onDelete('cascade');
            $table->string('slug');
            $table->string('name');
            $table->integer('duration')->unsigned()->default(60);
            $table->string('description');
            $table->string('prerequisites')->nullable();
            $table->string('color', 12)->nullable();
            $table->foreign('type_id')->references('id')->on('service_types')->onDelete('cascade');
            $table->softDeletes();
            $table->Timestamps();

            $table->unique(['business_id', 'slug']);
        });

我使用了上面的代码,但由于它发送了错误,我猜它是错误的。

1 个答案:

答案 0 :(得分:1)

IIUUC,尽管您需要首先旋转DataFrame.plot.area(),但是使用DataFrame可以得到所需的内容,以便每一列都是一个不同的区域,而索引是日期。

import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(9, 6))
plt.style.use('fivethirtyeight')

df = df.set_index('Date')
_ = df.pivot(columns='Region', values='Dengue_Cases').plot.area(ax=ax)

_ = plt.legend(bbox_to_anchor=(1.04,1), loc="upper left")
plt.show()

enter image description here