0
点赞
收藏
分享

微信扫一扫

mathplotlib库学习笔记

才德的女子 2022-02-24 阅读 30
学习python

绘制直方图

导入mathplotilb库

import matplotlib.pyplot as plt

设置使其支持中文

from matplotlib import rcPamrams
rcPamrams['font.family']=rcPamrams['font.sans-serif']='SimHer'

用figure创建窗口对象

chuang=plt.figure()

用add_subplot创建一个子图对象

ax=chuang.add_subpolt()

用bar创建直方图

ax.bar(x=(0.1,0.2,0.3),height=(1,2,3),width=0.5)

x: 坐标,几个就是几个柱子

height:高

width:宽

set_title: 设置标题

ax.set_title('直方图')

show显示窗口(显示出图像)

plt.show()

barh绘制纵向直方图(x修改为y)

ax.barh(y=(0.1,0.2,0.3),height=(1,2,3),width=0.05)

堆叠直方图

创建子图

import matplotlib.pyplot as plt
chuang=plt.figure()
ax=chuang.add_subplot()

设置参数

ax.bar(x=(1,2,3),height=(20,25,30),width=(0.5),color='red',label='shuju1')
ax.bar(x=(1,2,3),height=(30,45,29),width=(0.5),color='green',label='shuju2',bottom=(20,25,30))# 他的底与另一个高一样

color:设置颜色bel:设置右上角标注,需要ax.legend()方法才能使用

bottom:设置柱状图的底部在哪里

label:设置右上角的标注,有ax.legend()才能显示

set_xticks 设置x轴刻度(set_yticks设置y轴刻度)

ax.set_xticks((1,2,3))

set_ylabel指明y轴的含义(为y轴打上标签)

ax.set_ylabel('shuju')

set_xticklabels()设置x轴刻度下的文字

ax.set_xticklabels(('diyi','dier','disan'))

显示右上角的标注

ax.legend()

set_xlim设置x轴范围(x改为y设置y轴范围)

ax.set_xlim(0,5)

对比直方图

figsize指定窗口大小(dpi为清晰度)

fig=plt.figure(figsize=(10,5),dpi=100)

text在任意位置写文字

ax.text(x1,y1,s1,rotation=90,ha='ceter',va='center',size=20)

x1:x坐标

y1:y坐标

si:要写的字

rotation:旋转的角度

ha:水平对齐方式

va:竖直对齐方式

size:大小

 a1.get_x()

a1.get_height()

import matplotlib.pyplot as plt
fig=plt.figure()
ax=fig.add_subplot()
a1x=(1,3,5)#a1的x轴的坐标
a2x=(2,4,6)#a2的x轴坐标
a1h=(20,25,15)#a1的高
a2h=(30,25,20)
shua=('A1','A2','A3','A4','A5','A6')
a1=ax.bar(a1x,a1h,width=0.5,color='red',label='xiaomi')
a2=ax.bar(a2x,a2h,width=0.5,color='green',label='huawei')
ax.legend()
ax.set_xticks((1,2,3,4,5,6))
ax.set_xticklabels(shua)
def cao(a,b):
    for i in b:#相当于其中每个柱子
        ax.text(i.get_x()+0.25,i.get_height(),str(i.get_height()))
cao(ax,a1)
cao(ax,a2)
plt.show()

折线图与散点图

plot绘制折线图

ax.plot(x1,y1,'red',label=' ')

x1:横坐标(各个顶点)

y1:纵坐标

 scatter绘制散点图

ax.scatter(x,y,c='red')

 shuffle打乱一组数

ax.pie(x,label=(),exploade=[],autopct='%.2f',shadow=True,labeldistance=1.1,pctdistance=0.6,startangle=90)
random.shuffle(a)

x,y是坐标

画一个散点图一个折线图

import matplotlib.pyplot as plt
import math,random
chu=plt.figure(dpi=500)
ax=chu.add_subplot()
x1=(1,5)#散点图的横坐标
y1=(1,5)#纵坐标
ax.scatter(x1,y1,c='red')
ax.plot((1,2,3),(4,9,6),'green')
plt.show()

绘制饼图

ax.pie(x,label=(),exploade=[],autopct='%.2f',shadow=True,labeldistance=1.1,pctdistance=0.6,startangle=90)

x:各部分占的百分比

label:标签显示在扇形上

exploade:每个扇形突出的部分

autopct=’%.2f‘:在每个扇形上显示各自站的百分比

shadow=True:是否打开阴影

labeldistance=1.1:每个标签到扇形的距离

pctdistance=:显示的百分比圆心的距离到:如0.6是半径的0.6倍

startangle=90:从哪里开始话,90是从90°处逆时针话

画一个

import matplotlib.pyplot as plt
ax=plt.figure().add_subplot()
x=(10,20,30,40)
l=('a','b','c','d')
ax.pie(x,labels=l,explode=[0,0,0.1,0],autopct='%.2f',shadow=True,labeldistance=1.1,pctdistance=0.6,startangle=90)
plt.show()

绘制热力图

imshow绘制热力图

ax.imshow(date,cmap=plt.cm.hot,aspect='auto',vmin= ,vmax=)

date:二维

cmap。。。。:设置热力图,如果hot改为cold则改为冷图

vmax=:设置最亮的数值,默认date中最大的数最亮

vmin:设置最暗的数值

colorbar绘制热力指示柱

a1=ax.imshow(date,cmap=plt.cm.hot,aspect='auto',vmin= ,vmax=)
plt.colorbar(a1)

plt.xticts坐标标注旋转对齐

plt.xticts(rotation=90 ,ha='right')

画一个

import matplotlib.pyplot as plt
import numpy as np
ax=plt.figure().add_subplot()
date=np.arange(12).reshape(3,4)
a=ax.imshow(date,cmap=plt.cm.hot,aspect='auto',vmin=1,vmax=20)
plt.colorbar(a)
plt.show()

雷达图

设置极坐标形式的子图

ax=plt.figure().add_subplot(projection='polar')

 thetagrids设置雷达图一层层的圈

ax.set_thetagrids(ang,ladel=,fontpropertief='simple')

ang;每层圈的角度

ladel:每层圈上的标签

fon........:设置中文简体

fill填充雷达图

ax.fill(an,date,facecolor='',alpha=0.25)

an:每一条半径的角度

date:每个的属性值(每个角)

alpha:透明度

一个窗口多幅图

ax=plt.figure().add_subplot(x,y,z)
x

x,y:将窗口分为x行y个,

z:该图占据窗口的那一格(前面自己分的)

占多格

ax=plt.figure().add_subplot(2,2,1)
ax=plt.figure().add_subplot(2,1,2)

第一个分为2行2列4格

第二个分为2行一列

在窗口上添加标题

plt.figtext(x,y,'z')

x,y:坐标

z:标题

举报

相关推荐

0 条评论