如何绘制时间序列和分组年份?

时间:2020-05-12 16:49:58

标签: pandas matplotlib plot

我有一个如下所示的数据框,日期是索引。如何绘制显示每一年的一条线的时间序列?我已经尝试过df.plot(figsize=(15,4)),但这给了我一行。

Date       Value
2008-01-31   22
2008-02-28   17
2008-03-31   34
2008-04-30   29
2009-01-31   33
2009-02-28   42
2009-03-31   45
2009-04-30   39 
2019-01-31   17
2019-02-28   12  
2019-03-31   11 
2019-04-30   12
2020-01-31   24
2020-02-28   34
2020-03-31   43
2020-04-30   45

2 个答案:

答案 0 :(得分:0)

您可以只使用年份进行分组。

df = pd.read_clipboard()
df = df.set_index(pd.DatetimeIndex(df['Date']))
df.groupby(df.index.year)['Value'].plot()

enter image description here

答案 1 :(得分:0)

如果要将年份用作一系列数据并每天进行比较:

jaxbContext = JAXBContext.newInstance(UICMessage.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.setProperty("com.sun.xml.bind.characterEscapeHandler", (CharacterEscapeHandler) (ch, start, length, isAttVal, out) -> out.write(ch, start, length));
StringWriter sw = new StringWriter();
jaxbMarshaller.marshal(request, sw);
String xmlString = sw.toString();
String hackedString = xmlString.substring(xmlString.indexOf("<TrainCompositionMessage"), xmlString.indexOf("</message"));
jaxbContext = JAXBContext.newInstance(TrainCompositionMessage.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
TrainCompositionMessage errorMessage = (TrainCompositionMessage) unmarshaller.unmarshal(new StringSource(hackedString));

here