第二章 使用matplotlib绘制简单图表

第二章 使用matplotlib绘制简单图表

1、使用plot()绘制折线图

plot(x,y,fmt,scaley=True,scaley=True,data=None,label=None,*args,**kwargs)

x:表示x轴数据
y:表示y轴数据
fmt:表示快速设置线条样式的格式字符串
label:表示应用于图例的标签文本
2、使用bar()绘制柱形图或者堆积柱形图

bar(x,height,width=0.8,bottom=None,align='center',data=None,tick_label=None,xerr=None,yerr=None,error_kw=None,**kwargs)

x:表示柱形的x坐标值
height:表示柱形的高度
width:表示柱形的宽度,默认0.8
bottom:表示柱形的底部的y坐标值,默认为0
align:表示柱形的对齐方式,有center和edge两个取值
tick_label:表示柱形对应的刻度标签
err,year:若未设为None,则需要为柱形图添加水平/垂直误差棒。
error——kw:表示误差棒的属性字典,字典得键对应errorbar()函数的关键字参数。
3、使用barh()绘制条形图或堆积条形图。

barh(y,width,height=0.8,left=None,align='center',*,**kwargs)

y:表示条形的y坐标值。
width:表示条形的宽度,默认值为0.8.
height:表示条形的高度。
left:条形左侧的x坐标,默认值为0。
align:表示条形的对齐方式,有‘center’和’edge‘两个取值,其中’center‘表示将图形与刻度线居中对齐;’edge’表示将条形的底边与刻度线对齐。
4、使用stackplot()绘制堆积面积图。

stackplot(x,y,labels=(),baseline='zero',data=None,*args,**kwargs)

x:表示x轴数据,可以是一组数组。
y:表示y轴数据,可以是二维数组或一位数组序列。
labels:表示每组折线及填充区域的标签。
baseline:表示计算基线的方法。
5、使用hist()绘制直方图。

hist(x,bins=None,range=None,density=None,weights=None,cumulative=Falae,bottom=None,histtype,histtype='bar',align='mid',orientation='vertical',rwidth=None,log=False,label=None,stacked=False,normed=None,*,data=None,**kwargs)

x:表示x轴的数据
bins:表示矩形条的个数,默认为10个。
range:表示数据的范围
cumulative:表示是否计算累积频数或频率
histtype:表示直方图的类型
align:表示矩形条边界的对齐方式
orientation:表示矩形条边界的摆放方式
rwidth:表示矩形条宽度的百分比,默认为0.
stacked:表示是否将多个矩形条以堆积形式摆放
6、使用pie()绘制饼图或圆环图

pie(x,explode=None,labels=None,autopct=None,pctdistance=0.6,shadow=False,labeldistance=1.1,startangle=None,radius=None,counterclock=True,wedgeprops=None,textprops=None,center=(0,0),frame=False,rotatelabels=False,*,data=None)

x:表示扇形或契形的数据
explode:表示扇形或契形离开圆心的距离
labels:表示扇形或契形对应的标签文本
autopct:表示控制扇形或契形的数值显示的字符串
pctdistance:表示扇形或契形对应的数值标签距离圆心的比例,默认为0.6
shadow:表示是否现实阴影
labeldistance:表示标签文本的绘制位置
startangle:表示起始绘制角度
radius:表示扇形或契形的半径
wedgeprops:表示控制扇形或契形属性的字典
textprops:表示控制图表中文本属性的字典
center:表示图表的中心点位置
frame:表示是否显示图框
7、使用scatter()绘制散点图或气泡图

scatter(x,y,s=None,c=None,marker=None,cmap=None,norm=None,xmin=None,vmax=None,alpha=None,linewidths=None,verts=None,edgecolors=None,*,plotnonfinite=False,data=None,**kwargs)

8、使用boxplot()绘制箱形图

boxplot(x,notch=None,sym=None,vert=None,whis=None,positions=None,widths=None,patch_artist=None,bootstrap=None,usermedians=None,conf_intervals=None,meanline=None,showmeans=None,showcaps=None,showbox=None,showfliers=None,boxprops=None,labels=None,flierprops=None,medianprops=None,mesnprops=None,capprops=None,whiskerprops=None,manage_ticks=Ture,autorange=False,zorder=None,*,data=None)

9、使用polar()绘制雷达图

polar(theta,r,**kwargs)

10、使用errorbar()绘制误差棒图

errorbar(x,y,yerr=None,xerr=None,fmt='',ecolor=None,elinewidth=None,capsize=None,barsabove=False,lolims=False,uplims=False,xlolims=False,xuplims=False,errorevery=1,capthick=None,*,data=None,**kwargs)
import numpy as np
import matplotlib.pyplot as plt
y = np.arange (5)
x1 = np.array ([10, 8, 7, 11, 13])
bar_height = 0.3
plt.barh (y,x1,tick_label=['a','b','c','d','e'],height=bar_height)
plt.title("2020080603050")
plt.show()


在这里插入图片描述

y = np.arange (5)
x1 = np.array ([10, 8, 7, 11, 13])
x2 = np.array ([9, 6, 5, 10, 12])
bar_height = 0.3
plt.barh (y,x1,tick_label=['a','b','c','d','e'],height=bar_height)
plt.barh (y+bar_height,x2,height=bar_height)
plt.title("2020080603050")
plt.show()

在这里插入图片描述

plt.barh (y,x1,tick_label=['a','b','c','d','e'],height=bar_height)
plt.barh (y,x2,left=x1,height=bar_height)
plt.title("2020080603050")
plt.show()

在这里插入图片描述

import numpy as np
import matplotlib.pyplot as plt
x = np.arange (5)
y1 = np.array ([10, 8, 7, 11, 13])
bar_width = 0.3
plt.bar(y,x1,tick_label=['a','b','c','d','e'],width=bar_width)
plt.title("2020080603050")
plt.show()

在这里插入图片描述

x = np.arange (5)
y1 = np.array ([10, 8, 7, 11, 13])
y2 = np.array ([9, 6, 5, 10, 12])
bar_width = 0.3
plt.bar(y,x1,tick_label=['a','b','c','d','e'],width=bar_width)
plt.bar(x+bar_width,y2,width=bar_width)
plt.title("2020080603050")
plt.show()

在这里插入图片描述

import numpy as np
import matplotlib.pyplot as plt
x=np.arange(6)
y1=np.array([1,4,3,5,6,7])
y2=np.array([1,3,4,2,7,6])
y3=np.array([3,4,3,6,5,5])
plt.stackplot(x,y1,y2,y3)
plt.title("2020080603050")
plt.show()

在这里插入图片描述

scores=np.random.randint(0,100,50)
plt.hist(scores,bins=8,histtype='stepfilled')
plt.title("2020080603050")
plt.show()

在这里插入图片描述

data=np.array([20,50,10,15,30,55])
pie_labels=np.array(['A','B','C','D','E','F'])
plt.pie(data,radius=1.5,labels=pie_labels,autopct='%3.1f%%',pctdistance=0.75)
plt.title("2020080603050")
plt.show()

在这里插入图片描述

data=np.array([20,50,10,15,30,55])
pie_labels=np.array(['A','B','C','D','E','F'])
plt.pie(data,radius=1.5,wedgeprops={'width':0.7},labels=pie_labels,autopct='%3.1f%%',pctdistance=0.75)
plt.title("2020080603050")
plt.show()

在这里插入图片描述

import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']=False
kinds=['购物','人情来往','餐饮美食','通信物流','生活用品','交通出行','休闲娱乐','其他']
money_scale=[800/3000,100/3000,1000/3000,200/3000,300/3000,200/3000,200/3000,200/3000]
dev_position=[0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,]
plt.pie(money_scale,labels=kinds,autopct='%3.1f%%',shadow=True,explode=dev_position, startangle=90)
plt.title("2020080603050")
plt.show()

在这里插入图片描述

num=50
x=np.random.rand(num)
y=np.random.rand(num)
plt.scatter(x,y)
plt.show()

在这里插入图片描述

plt.rcParams['font.family']='SimHei'
plt.rcParams['axes.unicode_minus']=False
dim_num=6
data=np.array([[0.40,0.32,0.35,0.30,0.30,0.88],
               [0.85,0.35,0.30,0.40,0.40,0.30],
               [0.43,0.89,0.30,0.8,0.22,0.30],
               [0.30,0.25,0.45,0.85,0.45,0.40],
               [0.34,0.31,0.38,0.40,0.92,0.28],
               [0.34,0.31,0.38,0.40,0.92,0.28]]
             )
angles=np.linspace(0,2 * np.pi,dim_num,endpoint=False)
angles=np.concatenate((angles,[angles[0]]))
data=np.concatenate((data,[data[0]]))
radar_labels=['研究(I)','艺术(A)','社会(S)','企业(E)','传统(C)','现实(R)']
radar_labels=np.concatenate((radar_labels,[radar_labels[0]]))
plt.polar(angles.data)
plt.thetagrids(angles * 180/np.pi,labels=radar_labels)
plt.fill(angles,data,alpha=0.25)
plt.title("2020080603050")
plt.show()

在这里插入图片描述

x=np.arange(5)
y=(25,32,34,20,25)
y_offset=(3,5,2,3,3)
plt.errorbar(x,y,yerr=y_offset,capsize=3,capthick=2)
plt.title("2020080603050")
plt.show()

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值