Matplotlib 绘制折线图

Matplotlib学习笔记001 折线图绘制

1. plot()方法语句

matplotlib.pyplot.plot(*args, scalex=True, scaley=True, data=None, **kwargs)
参数:
scalex, scaley:bool类型, 默认值为True ,这些参数确定视图限制是否适应数据限制。值被传递给autoscale_view.
**kwargs :Line2D属性,可选kwargs用于指定线标签(用于自动图例)、线宽、抗锯齿、标记面颜色等属性

绘制线条可使用以下简略plot()语句:

plot([x], y, [fmt], *, data=None, **kwargs)  # 绘制一条线
plot([x], y, [fmt], [x2], y2, [fmt2], ..., **kwargs)  # 绘制多条线
参数说明:
[x] :数据点的水平坐标,可选类型,默认为range(len(y)),要和y轴长度相等,不可通过关键字传递(例如x=x1)
y : 数据点的垂直坐标,不可通过关键字传递(例如y=y1)
[fmt] :fmt = '[marker][line][color]' 格式字符串,由颜色、标记和线条部分组成,可选类型
data : 可索引对象,可选,使用时data内要有和x,y轴相同的标签列
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

2. 绘制单线条折线图

2.1 只传入 y 坐标

只传入一个参数,默认为y轴

y = [np.random.randint(0,10) for i in range(20)]  # 在0-9间随机选取20个整数
plt.plot(y)   # x默认产生range(0,len(y))的数

在这里插入图片描述

​ 注:因为y轴的值是随机的,每次图形都可能不一样

2.2 传入 x,y 坐标
y = [np.random.randint(0,20) for i in range(20)]
x = range(10,30) # x轴要和y轴长度相等,起始位置可以改变,x轴起始点为10
# x = range(0,20) # x轴起始点为0
plt.plot(x,y)

注:x,y参数不可通过关键词传递,只能通过位置参数传递

错误示例

y1 = [np.random.randint(0,20) for i in range(20)]
x1 = range(10,30) 
plt.plot(x=x1,y=y1)  # 这里用了关键词传递x1,y1
输出报错:
TypeError: plot got an unexpected keyword argument 'x'
2.3 通过data参数传递数据

data内的标签要和x,y轴标签一致

字典:

dict = {
   
    'x':range(10,30),
    'y':[np.random.randint(0,10) for x in range(20)]
}
plt.plot('x','y',data=dict)

这样看似没错,但运行会出现warning,

<ipython-input-60-afe01570d7b8>:6: RuntimeWarning: Second argument 'y' is ambiguous: could be a format string but is in 'data'; using as data.  If it was intended as data, set the format string to an empty string to suppress this warning.  If it was intended as a format string, explicitly pass the x-values as well.  Alternatively, rename the entry in 'data'.
  plt.plot('x','y',data=dict) 

警告出现的原因是dict字典内的y标签不明确,因为在参数fmt中,其[color]中有一个’y’(黄色)标签,要解决这种问题可以使用其他fmt中没有的标签

例如:

dict = {
   
    't':range(10,30),
    'u':[np.random.randint(0,10) for x in range(20)]
}
plt.plot('t','u',data=dict)

或者把fmt参数加进去,不需要使用fmt就给个空字符即可

dict = {
   
    'x':range(10,30),
    'y':[np.random.randint(0,10) for x in range(20)]
}
plt.plot('x','y','',data=dict)

在这里插入图片描述

DataFrame:

dict = {
   
    'x':range(10,30),
    'y':[np.random.randint(0,10) for x in range(20)]
}
df = pd.DataFrame(dict)
plt.plot('x','y','',data=df)

3. 绘制多条折线图

3.1 多次调用plot()方法
y1 = [np.random.randint(0,10) for x in range(15)]
x1 = range(10,25)
y2 = [np.random.randint(0,10) for x in range(10)]
x2 = range(10,20)
plt.plot(x1,y1)
plt.plot(x2,y2)

3.2 只调用一次plot()方法
plot([x], y, [fmt], [x2], y2, [fmt2], ..., **kwargs)  # 绘制多条线

示例:

y1 = [np.random.randint(0,10) for x in range(15)]
x1 = range(10,25)
y2 = [np.random.randint(0,10) for x in range(10)]
x2 = range(10,20)
plt.plot(x1,y1,x2,y2)

3.3 x或y为二维数组
x = [1, 
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值