Matplotlib入门

#折线图用来表示数据的变化

plt.plot(x,y)

#直方图用来统计连续性数据 无间隔

plt.hist(data数组,组数)

#条形图用来统计离散的数组 并且反映其变化 有间隔

plt.bar(x,y,width = 0.3)
plt.barh(y,x,height = 0.3)

#散点图用来xy轴之间的联系 趋势

 plt.scatter(x,y)

#导入pyplot
from matplotlib import pyplot as plt
#限制大小和像素
plt.figure(figsize = (20,8),dpi = 80)

#数据集中对应的x坐标 是一个迭代对象
x = rnage(2,26,2)#共12个数据
#数据集中对应的y坐标 是一个迭代对象
y = [15,13,14.5,17,20,25,26,26,24,22,18,15]#共12个数据
#x和y组成了12个数据点

#绘制x轴的刻度 
plt.xticks( range(2,25,1) )
#plt.xticks(x)
#plt.xticks( [i/2 for i in range(2,49)] )

#绘制y轴的刻度
plt.yticks( range(min(y),max(y)+1 )

#传入x y 通过plot绘制折线图
plt.plot(x,y)

#保存
plt.savefig("./t1.png")

#展示图形
plt.show()

 

 

from matplotlib import pyplot as plt
import random

#设置字体参数
font = {
    'family' : 'YouYuan',
    'weight' : 'bold',
    'size' : 12
}

#将字体参数应用到全局
plt.rc('font',**font)

#设置数据集的x y坐标
x = range(0,120)#共120个数(分钟)
y = [ random.randint(20,35) for i in range(120) ]

#设置图形大小
plt.figure( figsize = (20,8),dpi = 60 )

#设置刻度
#分别表示开始的位置和结束的位置(不包括)和步长
_x = x[::10]#表示每10分钟为1个刻度
_xtick_labels = ["现在是{}时{}分".format(i//60 + 10,i%60) for i in _x]
#刻度集合 刻度值 旋转程度
plt.xticks(_x,_xtick_labels,rotation = -90)

#绘制图片
plt.plot(x,y)

#绘制网格 设置透明度
plt.grid(alpha=0.3)

#设置标签
plt.xlabel("时间")
plt.ylabel("温度 单位(C)",labelpad = 20)#偏移
plt.tile("10点到12嗲每分钟的气温变化情况")

#展示图片
plt.show()

 

#coding = utf-8
from matplotlib import pyplot as plt
from matplotlib import font_manager
font = {
    'family':'YouYuan'
    'weight':'bold'
    'size':12
}
plt.rc('font',**font)

#give the data
y_1 = [1,0,1,1,2,4,3,2,3,4,4,5,6,5,4,3,3,1,1,1]
y_2 = [1,0,3,1,2,2,3,3,2,1,2,1,1,1,1,1,1,1,1,1]
x = range(11,31) 

#set the size of the graph
plt.figure(figsize=(20,8),dpi=80)

#set the scale of axis x and axis y
_x = x
_xticks_labels = ["{}岁".format(i) for i in _x]
plt.xticks(_x,_xticks_labels)#data from(data numbers) format
plt.yticks(range(0,7))

#draw the picture respectively
plt.plot(x,y_1,label = "自己",color = "orange",linestyle = ":")
plt.plot(x,y_2,label = "同桌",color = "cyan",linestyle = "--")

#set the grid and the tansparency
plt.grid(alpha = 0.3,color = "black",linestyle = "dotted")#color can be #00ff00

#put the position of the legend
plt.legend(loc="upper left")

#draw the picture
plt.show()

 

 

 

#coding = utf-8
from matplotlib import pyplot as plt
from matplotlib import font_manager
font = {
    'family':'YouYuan',
    'weight':'bold',
    'size' :10
}
plt.rc("font",**font)

#give the data
x_3 = range(1,29)
y_3 = [11,17,16,11,12,11,12,6,6,7,8,9,12,15,14,17,18,21,16,17,20,14,15,15,19,21,22,23]#28 numbers
x_10 = range(51,82)
y_10=[26,26,28,19,21,17,16,19,18,20,20,19,22,23,17,20,21,20,22,15,11,15,5,13,17,10,11,13,12,
13,6] #31 numbers

plt.figure(figsize=(20,8),dpi=80)

#in order to put the seperate x together put the data to 2 sides
_x = list(x_3)+list(x_10)

#use scatter to draw the picture
plt.scatter(x_3,y_3,label = "March")
plt.scatter(x_10,y_10,label = "Octomber")

#give the scale of axis x
_xtick_labels = ["3月{}日".format(i) for i in x_3]
_xtick_labels += ["10月{}日".format(i) for i in x_10]
plt.xticks(_x[::3],_xtick_labels[::3],rotation = 45)#every 3 days is one scale

#add the position of legend
plt.legend(loc="upper right")
#add the descreptive information
plt.xlabel("时间")
plt.ylabel("温度")
#add grid
plt.grid(alpha = 0.4)
#show the picture
plt.show()

 

 

 

from matplotlib import pyplot as plt
from matplotlib import font_manager
font ={
    'family':'YouYuan',
    'weight':'bold',
    'size':10
}
plt.rc("font",**font)
#You can make it more tidy by adding \n to the same position
a = ["电影1\n111","电影2","电影3\n2","电影3\n34","电影5\n4","电影6\n4","电影6\n66667","电影8","电影9","电影1\n0","电影1\n1","电影1\n2","电影1\n3","电影1\n4","电影1\n5","电影1\n6","电影1\n7","电影1\n8"]
b = [56.01,26.94,17.53,16.49,15.45,12.96,11.8,41.21,32.34,22.23,77.7,88.8,12,14,16,1,19,33]
plt.figure(figsize=(20,15),dpi=80)

#draw the bar picture
#if You want to change the pisition of x and y just do the tips 1 and 2
plt.bar(a,b,width = 0.3)
#1. plt.barh(a,b,height = 0.3)

#fix the scale of x
_xtick_labels = [i for i in a]
plt.xticks(a,_xtick_labels,rotation = 60)
#2.plt.yticks(a,_xtick_labels,rotation = 60)

plt.show()


 

 

#coding = utf-8
from matplotlib import pyplot as plt
from matplotlib import font_manager
font = {
    'family':'YouYuan',
    'weight':'bold',
    'size':10
}

#give the data axis x and y
a = ["猩球崛起3:终极之战","敦刻尔克","蜘蛛侠:英雄归来","战狼2"]
b_14 = [2358,399,2358,362]
b_15 = [12357,156,2045,168]
b_16 = [15746,312,4497,319]

#set the size of the graphic
plt.figure(figsize=(20,8),dpi=80)

#change the pos of the scale
x_14 = range(len(a))
x_15 = [i+bar_width for i in x_14]#clost to just right
x_16 = [i+bar_width for i in x_15]


#trans the data collections into 
plt.bar(x_14,b_14,width = bar_width,label="14日")#first one is scale
plt.bar(x_15,b_15,width = bar_width,label="15日")
plt.bar(x_16,b_16,width = bar_width,label="16日")

plt.legend(loc="upper left")

#give the scale parameters
plt.xticks(x_15,a)

plt.show()

 

 

 

from matplotlib import pyplot as plt
from matplotlib import font_manager
import random
font = {
    'family':'YouYuan',
    'weight':'bold',
    'size':1
}
random.seed(0)
a = [random.randint(70,154) for i in range(120)]
a.append(160)
a.append(60)

plt.figure(figsize = (20,80),dpi = 80)

#calculation 1.组数==极差/组距 2.组距==极差/组数
#1.
#fix the num_bins then calculate the distance
#tip: the (max(a)-min(a))/num_bins must == 0
num_bins=20
d = (max(a)-min(a))//num_bins
#draw the picture 
plt.hist(a,num_bins)#原始数组a和分组

#fix the scale of axis a
plt.xticks(range(min(a),max(a)+d,d))

plt.grid()
plt.show()


 

 

import random
from matplotlib import pyplot as plt
from matplotlib import font_manager
font = {
    'family':'YouYuan',
    'weight':'bold',
    'size':1
}
random.seed(0)
a = [random.randint(70,154) for i in range(120)]
a.append(160)
a.append(60)
plt.figure(figsize = (20,80),dpi = 80)
#计算组数 组数==极差/组距  
#将hist中第2个参数的值写为plt.xticks中的值 然后组距可以随便调
#其实就是一个刻度就是一组 然后用d的值分出来所需要的组数
d = 5
plt.hist(a,range(min(a),max(a)+d,d))#原始数组a和分组
#设置x轴的刻度
plt.xticks(range(min(a),max(a)+d,d))#刚好包含max(a)
#画网格
plt.grid()
plt.show()

 

#coding=UTF-8
from matplotlib import pyplot as plt
from matplotlib import font_manager
#这是统计过的所以用bar条形图 不连在一起
#没有统计过用hist直方图 连在一起
interval = [0,5,10,15,20,25,30,35,40,45,60,90]
width = [5,5,5,5,5,5,5,5,5,15,30,60]
quantity = [836,2737,3723,3926,3596,1438,3273,642,824,613,215,47]
plt.bar(range(len(quantity)),quantity,width=1)#width为1可以让条形图连在一起
#设置x轴的刻度
_x = [i*0.5 for i in range(len(interval)+1)]# 分成了多少份 
_xtick_labels = interval+[150] #额外加上一个元素
plt.xticks(_x,_xtick_labels)
plt.grid(alpha = 0.4)
plt.show()

 

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/794306.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Python大数据分析——K近邻模型(KNN)

Python大数据分析——K近邻模型 数学部分模型思想模型步骤距离度量指标欧氏距离曼哈顿距离余弦相似度 K值选择 代码部分函数示例1——知识掌握程度示例2——预测发电量 数学部分 模型思想 如图所示,模型的本质就是寻找k个最近样本,然后基于最近样本做“…

Qwen-7B推理教程【Python调用+web端部署】

前提 操作系统为Ubuntu22.04.4 LTS安装Anaconda(本人安装教程如下) Ubuntu22.04.4 LTS系统/安装Anaconda【GPU版】-CSDN博客 安装python3.9/pytorch/torchvision(本人安装教程如下) Ubuntu22.04.4系统/安装python3.9/pytorch/…

51单片机嵌入式开发:9、 STC89C52RC 操作LCD1602技巧

STC89C52RC 操作LCD1602技巧 1 代码工程2 LCD1602使用2.1 LCD1602字库2.2 巧妙使用sprintf2.3 光标显示2.4 写固定长度的字符2.5 所以引入固定长度写入方式: 3 LCD1602操作总结 1 代码工程 承接上文,在原有工程基础上,新建关于lcd1602的c和h…

前端项目本地的node_modules直接上传到服务器上无法直接使用(node-sasa模块报错)

跑 jekins任务的服务器不能连接外网下载依赖包,就将本地下载的 node_modules直接上传到服务器上,但是运行时node-sass模块报错了ERROR in Missing binding /root/component/node_modules/node-sass/vendor/linux-x64-48/binding.node >> 报错信息类…

深度学习设计模式之代理模式

文章目录 前言一、介绍二、详细分析1.核心组成2.实现步骤3.代码示例4.优缺点优点缺点 5.使用场景 总结 前言 代理模式是结构型设计模式,主要是为其他对象提供一种代理以控制对这个对象的访问。 一、介绍 代理模式(Proxy Pattern)是一种常用…

快速在springboot项目中应用EasyExcel

一、介绍 EasyExcel 是阿里巴巴开源的简化Excel文件读取和写入的开源库。主要的特点如下: 简单易用的API:EasyExcel提供简单API,隐藏处理Excel文件的底层细节。注解支持:支持使用注解将Java对象映射到Excel列,便于Ja…

《梦醒蝶飞:释放Excel函数与公式的力量》11.3 ISTEXT函数

第11章:信息函数 第三节 11.3 ISTEXT函数 11.3.1 简介 ISTEXT函数是Excel中的一个信息函数,用于检查指定单元格中的内容是否为文本。如果单元格内容是文本,则返回TRUE;否则返回FALSE。ISTEXT函数在数据验证、条件格式化和逻辑判…

【UE5.1】Chaos物理系统基础——06 子弹破坏石块

前言 在前面我们已经完成了场系统的制作(【UE5.1】Chaos物理系统基础——02 场系统的应用_ue5)以及子弹的制作(【UE5.1 角色练习】16-枪械射击——瞄准),现在我们准备实现的效果是,角色发射子弹来破坏石柱。…

如何利用扩散实现结构功能动态调控?

如何利用扩散实现结构功能动态调控? 利用扩散机制在生物打印结构内部创建特定空间梯度的方法,主要分为两个方面: 1. 形成形态发生素梯度 形态发生素的作用:形态发生素是能够诱导细胞响应的信号分子,常用于生物打印结…

探索GitHub上的两个革命性开源项目

在数字世界中,总有一些项目能够以其创新性和实用性脱颖而出,吸引全球开发者的目光。今天,我们将深入探索GitHub上的两个令人惊叹的开源项目:Comic Translate和GPTPDF,它们不仅改变了我们处理信息的方式,还极…

Debezium日常分享系列之:Debezium 3.0.0.Alpha1 Released

Debezium日常分享系列之:Debezium 3.0.0.Alpha1 Released 一、重大改变Java 和 Maven 要求已更改 二、新的特征和提高MongoDB 三、更多内容 Debezium 3 的第一个预发布版本 3.0.0.Alpha1。这个版本虽然比正常的预版本要小,但高度关注几个关键点&#xff…

docker中mysql设置lower_case_table_names配置的坑

前沿 今天在使用flowable流程框架的时候,遇到一个问题。需要配置MySQL数据库以实现表名大小写不敏感。本以为这是一个简单的任务,却耗费了我两个多小时的时间。 docker容器中修改配置,重启不成功 我们前提是容器中的mysql中已经有很多数据…

Web安全:SQL注入

一、SQL注入三要素 1、用户可以对输入的参数值进行修改。 2、后端不对用户输入的参数值进行严格过滤。 3、用户修改后的参数值可以被带入后端中成功执行,并返回一定结果。 二、SQL注入原理 简单来说,用户输入的值会被插入到SQL语句中,然后…

Milvus 核心设计(1) ---- 数据一致性的等级及使用场景

目录 背景 Milvus的数据一致性 设置数据一致性等级 等级类型 PACELC定理 level 详细解释 Strong Bounded staleness Session Eventually 总结 背景 分布式上的可扩展性是个比较重要的concept。Chroma 核心之前写过了,他的最大优势在于轻量级且好用。Milvus相对Ch…

tkinter-TinUI-xml实战(11)多功能TinUIxml编辑器

引言 在TinUIXml简易编辑器中,我们通过TinUI搭建了一个简易的针对TinUIXml布局的编辑器,基本掌握了TinUIXml布局和TinUIXml的导入与导出。现在,就在此基础上,对编辑器进行升级。 本次升级的功能: 更合理的xml编辑与…

大众汽车入职SHL在线测评、英语口语、招聘笔试如何通过、考点分析|备考建议

大众汽车入职在线测验真题考点分析,通过技巧? 大众汽车集团(中国)在招聘过程中,认知能力测试是评估候选人是否适合某个职位的重要环节。候选人会收到带有线上测评链接的邮件,测评包括胜任力潜力测试(Compe…

多输入多输出 | Matlab实现Transformer多输入多输出预测

多输入多输出 | Matlab实现Transformer多输入多输出预测 目录 多输入多输出 | Matlab实现Transformer多输入多输出预测预测效果基本介绍程序设计参考资料 预测效果 基本介绍 多输入多输出 | Matlab实现Transformer多输入多输出预测(完整源码和数据) 1.da…

CSS选择器:基本选择器、复合选择器、伪类选择器、伪元素选择器

CSS选择器包含:基本选择器、复合选择器、伪类选择器、伪元素选择器。 选择器是选择标签的一种方式,例如 ID 选择器就是通过 ID 选择标签的,类选择器就是通过类名选择标签的。 在 CSS3 中有很多类型的选择器,如下是《W3school》提…

Android 使用 Debug.startMethodTracing 分析方法耗时

参考 Generate Trace Logs by Instrumenting Your App 官网提供了 trace 工具来分析方法耗时。 生成 trace 文件 package com.test.luodemo.trace;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle; import android.os.Debug; import android.uti…

昇思25天学习打卡营第13天|基于MindNLP+MusicGen生成自己的个性化音乐

关于MindNLP MindNLP是一个依赖昇思MindSpore向上生长的NLP(自然语言处理)框架,旨在利用MindSpore的优势特性,如函数式融合编程、动态图功能、数据处理引擎等,致力于提供高效、易用的NLP解决方案。通过全面拥抱Huggin…