Python学习之matplotlib 绘制折线图详解

本文介绍了Python中使用matplotlib库绘制折线图和散点图的详细步骤,包括模块安装、简单折线图、定制标签与线条粗细、设置X坐标以及绘制散点图的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文和大家分享的主要是 python开发中matplotlib  绘制折线图相关内容,一起来看看吧,希望对大家学习和使用这部分内容有所帮助。
   matplotlib
   1、安装matplotlib
  ① linux 系统安装
  安装 matplotlib 模块
  $ sudo apt-get install python3-matplotlib#  如果是 python2.7  执行如下命令
  $ sudo apt-get install python-matplotlib#  如果你安装较新的 Python ,安装模块一乐的一些库
  $ sudo apt-get install python3.5-dev python3.5-tk tk-dev
  $ sudo apt-get install libfreetype6-dev g++#  再使用 pip 来安装模块
  $ pip install --user matplotlib
  ② OS 系统中安装
  安装 matplotlib 模块
  $ pip install --user matplotlib
  ③ windows 系统中安装
  安装 matplotlib 模块
  $pip3 install matplotlib
  进入终端执行import matplotlib  不报错表示执行成功
   2、绘制简单的折线图
  ①  创建 mpl_squares.py 文件:
   import matplotlib.pyplot  as plt     #  导入模块
  squares = [1,4,9,16,25]         #  指定列表 Y 坐标为列表中的值, X 坐标为列表下标
  plt.plot(squares)           #  传入列表
  plt.show()                  #  输出图像
  绘图:
 
    ②  修改标签文字和线条粗细:
   import matplotlib.pyplot  as plt     #  导入模块
  squares = [1,4,9,16,25]         #  指定列表 Y 坐标为列表中的值, X 坐标为列表下标
  plt.plot(squares,linewidth=5)           # linewidth 决定绘制线条的粗细
  plt.title('Square Numbers',fontsize=24)     #  标题
  plt.xlabel('Vaule',fontsize=14)
  plt.ylabel('Square of Vaule',fontsize=14)
  plt.tick_params(axis='both',labelsize=14)      #  刻度加粗
  plt.show()                  #  输出图像
  绘图:
 
   ③  校正图形(设定 X 坐标):
   import matplotlib.pyplot  as plt     #  导入模块
  squares = [1,4,9,16,25]         #  指定列表 Y 坐标为列表中的值
  input_values = [1,2,3,4,5]
  plt.plot(input_values,squares,linewidth=5)           # linewidth 决定绘制线条的粗细
  plt.title('Square Numbers',fontsize=24)     #  标题
  plt.xlabel('Vaule',fontsize=14)
  plt.ylabel('Square of Vaule',fontsize=14)
  plt.tick_params(axis='both',labelsize=14)      #  刻度加粗
  plt.show()                  #  输出图像
  绘图:
 
   3、绘制散点图
  ①  创建 scatter_sqares.py:
   import matplotlib.pyplot  as plt
  plt.scatter(2,4,s=200)      #X 坐标 2 Y 坐标 4 S=200  点大小
  plt.title('Square Numbers',fontsize=24)     #  标题
  plt.xlabel('Vaule',fontsize=14)
  plt.ylabel('Square of Vaule',fontsize=14)
  plt.tick_params(axis='both',labelsize=14)      #  刻度加粗
  plt.show()                  #  输出图像
  绘图:
 
   ②  绘制一系列点:
   import matplotlib.pyplot  as plt
  x_values = [1,2,3,4,5]      #  指定 X
  y_values = [1,4,9,16,25]        #  指定 Y
  plt.scatter(x_values,y_values,s=100)
  --snip---
  plt.show()                  #  输出图像
  绘图
 
来源: 博客园
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值