# Python入门之时间序列折线图

fig,ax = plt.subplots(col,row,figsize=(x,y))

g = sns.FacetGrid(data,col,row,hue,size,aspect)

import matplotlib.pyplot as plt

fig,ax = plt.subplots(figsize=(6,3))

plt.plot(test.data_date,test.ratio);

import seaborn as sns

g = sns.FacetGrid(test.sort_values(by = 'data_date'),size=3, aspect = 2)

g.map(plt.plot,'data_date','ratio',alpha=0.9)

1. 调整横轴显示

ax.set_xticks([])

ax.set_xticklabels([]);

g.set_xticklabels(step=2)

2.横轴旋转

ax.set_xticklabels([],rotation=45);

g.set_xticklabels(step=2,rotation = 45)

3.绘制垂直线

plt.vlines('2018-12-25',0, 0.15, colors = "b", linestyles = "dashed")

4.添加文本记录

plt.text('2018-12-21',0.16,'2018-12-25\n大幅提升')

plt.annotate('2018-12-25\n大幅提升', \

xy=('2018-12-25',0.16),\

xytext=('2018-12-29',0.17),\

arrowprops=dict(facecolor='black', shrink=0.05))

5. 进阶 多时间维度横轴

import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

import matplotlib.dates as dates

#数据

idx = pd.date_range('2011-05-01', '2011-07-01')

#画布

fig, ax = plt.subplots()

ax.plot_date(idx.to_pydatetime(), s, 'v-')

#横轴(日周)

interval=1))

#网格线

# 横轴(月年)

plt.tight_layout()

plt.show()

• 发表于:
• 原文链接https://kuaibao.qq.com/s/20190127G0NS5Y00?refer=cp_1026
• 腾讯「云+社区」是腾讯内容开放平台帐号（企鹅号）传播渠道之一，根据《腾讯内容开放平台服务协议》转载发布内容。

2019-12-12