Python matplotlib实现折线图的绘制
作者:渴望成为寂寞胜者 发布时间:2022-05-03 11:14:58
标签:Python,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()
四、多个一次函数
创建一个关于电影票房的图表:
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)
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)
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()
# 折线图进阶
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()
更改一下版式
# 折线图进阶
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()
五、填充折线图
填充折线图:当确定一条数据线上面的一点的时候,能够将该点的上下两部分分别使用不同的颜色填充。
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()
# 绘制带阴影的折线图
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()
# 绘制带阴影的折线图
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()
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()
来源:https://blog.csdn.net/wxfighting/article/details/123299844


猜你喜欢
- 1、路径https://www.lfd.uci.edu/~gohlke/pythonlibs/PS:网上说有时候报404,解决办法是换浏览器
- *在起初pip install matplotlib时,主动安装到当时最新版本(matplotlib==3.3.2),在StackOverf
- 刷票一般要突破以下限制:1、验证码识别2、同一ip不可连续投票解决办法1、用tesseract工具,链接在此 https://code.go
- 该sql如下:Select /*+ parallel(src, 8) */ distinct src.systemn
- matplotlib窗口图标默认是matplotlib的标志,如果想修改怎么改呢?由于我选择的matplotlib后端是PyQT5,直接查看
- 一、目的之前在博文SQL Server数据库最小宕机迁移方案中提到了使用了完全备份+差异备份的功能完成了数据库的转移,但是这个方法在遇到了7
- 我们开发数据库应用时,常常需要用到模糊查询。如果同一个条件需要匹配很多字段怎么办呢?通常,程序员会每个字段都在SQL中“field like
- 1、目的:在Python中实现只读取扩展名为xlsx的文件解决方法:使用os模块。解决思路:1、确定目录2、循环遍历每一个文件3、筛选符合条
- 环境:centos7 python3.6测试网址:www.bai.com测试方式:抓取百度100次结果:aio: 10.7021474838
- 什么是Flyway?转载:https://blog.waterstrong.me/flyway-in-practice/Flyway is
- 如下所示:import cv2 # [1]导入OpenCv开源库import numpy as npimage_path = "F
- 本文为官方文档译文,官方文档现已非机器翻译 https://docs.microsoft.com/zh-cn/aspnet/core/fun
- inet_pton是一个IP地址转换函数,可以在将IP地址在“点分十进制”和“二进制整数”之间转换,而且inet_pton和inet_nto
- 1、缘由在日常开发中,我们经常需要监控应用程序的状态,及时发现问题并采取措施解决。而通过邮件发送报警信息则是一种常见的实现方式。Python
- declare @tt varchar(20) set @tt = 'monisubbouns' declare @int
- 今天在老师工作室做项目的时候,突然看到一个页面用了2种不同的传值类型,突然有了兴趣,想弄明白本质的区别,虽然以前用的知道2种的用法,但是还是
- 用XMLHTTP Post Form时的表单乱码有两方面的原因——Post表单数据时中文乱码;服务器Response被XMLHTTP不正确编
- 白噪声检验也称为纯随机性检验, 当数据是纯随机数据时,再对数据进行分析就没有任何意义了, 所以拿到数据后最好对数据进行一个纯随机性检验aco
- 使用python画图,发现生成的图片在console里。不仅感觉很别扭,很多功能也没法实现(比如希望在一幅图里画两条曲线)。想像matlab
- 一、连接MYSQL格式: mysql -h主机地址 -u用户名 -p用户密码1、 连接到本机上的MYSQL。首先打开DOS窗口,然后进入目录