曲线末端的标签(matplotlib-seaborn)

时间:2018-10-29 17:53:07

标签: python matplotlib seaborn

我有多个这种格式的数据帧:

year    count   cum_sum
2001    5   5
2002    15  20
2003    14  34
2004    21  55
2005    44  99
2006    37  136
2007    55  191
2008    69  260
2009    133 393
2010    94  487
2011    133 620
2012    141 761
2013    206 967
2014    243 1210
2015    336 1546
2016    278 1824
2017    285 2109
2018    178 2287

我已经生成了以下图: enter image description here

以下代码已用于此目的:

fig, ax = plt.subplots(figsize=(12,8))

sns.pointplot(x="year", y="cum_sum", data=china_papers_by_year_sorted, color='red')
sns.pointplot(x="year", y="cum_sum", data=usa_papers_by_year_sorted, color='blue')
sns.pointplot(x="year", y="cum_sum", data=korea_papers_by_year_sorted, color='lightblue')
sns.pointplot(x="year", y="cum_sum", data=japan_papers_by_year_sorted, color='yellow')
sns.pointplot(x="year", y="cum_sum", data=brazil_papers_by_year_sorted, color='green')

ax.set_ylim([0,2000])
ax.set_ylabel("Cumulative frequency")

fig.text(x = 0.91, y = 0.76, s = "China", color = "red", weight = "bold") #Here I have had to indicate manually x and y coordinates
fig.text(x = 0.91, y = 0.72, s = "South Korea", color = "lightblue", weight = "bold") #Here I have had to indicate manually x and y coordinates

plt.show()

问题在于,将文本添加到绘图中的方法无法识别数据坐标。因此,我不得不手动指示每个数据框标签的坐标(请参见“中国”和“韩国”)。有聪明的方法吗?我已经看到了使用“ .last_valid_index()”方法的示例。但是,由于无法识别数据坐标,因此无法正常工作。

1 个答案:

答案 0 :(得分:0)

您无需重复调用country并手动添加标签。而是在数据框中添加一个hue列以指示国家/地区,合并数据框,然后简单地将国家/地区用作# Add a country label to dataframe itself china_papers_by_year_sorted['country'] = 'China' usa_papers_by_year_sorted['country'] = 'USA' korea_papers_by_year_sorted['country'] = 'Korea' japan_papers_by_year_sorted['country'] = 'Japan' brazil_papers_by_year_sorted['country'] = 'Brazil' # List of dataframes with same columns frames = [china_papers_by_year_sorted, usa_papers_by_year_sorted, korea_papers_by_year_sorted, japan_papers_by_year_sorted, brazil_papers_by_year_sorted] # Combine into one dataframe result = pd.concat(frames) # Plot.. hue will make country name a label ax = sns.pointplot(x="year", y="cum_sum", hue="country", data=result) ax.set_ylim([0,2000]) ax.set_ylabel("Cumulative frequency") plt.show() 来绘制累积总数与年份的关系。

相反,请执行以下操作:

public function add()
{
    $business = $this->Businesses->newEntity();

    if ($this->request->is('post')) {

        $business = $this->Businesses->patchEntity($business, $this->request->getData());

        $business->set('hash');
        $business->set('active', 0);
        $business->set('slug');

        $profile = TableRegistry::getTableLocator()->get('Profiles')->get($this->_currentUser->profile_id);

        if ($this->Businesses->save($business)) {

            **$this->Businesses->Profiles->link($business, [$profile]);**
            $this->Flash->success(__('The business has been saved.'));

            return $this->redirect(['action' => 'index']);
        }
        else

        {
            debug($business->getErrors()); // debug
            $this->Flash->error(__('The business could not be saved. Please, try again.'));
        }
    }
    $this->set(compact('business'));
}

编辑:进行编辑以添加以下内容:如果您想对行本身进行注释而不是使用图例,则this existing question的答案表示如何对行末进行注释。