matlibplot常见图表绘制

更新时间:2023-07-07 05:43:09 阅读: 评论:0

matlibplot常见图表绘制
⼀、柱状图
1.通过obj.plot()
柱状图⽤bar表⽰,可通过obj.plot(kind='bar')或者obj.plot.bar()⽣成;在柱状图中添加参数stacked=True,会形成堆叠图。
k是什么意思fig,axes = plt.subplots(2,2,figsize=(10,6))
s = pd.Series(np.random.randint(0,10,15),index = list('abcdefghijklmno'))lava lava
df = pd.DataFrame(np.random.rand(10,3),columns = ['A','B','C'])
s.plot(kind = 'bar',ax = axes[0,0]) #kind表⽰图表类型
df.plot(kind = 'bar',ax = axes[0,1])
df.plot.bar(ax = axes[1,0],stacked = True)  #stacked = True表⽰显⽰为堆叠样式
df.plot.barh(ax = axes[1,1])  #横向的柱状图
绯闻女孩第二季
2.通过plt.bar(x,y)
直接使⽤plt.bar()时,需要在参数中指定x轴和y轴表⽰的数据。
plt.figure(figsize=(8,6))
x = np.arange(10)
y1 = np.random.rand(10)
y2 = -np.random.rand(10)
plt.bar(x,y1,yerr = 0.1 * y1)relation
for i,j in zip(x,y1):
<(i-0.3,j+0.05,'%.2f'%j)  #添加标签-0.3和+0.05是为了让标签的位置更合理
plt.bar(x,y2)
柱状图外嵌图表
influence的用法通过plt.table()可以实现在柱状图下⽅附上表格的功能。
plt.table(cellText=data.T,cellLoc='center',cellColours=None,rowLabels=cols,rowLoc='center',colLabels=rows,colLoc='center',loc=
cellText:表格内容,通常为DataFrame的values部分
cellLoc:表格内容的对齐⽅式,通常为center居中对齐
cellColours:表格内容背景颜⾊
rowLabels:⾏标签
rowLoc:⾏标签的对齐⽅式
colLabels:⾏标签的背景颜⾊
colLoc:列标签的对齐⽅式
loc:表格位置,常⽤bootom,
data = np.array([[1,2,3,1,2],[3,4,5,3,4],[2,3,4,2,3],[4,5,6,3,4]])
rows = ['x','y','z','w']
cols = ['a','b','c','d','e']
df = pd.DataFrame(data,index = rows,columns=cols)
df.plot(kind='bar',stacked=True,colormap='Blues_r')
托福雅思考试取消
plt.table(cellText=data.T,cellLoc='center',cellColours=None,rowLabels=cols,rowLoc='center',\
BuPu(np.linspace(0,0.5,5))[::-1],colLabels=rows,colLoc='center',colColours=None,loc='bottom')
⼆、⾯积图
⾯积图使⽤obj.plot(kind='bar')或者obj.plot.bar()⽣成,没有plt.area()⽅法。
⾯积图默认是堆叠的,并且要求值必须同时为正值或同时为负值;如果既有正值也有负值,不能堆叠,需要使⽤参数stacked=Fal。
fig,axes = plt.subplots(1,2,figsize=(10,6))
df1 = pd.DataFrame(np.random.rand(10,3),columns=['A','B','C'])
df2 = pd.DataFrame(np.random.randn(10,3),columns=['a','b','c'])
df1.plot.area(colormap='summer',ax = axes[0])
df2.plot(kind='area',stacked = Fal,ax = axes[1])
三、填图
填充x轴与⼀个y轴之间的空间:需要y轴与x轴有交叉,否则不会显⽰
填充两个y轴之间的空间:相当于两个y轴的差
fig,axes = plt.subplots(1,2,figsize=(10,4))
x = np.linspace(0,1,500)
y1 = np.sin(4*np.pi*x)
y2 = -np.sin(4*np.pi*x)
# y1 = 2*x
# y2 = -x  #跳⾍y1和y2都没有效果
axes[0].fill(x,y1,'lightblue',label='y1')
axes[0].fill(x,y2,'pink',label='y2')
axes[1].fill_between(x,y1,y2,color = 'lightblue',label='y1-y2')#此处需要⽤color指定颜⾊,直接写颜⾊会报错
四、饼图
affirmationplt.pie(s,explode=None,labels=None,colors=None,autopct=None,pctdistance=0.6,labeldistance=1.1,shadow=Fal,startangle=N
s:⼀个Seris
explode:突出的饼块位置及突出⼤⼩
labels:饼块的标签
colors:饼块的颜⾊
autopct:饼块的值的显⽰格式
pctdistance:饼块的值距离圆⼼的距离,相对于半径来说
labeldistance:标签距离圆⼼的距离,相对于半径来说
shadow:是否显⽰阴影
startangle:第⼀个饼开始的地⽅,默认从⽔平线开始逆时针⽅向进⾏,30表⽰从⽔平⽅向逆时针旋转30°的地⽅开始
radius:半径的长度
s = pd.Series(np.random.rand(4),index=['a','b','c','d'])
plt.axis('equal')  #
plt.pie(s,explode=[0.1,0,0,0],labels=s.index,colors=['r','g','b','y'],autopct='%.2f%%',pctdistance=0.5,\
labeldistance=1.2,shadow=Fal,startangle=0,radius=1.2,frame=Fal)
五、直⽅图
柱状图是⽤于展⽰数值的,⽽直⽅图是⽤来统计频率的。s.hist()常⽤参数:
bins柱⼦的数量,即将样本分为多少个组进⾏统计
histtype:直⽅图的风格,默认为bar,其他还有step、stepfilled、barstacked(有时候对于df不起作⽤,堆叠直⽅图⼀般不⽤s.hist()) align:对齐⽅式,默认为mid居中对齐,其他还有left和right
orientation:⽅向,默认为vertical竖直⽅向,其他horizontal
mormed:密度,默认为Fal,y轴显⽰数量,True表⽰y轴显⽰为0-1的区间,与密度图类似
grid:直⽅图默认有⽹格
fig,axes = plt.subplots(1,2,figsize=(10,5))
s = pd.Series(np.random.randn(1000))
s.hist(bins=20,histtype='bar',align='right',orientation='vertical',alpha=0.8,density=Fal,grid=True,ax=axes[0])#四种⽅式
# s.plot(kind='hist')
# s.plot.hist()
# plt.hist(s,bins=30)
s.hist(bins=20,alpha=0.8,density=True,ax=axes[1]) #直⽅图形式的密度图
s.plot(kind='kde',ax=axes[1] )#密度图
堆叠直⽅图,由于直⽅图的histtype设置为batstacked经常不⽣效,⽣成多系列的堆叠直⽅图尝试⽤df.plot()或df.plot.hist()来⽣成
df = pd.DataFrame({'A':np.random.randn(1000),'B':np.random.randn(1000),'C':np.random.randn(1000)})
df.plot.hist(stacked=True)
谢霆锋演讲
# df.plot(kind='hist',stacked=True)
# df.hist(stacked=True,histtype='barstacked')  #不起作⽤,⽣成3个独⽴的直⽅图
# plt.hist(df) #⽆法使⽤
六、散点图
plt.scatter(x, y, s=None, c=None, marker=None, cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None,
读万卷书不如行千里路       verts=None, edgecolors=None, *, plotnonfinite=Fal, data=None, **kwargs)
# x和y分别表⽰x轴和y轴数据
# s表⽰点的⼤⼩,可以为⼀个数值,也可为⼀变量,为变量则s可表⽰除x和y的另⼀个维度
# c表⽰点的颜⾊,可以为⼀具体颜⾊,也可为⼀变量,为变量则c可表⽰除x和y的另⼀个维度
# vmin和vmax表⽰亮度的最⼩和最⼤值,是⼀个具体的数值
plt.figure(figsize=(8,5))
x = np.random.randn(1000)
y = np.random.randn(1000)
plt.scatter(x,y,s =np.random.randn(1000)*30,c=np.random.randn(1000)*30,cmap='Reds') #只有这⼀种⽅法
矩阵散点图是pandas中的⽅法,不是matplotlib中的⽅法,常⽤来看各个指标之间的关系。
df = pd.DataFrame(np.random.randn(100,3),columns = ['A','B','C'])
jobinterview
pd.plotting.scatter_matrix(df,figsize=(8,8),diagonal='kde',marker='o',range_padding=0.1)
# 将df的三列进⾏两两对⽐,寻找任意两个之间的关系
# diagonal为对⾓线处的图表显⽰类型,即每⼀列⾃⾝,常⽤bar或kde
# range_padding为图表与x轴和y轴之间的空⽩
七.极坐标图
极坐标图是以⼀个圆盘进⾏显⽰的,起点为右侧⽔平⽅向的半径,距离起点的⾓度相当于x轴,距离圆⼼的距离相当于y轴。
s = pd.Series(np.arange(20))
arr = np.arange(0,2*np.pi,00.2)
fig = plt.figure(figsize=(10,5))
ax1 = plt.subplot(121,polar = True)
# ax1 = fig.add_subplot(121,projection='polar') #polar的两种参数写法等价,表⽰创建极坐标
ax2 = plt.subplot(122)
ax1.plot(s,linestyle='--',marker='o')
ax1.plot(arr,arr*3,linestyle='--',marker='.',color = 'r',lw=1)
ax2.plot(s,linestyle='--',marker='o')
ax2.plot(arr,arr*3,linestyle='--',marker='.',color = 'r',lw=1)
极坐标演⽰

本文发布于:2023-07-07 05:43:09,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/169655.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

上一篇:OLT-7360开通
下一篇:city什么意思
标签:堆叠   默认   距离   标签   表格
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图