matplotlib——折线图

matplotlib–折线图

折线图的概念:

该方法通常将数据绘制在折线图中,可以显示随时间变化,数据变化的趋势。
因此非常适用于显示在相等时间间隔下数据的走向变化。

折线图的做法:

示例:通过引入某商场一年中每天销售量的数据记载,用折线图来观察不同时期销售额的变化。首先同样的操作,要有数据,我们把已有的数据读取进来运行输出。


示例代码:

@skl--python

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("sale.csv")
df

运行结果:
这里写图片描述

需要稍加处理:

@skl--python

df["date"] = pd.to_datetime(df["date"])    #将date转换成日期格式
df.head()

看下结果:

这里写图片描述

现在,数据有了也简单的处理过了,那么从一个简单的折线图开始。下面我们采用的是面向对象的作图方式,之前有提过这种作图方式较有逻辑性,所以推荐运用,还是看代码比较直观吧!

示例代码:

@skl--python

fig,ax = plt.subplots(figsize = (8,5) , dpi = 80)
ax.plot(df["date"] , df["sale"])
ax.set(xlabel = "date" , ylabel = "sale" ,title = "sale_plot")
plt.show()

(以上代码中的语法及参数前两篇已经阐述)

运行结果:

这里写图片描述

补充知识:
对于画折线图会涉及到一些新的参数,通过结合实例来对这些参数的理解,下面将进行展示。

1.参数介绍
(1)针对线条的处理

  • 线条类型
    参数:linestyle或者ls,可以取值:(1)”-” (2)”–” (3)”-.” (4)”:”

  • 线条粗细
    参数:linewidth或lw,可自行设置

  • 线条颜色
    参数:color或c
    颜色名称或简写 blue/b green/g red/r black/k white/w yellow/y
    cyan/c magenta/m
    (r, g, b),取值为[0, 1]之间(分母为255)

示例代码:

@skl--python

fig,ax = plt.subplots(figsize = (8,5) ,dpi = 80)
ax.plot(df["date"] , df["sale"] , linestyle = "--" , linewidth = 2 , color = (222/255,89/255,155/255))
#线条类型为“--”,线条宽度为2,线条颜色为RGB(222,89,155)
ax.set(xlabel = "date" , ylabel = "sale" , title = "plot")
plt.show()

运行结果:

这里写图片描述
(2)针对数据标记的处理

  • 参数marker: 数据标记的类型
  • 参数markeredgecolor 或 mec: 数据标记的边界颜色
  • 参数markeredgewidth 或 mew:数据标记的宽度
  • 参数markerfacecolor 或 mfc: 数据标记的填充色

示例代码:

@skl--python

fig , ax = plt.subplots(figsize = (8,5) ,dpi = 80)
ax.plot(df["date"] , df["sale"] ,marker = "o" ,mec = "k" , mfc = "w" , mew = 0.5)
#添加圆圈形状的数据标记,数据标记的边框颜色为绿色,填充色为白色
ax.set(xlabel = "date" ,ylabel = "sale" ,title = "plot")
plt.show()

运行结果:

这里写图片描述

(3)增加图例

  • legend()函数中的loc参数:图例的位置
    可取的值有:
    0: “best”
    1: “upper right”
    2: “upper left”
    3: “lower left”
    4: “lower right”
    5: “right”
    6: “center left”
    7: “center right”
    8: “lower center”
    9: “upper center”
    10: “center”

示例代码:

@skl--python

fig ,ax = plt.subplots(figsize = (8,5) , dpi = 80)
ax.plot(df["date"] , df["sale"] , color = "g" , label = "sale")    #增加图例时,后面多加了一个参数label=""
ax.set(xlabel = "date" , ylabel = "sale" ,title = "plot")
ax.legend(loc = "best")   #best为最适合位置,自动寻找空白地方最大的位置
plt.show()

运行结果:

这里写图片描述
(4)网格线

  • 在绘制的折线图上,加网格线作为图板背景。

示例代码:

@skl--python

fig , ax = plt.subplots(figsize = (8,5) , dpi =80)
ax.plot(df["date"] , df["sale"] , color = (89/255 , 89/255 , 89/255) , label = "sale")
ax.set(xlabel = "date" , ylabel = "sale" ,title = "plot")
ax.legend(loc = "best")
ax.grid(True) #添加网格线
plt.show()

运行结果:

这里写图片描述

2.在一个图中画多条线
示例代码:

@skl--python

fig , ax = plt.subplots(figsize = (8,5) , dpi =80)
ax.plot(df["date"] , df["sale"] , label = "sale")
ax.plot(df["date"] , df["sale"] +np.random.randint(1000,5000) , label = "more sale")
ax.set(xlabel = "date" , ylabel = "sale" , title ="plot")
ax.legend()
plt.show()

其中randint()函数:
randint(low[, high, size]),返回随机的整数,位于半开区间[low,high)。

运行结果:

这里写图片描述

  • 3
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值