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()

 

 

 

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值