如何根据值而不是列来绘制2行 [英] How to Plot 2 lines based on the value not column

查看:44
本文介绍了如何根据值而不是列来绘制2行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集:

ReviewDate_year,ReviewDate_month,Sales
 2010,11,1
 2011,02,2
 2011,11,1
 2011,12,6
 2012,01,11
 2012,02,8
 2012,03,3
 2012,04,1
 2012,05,8
 2012,06,3
 2012,07,1
 2012,08,2
 2012,09,1
 2012,11,1
 2012,12,8
 2013,01,2
 2013,02,2
 2013,03,4
 2013,04,4
 2013,05,7
 2013,06,5
 2013,07,6
 2013,08,4
 2013,09,4
 2013,10,5
 2013,11,3
 2013,12,5
 2014,01,9
 2014,02,4
 2014,03,8
 2014,04,7
 2014,05,3
 2014,06,7
 2014,07,1

如何在不同的行中按月份和不同年份绘制销售额

How do I plot Sales by month and different year in different line

我当然可以在一年内完成

I can certainly do it by year

df_2013 = df_monthlycount[df_monthlycount['ReviewDate_year'] == 2013]
df_2014 = df_monthlycount[df_monthlycount['ReviewDate_year'] == 2014]

df_2013.plot(x='ReviewDate_month',y='ProductId')

plt.show()

但是如何在单个图表中制作不同年份的线条?

but how can I make lines of different year in a single chart?

推荐答案

您可以使用 groupby 绘图.不必为每个组创建一个 ax ,而是可以指定一个 ax 并将所有内容绘制在该 ax 上:

You can use groupby plot. Instead of creating one ax for each group, you can specify an ax and have everything plotted on that ax:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.cm as cm

ax = plt.subplot(111)
df[df.ReviewDate_year.isin([2012,2013])].groupby('ReviewDate_year').plot(y='Sales', x='ReviewDate_month', kind='line', ax=ax)
L = plt.legend()
_ = [plt.setp(item, 'text', T) for item, T in zip(L.texts, ['2012','2013'])]
_ = ax.set_xticks(df.ReviewDate_month.unique())

这篇关于如何根据值而不是列来绘制2行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆