脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python matplotlib实现折线图的绘制

Python matplotlib实现折线图的绘制

2022-10-27 11:21渴望成为寂寞胜者 Python

Matplotlib作为Python的2D绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形。本文将利用Matplotlib库绘制折线图,感兴趣的可以了解一下

官网: https://matplotlib.org

一、版本

# 01 matplotlib安装情况 
import matplotlib 
matplotlib.__version__

 

二、图表主题设置

请点击:图表主题设置

 

三、一次函数

import numpy as np 
from matplotlib import pyplot as plt 
# 如何使用中文标题
plt.rcParams['font.sans-serif']=['Microsoft YaHei'] # 使用微软雅黑的字体
x = np.arange(1,11) 
y =  2  * x +  5  # 图片显示的是这个公式
plt.title("Matplotlib展示") 
plt.xlabel("x轴") 
plt.ylabel("y轴") 
plt.plot(x,y) 
plt.show()

Python matplotlib实现折线图的绘制

 

四、多个一次函数

创建一个关于电影票房的图表:

films=['穿过寒冬拥抱你','反贪风暴5:最终章','李茂扮太子','误杀2','以年为单位的恋爱','黑客帝国:矩阵重启','雄狮少年','魔法满屋','汪汪队立大功大电影','爱情神话']
regions=['中国','英国','澳大利亚','美国','美国','中国','英国','澳大利亚','美国','美国']
bos=['61,181','44,303','42,439','22,984','13,979','61,181','44,303','41,439','20,984','19,979']
persons=['31','23','56','17','9','31','23','56','17','9']
prices=['51','43','56','57','49','51','43','56','57','49']
showdate=['2022-12-03','2022-12-05','2022-12-01','2022-12-02','2022-11-05','2022-12-03','2022-12-05','2022-12-01','2022-12-02','2022-11-05']
ftypes=['剧情','动作','喜剧','剧情','剧情','爱情','动作','动画','动画','动画']
points=['8.1','9.0','7.9','6.7','3.8','8.1','9.0','7.9','6.7','3.8']
filmdescript={
  'ftypes':ftypes,
  'bos':bos,
  'prices':prices,
  'persons':persons,
  'regions':regions,
  'showdate':showdate,
  'points':points
}
import numpy as np
import pandas as pd
cnbo2021top5=pd.DataFrame(filmdescript,index=films)
cnbo2021top5[['prices','persons']]=cnbo2021top5[['prices','persons']].astype(int)
cnbo2021top5['bos']=cnbo2021top5['bos'].str.replace(',','').astype(int)
cnbo2021top5['showdate']=cnbo2021top5['showdate'].astype('datetime64')
cnbo2021top5['points']=cnbo2021top5['points'].apply(lambda x:float(x) if x!='' else 0)

关于cnboo1.xlsx,我放在我的码云里,需要的朋友自行下载:cnboo1.xlsx

# 读取并初步整理数据集
import pandas as pd 
cnbodf=pd.read_excel('cnboo1.xlsx')
cnbodfsort=cnbodf.sort_values(by=['BO'],ascending=False)

Python matplotlib实现折线图的绘制

def mkpoints(x,y): # 编写points评分 
  return len(str(x))*(y/25)-3

cnbodfsort['points']=cnbodfsort.apply(lambda x:mkpoints(x.BO,x.PERSONS),axis=1)

Python matplotlib实现折线图的绘制

cnbodfsort.to_excel("cnbodfsort.xlsx",index=False) # 创建一个Excel文件
from matplotlib import pyplot as plt 
plt.rcParams['font.sans-serif']=['Microsoft YaHei'] # 使用微软雅黑的字体
plt.title("票房2021TOP5") 
plt.xlabel("x轴") 
plt.ylabel("y轴")
x=cnbo2021top5.persons.sort_values()
y=cnbo2021top5.prices.sort_values()
plt.plot(x,y,marker=".",markersize=20,color='red',linewidth=4,markeredgecolor='blue')
plt.show()

Python matplotlib实现折线图的绘制

# 折线图进阶
from matplotlib import pyplot as plt 
plt.rcParams['font.sans-serif']=['Microsoft YaHei'] # 使用微软雅黑的字体
plt.title("中国票房2021TOP5") 
plt.plot(bo,prices,label='票房与票价')
plt.plot(bo,persons,label='票房与人次')
plt.plot(bo,points,label='票房与评价')
plt.legend() # 显示标签
plt.xlabel('票房') # 横坐标轴
plt.ylabel('行情') # 纵坐标轴
plt.show()

Python matplotlib实现折线图的绘制

更改一下版式

# 折线图进阶
from matplotlib import pyplot as plt 
plt.rcParams['font.sans-serif']=['Microsoft YaHei'] # 使用微软雅黑的字体
plt.title("中国票房2021TOP5") 
plt.plot(bo,prices,'r^--',label='票房与票价')
plt.plot(bo,persons,'g*-',label='票房与人次')
plt.plot(bo,points,color='blue',marker='o',markersize=10,label='票房与评价')
plt.legend() # 显示标签
plt.xlabel('票房') # 横坐标轴标题
plt.ylabel('行情') # 纵坐标轴标题
plt.show()

Python matplotlib实现折线图的绘制

 

五、填充折线图

填充折线图:当确定一条数据线上面的一点的时候,能够将该点的上下两部分分别使用不同的颜色填充。

dev_x=[25,26,27,28,29,30] # 开发者的年龄
dev_y=[7567,8789,8900,11560,16789,25231] #收入情况
py_dev_y=[5567,6789,9098,15560,20789,23231] # python开发者
js_dev_y=[6567,7789,8098,12356,14789,20231] # java开发者
devsalary=pd.DataFrame([dev_x,dev_y,py_dev_y,js_dev_y])
devsalaryT=pd.DataFrame(devsalary.values.T,columns=["Age","Dev","Python","Java"])
# 绘制带阴影的折线图
from matplotlib import pyplot as plt 
plt.style.use('classic')
plt.figure(figsize=(7,4))
plt.rcParams['font.sans-serif']=['Microsoft YaHei'] # 使用微软雅黑的字体
plt.title("开发人员薪资情况") 

baseline=10000

plt.plot(devsalaryT["Age"],devsalaryT["Dev"],label="总体薪资")
plt.plot(devsalaryT["Age"],devsalaryT["Python"],label="Python薪资") # 如果没有label是不会显示legend的数据标签的

plt.fill_between(devsalaryT["Age"],devsalaryT["Python"],baseline,where=(devsalaryT["Python"]>baseline),interpolate=True,color='yellow')
plt.fill_between(devsalaryT["Age"],devsalaryT["Python"],baseline,where=(devsalaryT["Python"]<=baseline),interpolate=True,color='red')

plt.grid()
plt.legend()
plt.show()

Python matplotlib实现折线图的绘制

# 绘制带阴影的折线图
from matplotlib import pyplot as plt 
plt.style.use('classic')
plt.figure(figsize=(7,4))
plt.rcParams['font.sans-serif']=['Microsoft YaHei'] # 使用微软雅黑的字体
plt.title("开发人员薪资情况") 

baseline=10000

plt.plot(devsalaryT["Age"],devsalaryT["Dev"],label="总体薪资")
plt.plot(devsalaryT["Age"],devsalaryT["Python"],label="Python薪资") # 如果没有label是不会显示legend的数据标签的

plt.fill_between(devsalaryT["Age"],devsalaryT["Python"],baseline,where=(devsalaryT["Python"]>baseline),interpolate=True,color='yellow',alpha=0.3)
plt.fill_between(devsalaryT["Age"],devsalaryT["Python"],baseline,where=(devsalaryT["Python"]<=baseline),interpolate=True,color='red',alpha=0.3) # alpha=0.3调整透明度

plt.grid()
plt.legend()
plt.show()

Python matplotlib实现折线图的绘制

# 绘制带阴影的折线图
from matplotlib import pyplot as plt 
plt.style.use('classic')
plt.figure(figsize=(7,4))
plt.rcParams['font.sans-serif']=['Microsoft YaHei'] # 使用微软雅黑的字体
plt.title("开发人员薪资情况") 

baseline=10000

plt.plot(devsalaryT["Age"],devsalaryT["Dev"],label="总体薪资")
plt.plot(devsalaryT["Age"],devsalaryT["Python"],label="Python薪资") # 如果没有label是不会显示legend的数据标签的

plt.fill_between(devsalaryT["Age"],devsalaryT["Python"],baseline,where=(devsalaryT["Python"]>baseline),interpolate=True,color='pink',alpha=0.7,label="高于10000元")
plt.fill_between(devsalaryT["Age"],devsalaryT["Python"],baseline,where=(devsalaryT["Python"]<=baseline),interpolate=True,color='purple',alpha=0.7,label="低于或等于10000元") # alpha=0.3调整透明度

plt.grid()
plt.legend()
plt.show()

Python matplotlib实现折线图的绘制

interpolate=True:将交叉的位置进行填充

# 绘制带阴影的折线图
from matplotlib import pyplot as plt 
plt.style.use('classic')
plt.figure(figsize=(7,4))
plt.rcParams['font.sans-serif']=['Microsoft YaHei'] # 使用微软雅黑的字体
plt.title("开发人员薪资情况") 

plt.plot(devsalaryT["Age"],devsalaryT["Dev"],label="总体薪资")
plt.plot(devsalaryT["Age"],devsalaryT["Python"],label="Python薪资") # 如果没有label是不会显示legend的数据标签的

plt.fill_between(devsalaryT["Age"],devsalaryT["Python"],devsalaryT["Dev"],where=(devsalaryT["Python"]>baseline),interpolate=True,color='green',alpha=0.7,label="高于总体")
plt.fill_between(devsalaryT["Age"],devsalaryT["Python"],devsalaryT["Dev"],where=(devsalaryT["Python"]<=baseline),interpolate=True,color='tomato',alpha=0.7,label="低于或等于总体") # alpha=0.3调整透明度

plt.grid()
plt.legend()
plt.show()

Python matplotlib实现折线图的绘制

到此这篇关于Python matplotlib实现折线图的绘制的文章就介绍到这了,更多相关Python matplotlib折线图内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/wxfighting/article/details/123299844

延伸 · 阅读

精彩推荐
  • PythonPython设计模式之外观模式实例详解

    Python设计模式之外观模式实例详解

    这篇文章主要介绍了Python设计模式之外观模式,结合实例形式详细分析了外观模式的概念、原理、用法及相关操作注意事项,需要的朋友可以参考下...

    Andy冉明9562021-05-17
  • Python详解Python中的 type()函数

    详解Python中的 type()函数

    type()是一个内置函数,可以很方便地查询对象数据类型;主要有两种用法:一个参数和三个参数,这篇文章主要介绍了Python中的 type()函数,需要的朋友可以...

    今天你吸猫了么9342022-08-24
  • PythonTensorflow使用tfrecord输入数据格式

    Tensorflow使用tfrecord输入数据格式

    这篇文章主要介绍了Tensorflow使用tfrecord输入数据格式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    ruyiweicas12442021-03-06
  • PythonPython学习笔记之变量、自定义函数用法示例

    Python学习笔记之变量、自定义函数用法示例

    这篇文章主要介绍了Python学习笔记之变量、自定义函数用法,结合实例形式分析了Python变量、自定义函数的概念、功能、使用方法及相关操作注意事项,需要...

    学习笔记6664922021-06-30
  • Pythonpython反编译教程之2048小游戏实例

    python反编译教程之2048小游戏实例

    这篇文章主要给大家介绍了关于python反编译教程之2048小游戏的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价...

    meteor_yh4682021-09-13
  • Pythonpython多进程和多线程究竟谁更快(详解)

    python多进程和多线程究竟谁更快(详解)

    下面小编就为大家带来一篇python多进程和多线程究竟谁更快(详解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    脚本之家15802020-11-13
  • PythonPython使用py2neo操作图数据库neo4j的方法详解

    Python使用py2neo操作图数据库neo4j的方法详解

    这篇文章主要介绍了Python使用py2neo操作图数据库neo4j的方法,结合实例形式详细分析了Python使用py2neo操作图数据库neo4j的具体步骤、原理、相关使用技巧与操...

    theVicTory6282020-04-26
  • Python聊聊Python中的GUI布局Tkinter

    聊聊Python中的GUI布局Tkinter

    现在极少有人会用上tkinter了,所以真正研究的人也就更少了,本来不想更新tkinter。看到很多人在学tkinter,其实用Python做布局,没有人这么干。但还是更新...

    Python之王7612020-12-01