Matplotlib绘制折线图

Matplotlib绘制折线图

利用Jupter Notebook 绘制折线图,主要介绍如何使用matplotlib库中的各种方法绘制折线图,以及对图形的修饰。

案例一:随机生成两地温度折线图
# 导入库
import matplotlib.pyplot as plt
import random

# 指定字体 雅黑
plt.rcParams['font.sans-serif'] = ['SimHei']

# 创建图形
plt.figure(figsize=(20,8),dpi=80)

# 准备数据
x = range(61)

y_shanghai = [random.uniform(15,18) for i in x]
y_beijing = [random.uniform(7,11) for i in x]

# 构造中文
x_ch = ['11点{}分'.format(i) for i in x]
y_ticks = range(40)

# 绘制折线图
plt.plot(x,y_shanghai,label='上海',color='r',linestyle='--')
plt.plot(x,y_beijing,label='北京',color='g',linestyle='-')

#修改刻度
plt.xticks(x[::5],x_ch[::5])
plt.yticks(y_ticks[::5])

# 增加描述信息
plt.xlabel('时间')
plt.ylabel('温度')

#增加标题
plt.title('上海和北京晚上11-12点温度变化折线图')

# 增加图例
plt.legend(loc='best')

plt.show()

在这里插入图片描述

案例二:11-30岁交友数量折线图
# 导入库
import matplotlib.pyplot as plt

# 设置字体
plt.rcParams['font.sans-serif'] = ['SimHei']

# 创建图形
plt.figure(figsize=(20,8),dpi=80)

# 准备数据
x = [i for i in range(11,31)]
y1 = [1,0,1,1,2,4,3,2,3,4,4,5,6,5,4,3,3,1,1,1]
y2 = [1,0,3,1,2,2,3,3,2,1,2,1,1,1,1,1,1,1,1,1]

# 添加中文
x_ch = ['{}岁'.format(i) for i in range(11,31)]

#绘制图形
plt.plot(x_ch,y1,label='自己',color='r',linestyle='--')
plt.plot(x_ch,y2,label='同桌',color='b',linestyle='-')

# 添加坐标轴描述信息
plt.xlabel('年龄:岁')
plt.ylabel('朋友数量:个')

# 添加标题
plt.title('11-30岁交友数量折线图')

# 增加图例
plt.legend(loc='best')

plt.show()

在这里插入图片描述

案例三:公众号阅读量直线图
# 导入模块
import pandas as pd
import matplotlib.pyplot as plt

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

# 读取需要绘图的数据
article_reading = pd.read_csv(r'E:\PythonData\exercise_data\wechart.csv')
article_reading.date = pd.to_datetime(article_reading.date)


# 取出8月份至9月28日的数据
sub_data = article_reading.loc[article_reading.date >= '2017-08-01' ,:]

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

# 绘图
plt.plot(sub_data.date, # x轴数据
         sub_data.article_reading_cnts, # y轴数据
         linestyle = '-', # 折线类型
         color = 'g', # 折线颜色
         marker = 'o', # 点的形状
) 

# 添加标题和坐标轴标签
plt.title('公众号每天阅读人数趋势图')
plt.xlabel('日期')
plt.ylabel('人数')

# 剔除图框上边界和右边界的刻度
plt.tick_params(top = 'off', right = 'off')

# 为了避免x轴日期刻度标签的重叠,设置x轴刻度自动展现,并且45度倾斜
fig.autofmt_xdate(rotation = 45)

# 显示图形
plt.show()

在这里插入图片描述

案例四:公众号阅读人数和阅读人次趋势图
# 导入库
import matplotlib.pyplot as plt

# 设置字体 雅黑
plt.rcParams['font.sans-serif'] = ['SimHei']

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

# 绘图--阅读人数趋势
plt.plot(sub_data.date, # x轴数据
         sub_data.article_reading_cnts, # y轴数据
         linestyle = '-', # 折线类型
         color = 'steelblue', # 折线颜色
         marker = 'o', # 点的形状
         label = '阅读人数') # 添加标签

# 绘图--阅读人次趋势
plt.plot(sub_data.date, # x轴数据
         sub_data.article_reading_times, # y轴数据
         linestyle = '-', # 折线类型
         color = '#ff9999', # 折线颜色
         marker = 'o', # 点的形状
         label = '阅读人次') # 添加标签

# 添加标题和坐标轴标签
plt.title('公众号每天阅读人数和人次趋势图')
plt.xlabel('日期')
plt.ylabel('人数')

# 显示图例
plt.legend()

# 显示图形
plt.show()

在这里插入图片描述
数据链接: https://pan.baidu.com/s/1ygH_wqeCagE_Eu0B6E4jQQ.
提取码:5rfd

总结:折线图的绘制相对较为简单,注意在导入数据时,需要输入数据的绝对路径,同时需要通过r进行反斜杠的转义操作。
  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜的乄第七章

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值