9-matplotlib数据可视化--折线图

折线图主要是看内容随时间变化的趋势情况

# 导入模块
import pandas as pd
import matplotlib.pyplot as plt

# 设置中文编码和负号的正常显示
plt.rcParams['font.sans-serif'] = 'SimHei'
plt.rcParams['axes.unicode_minus'] = False

# 读取需要绘图的数据
df = pd.read_excel(r'user_analysis.xlsx')
df
时间新关注人数取消关注人数净增关注人数
0NaTNaNNaNNaN
12017-09-0187.07.080.0
22017-09-0285.020.065.0
32017-09-0390.010.080.0
42017-09-0488.017.071.0
52017-09-0578.019.059.0
62017-09-0681.08.073.0
72017-09-07224.011.0213.0
82017-09-08124.012.0112.0
92017-09-0959.06.053.0
102017-09-1072.015.057.0
112017-09-1194.010.084.0
122017-09-1268.014.054.0
132017-09-13110.07.0103.0
142017-09-14108.013.095.0
152017-09-15142.010.0132.0
162017-09-1675.013.062.0
172017-09-1785.09.076.0
182017-09-18114.015.099.0
192017-09-1995.010.085.0
202017-09-20118.013.0105.0
212017-09-2187.07.080.0
222017-09-22108.010.098.0
232017-09-23103.012.091.0
242017-09-2479.09.070.0
252017-09-2593.06.087.0
262017-09-26309.019.0290.0
272017-09-27394.019.0375.0
282017-09-28310.024.0286.0
292017-09-29756.013.0743.0
302017-09-30510.047.0463.0
df.dropna(axis=0)
时间新关注人数取消关注人数净增关注人数
12017-09-0187.07.080.0
22017-09-0285.020.065.0
32017-09-0390.010.080.0
42017-09-0488.017.071.0
52017-09-0578.019.059.0
62017-09-0681.08.073.0
72017-09-07224.011.0213.0
82017-09-08124.012.0112.0
92017-09-0959.06.053.0
102017-09-1072.015.057.0
112017-09-1194.010.084.0
122017-09-1268.014.054.0
132017-09-13110.07.0103.0
142017-09-14108.013.095.0
152017-09-15142.010.0132.0
162017-09-1675.013.062.0
172017-09-1785.09.076.0
182017-09-18114.015.099.0
192017-09-1995.010.085.0
202017-09-20118.013.0105.0
212017-09-2187.07.080.0
222017-09-22108.010.098.0
232017-09-23103.012.091.0
242017-09-2479.09.070.0
252017-09-2593.06.087.0
262017-09-26309.019.0290.0
272017-09-27394.019.0375.0
282017-09-28310.024.0286.0
292017-09-29756.013.0743.0
302017-09-30510.047.0463.0
# 设置图框的大小
fig = plt.figure(figsize=(8,4))
# 绘图
plt.plot(df['时间'], # x轴数据
         df['新关注人数'], # y轴数据
        ) 

# 添加标题和坐标轴标签
plt.title('公众号每天新增用户数')
plt.xlabel('日期')
plt.ylabel('新增人数')

fig.autofmt_xdate(rotation = 45)

# 显示图形
plt.show()
C:\Users\Dell\AppData\Roaming\Python\Python36\site-packages\pandas\plotting\_converter.py:129: FutureWarning: Using an implicitly registered datetime converter for a matplotlib plotting method. The converter was registered by pandas on import. Future versions of pandas will require you to explicitly register matplotlib converters.

To register the converters:
	>>> from pandas.plotting import register_matplotlib_converters
	>>> register_matplotlib_converters()
  warnings.warn(msg, FutureWarning)

[外链图片转存失败(img-RBvyZRJB-1567910005271)(output_4_1.png)]

# 设置图框的大小
fig = plt.figure(figsize=(8,4))

# 设置绘图风格
plt.style.use('seaborn')

# 设置中文编码和负号的正常显示
plt.rcParams['font.sans-serif'] = 'SimHei'
plt.rcParams['axes.unicode_minus'] = False

# 绘图
plt.plot(df['时间'], # x轴数据
         df['新关注人数'], # y轴数据
        ) 

# 添加标题和坐标轴标签
plt.title('公众号每天新增用户数')
plt.xlabel('日期')
plt.ylabel('新增人数')

fig.autofmt_xdate(rotation = 45)

# 显示图形
plt.show()

[外链图片转存失败(img-xTeuED5U-1567910005272)(output_5_0.png)]

#一张图上多个折线图

# 设置图框的大小
fig = plt.figure(figsize=(8,4))

# 设置绘图风格
plt.style.use('seaborn')

# 设置中文编码和负号的正常显示
plt.rcParams['font.sans-serif'] = 'SimHei'
plt.rcParams['axes.unicode_minus'] = False

# 绘图
plt.plot(
         df['新关注人数'], # y轴数据
         marker = 'o', # 点的形状
         markersize = 5, # 点的大小
         markerfacecolor='mediumpurple' # 点的填充色
        ) 

plt.plot(
         df['取消关注人数'], # y轴数据
         marker = 'o', # 点的形状
         markersize = 5, # 点的大小
         markerfacecolor='orangered' # 点的填充色
) 


# 添加标题和坐标轴标签
plt.title('公众号关注情况')
plt.xlabel('日期')
plt.ylabel('新增人数')

fig.autofmt_xdate(rotation = 45)

# 显示图例
plt.legend()
# 显示图形
plt.show()

[外链图片转存失败(img-u9LSznHE-1567910005273)(output_6_0.png)]

#一张图上多个折线图

# 设置图框的大小
fig = plt.figure(figsize=(8,4))

# 设置绘图风格
plt.style.use('seaborn')

# 设置中文编码和负号的正常显示
plt.rcParams['font.sans-serif'] = 'SimHei'
plt.rcParams['axes.unicode_minus'] = False

# 绘图
plt.plot(
         df['新关注人数'], # y轴数据
        ) 

plt.plot(
         df['净增关注人数'], # y轴数据
        ) 


# 添加标题和坐标轴标签
plt.title('公众号每天新增用户数')
plt.xlabel('日期')
plt.ylabel('新增人数')

fig.autofmt_xdate(rotation = 45)

# 显示图例
plt.legend()
# 显示图形
plt.show()

[外链图片转存失败(img-C0SXg9OT-1567910005274)(output_7_0.png)]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值