1、堆积柱状图

``````import matplotlib.pyplot as plt

x = [1,2,3,4,5]
y = [6,10,4,5,1]
y1 = [2,6,3,8,5]

plt.bar(x,y,align="center",color="#66c2a5",tick_label=["A","B","C","D","E"],label="title_A")
plt.bar(x,y1,align="center",color="#8da0cb",tick_label=["A","B","C","D","E"],label="title_B")
plt.legend()

plt.show()``````

2、堆积条状图

``````import matplotlib.pyplot as plt

x = [1,2,3,4,5]
y = [6,10,4,5,1]
y1 = [2,6,3,8,5]

plt.barh(x,y,align="center",color="#66c2a5",tick_label=["A","B","C","D","E"],label="title_A")
plt.barh(x,y1,align="center",left=y,color="#8da0cb",tick_label=["A","B","C","D","E"],label="title_B")
plt.legend()

plt.show()``````

1、多数据并列柱状图

``````import matplotlib.pyplot as plt
import numpy as np

x = np.arange(5)
y = [6,10,4,5,1]
y1 = [2,6,3,8,5]

bar_width = 0.35
tick_label = ["A","B","C","D","E"]
plt.bar(x,y,align="center",color="c",width=bar_width,label="title_A",alpha=0.5)
plt.bar(x+bar_width,y1,align="center",color="b",width=bar_width,label="title_B",alpha=0.5)

plt.xticks(x+bar_width/2,tick_label)
plt.legend()

plt.show()``````

2、多数据平行条状图

``````import matplotlib.pyplot as plt
import numpy as np

x = np.arange(5)
y = [6,10,4,5,1]
y1 = [2,6,3,8,5]

bar_width = 0.35
tick_label = ["A","B","C","D","E"]
plt.barh(x,y,bar_width,align="center",color="c",label="title_A",alpha=0.5)
plt.barh(x+bar_width,y1,bar_width,align="center",color="b",label="title_B",alpha=0.5)

plt.yticks(x+bar_width/2,tick_label)
plt.legend()

plt.show()``````

1、函数stackplot() —— 绘制堆积折线图

``````import matplotlib.pyplot as plt
import numpy as np

x = np.arange(0,5,1)
y = [0,4,3,5,6]
y1 = [1,3,4,2,7]
y2 = [3,4,1,6,5]

labels = ["Blue","Brown","Green"]
colors = ["#8da0cb","#fc8d62","#66c2a5"]

plt.stackplot(x,y,y1,y2,labels=labels,colors=colors)

plt.legend(loc="upper left")

plt.show()``````

2、函数broken_barh() —— 绘制间断条形图

``````import matplotlib.pyplot as plt
import numpy as np

plt.broken_barh([(30,100),(180,50),(260,70)],(20,8),facecolors="#1f78b4")
plt.broken_barh([(60,90),(190,520),(230,30),(280,60)],(10,8),facecolors=("#7fc97f","#beaed4","#fdc086","#ffff99"))

plt.xlim(0,360)
plt.ylim(5,35)

plt.xticks(np.arange(0,361,60))
plt.yticks([15,25],["A","B"])

plt.grid(ls="-",lw=1,color="gray")

plt.show()``````

3、函数step() —— 绘制阶梯图

``````where：默认参数为“pre”，

``````import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(1,10,10)
y = np.sin(x)

plt.step(x,y,color="#8dd3c7",where="pre",lw=2)

plt.xlim(0,11)
plt.xticks(np.arange(1,11,1))
plt.ylim(-1.2,1.2)

plt.show()``````

1、hist()

``hist(x,bins=bins,color="b",histtype="bar",label="score",rwidth=10)``

``````x：连续型数据的输入值
bins：用于确定柱体的个数或是柱体边缘的范围
color：柱体的颜色
histtype：柱体类型
label：图例内容
rwidth：柱体宽度``````

2、堆积直方图

``````import matplotlib.pyplot as plt
import numpy as np

x1 = np.random.randint(0,100,100)
x2 = np.random.randint(0,100,100)

x = [x1,x2]
labels = ["A","B"]
bins = range(0,101,10)

plt.hist(x,bins=bins,color=colors,histtype="bar",rwidth=10,stacked=True,label=labels,edgecolor = 'k')

plt.legend(loc="upper left")
plt.show()``````

3、直方图与阶梯图结合对比

``````import matplotlib.pyplot as plt
import numpy as np

x1 = np.random.randint(0,100,100)
x2 = np.random.randint(0,100,100)

x = [x1,x2]
labels = ["A","B"]
bins = range(0,101,10)

plt.hist(x,bins=bins,color=colors,histtype="stepfilled",rwidth=10,stacked=True,label=labels,edgecolor = 'k')

plt.legend(loc="upper right")
plt.show()``````

1、分列式饼图

``````import matplotlib.pyplot as plt

labels = ["A","B","C","D"]

nums = [0.35,0.15,0.2,0.3]

colors = ["#377eb8","#4daf4a","#984ea3","#ff7f00"]
explode = (0.1,0.1,0.1,0.1)

plt.show()``````

2、非分裂式饼图

``````import matplotlib.pyplot as plt

labels = ["A","B","C","D"]

nums = [0.35,0.15,0.2,0.3]

colors = ["#377eb8","#4daf4a","#984ea3","#ff7f00"]

plt.pie(nums,labels=labels,autopct="%3.1f%%",startangle=45,colors=colors,pctdistance=0.7)
plt.show()``````

3、内嵌环形饼图

``````import matplotlib.pyplot as plt

labels = ["A","B","C","D","E"]

nums1 = [40,15,20,10,15]
nums2 = [30,25,15,20,10]

colors = ["#e41a1c","#377eb8","#4daf4a","#984ea3","#ff7f00"]

plt.legend(nums1,labels,fontsize=12,bbox_to_anchor=(0.91,0,0.3,1))

plt.setp(a1,size=15,weight="bold")
plt.setp(a2,size=15,weight="bold")
plt.setp(t1,size=12)
plt.show()``````

``plt.boxplot(testList,whis=whis,widths=width,sym="o",labels=labels,patch_artist=True)``

``````testList:绘制箱线图的输入数据
whis：四分位间距的倍数，用来确定箱须包含数据的范围大小
widths：设置箱体的宽度
sym：离群值的标记样式
labels：绘制每一个数据集的刻度标签
patch_artist：是否给箱体添加颜色``````

``````import matplotlib.pyplot as plt
import numpy as np

t1 = np.random.randn(5000)
t2 = np.random.randn(5000)

testList = [t1,t2]
labels = ["A","B"]
colors = ["#1b9e77","#d95f02"]

whis = 1.6
width = 0.35

bplot = plt.boxplot(testList,whis=whis,widths=width,sym="o",labels=labels,patch_artist=True)

for patch,color in zip(bplot["boxes"],colors):
patch.set_facecolor(color)

plt.grid(axis="y",ls=":",lw=1,color="gray",alpha=0.4)
plt.show()``````

2、水平方向箱线图

``````import matplotlib.pyplot as plt
import numpy as np

x = np.random.randn(1000)

plt.boxplot(x,vert=False)

plt.grid(axis="x",ls=":",lw=1,color="gray",alpha=0.4)
plt.show()``````

3、没有离群值的水平箱线图

``````import matplotlib.pyplot as plt
import numpy as np

x = np.random.randn(1000)

plt.boxplot(x,vert=False,showfliers=False)

plt.grid(axis="x",ls=":",lw=1,color="gray",alpha=0.4)
plt.show()``````

1、定量数据的误差范围

``````import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0.1,0.6,10)
y = np.exp(x)

err = 0.05 + 0.15 * x

low_err = err
upp_err = err * 0.3
err_lim = [low_err,upp_err]

plt.errorbar(x,y,yerr=err_lim,fmt=":o",ecolor="y",elinewidth=4,ms=5,mfc='r',capthick=1,capsize=2)

plt.xlim(0,0.7)
plt.show()``````

``````x,y : 数据点的位置
yerr:单一数值的非对称形式误差范围    //误差范围非对称形式，而且数据点下方的误差范围大于数据点上方的误差范围，xerr类似
fmt：数据点标记样式和数据点标记的连接线样式
ecolor：误差棒的线条颜色
elinewidth：误差棒的线条粗细
ms:数据点的大小
mfc：数据点的标记颜色
mec：数据点的标记边沿颜色
capthick：误差棒边界横杠的厚度
capsize：误差棒边界横杠的大小``````

2、带误差棒的柱状图

``````import matplotlib.pyplot as plt
import numpy as np

x = np.arange(5)
y = [100,68,79,91,82]
err = [7,2,6,10,5]

err_att = dict(elinewidth=2,ecolor="black",casize=3)

plt.bar(x,y,color="c",width=0.6,align="center",yerr=err,error_kw=err_att,tick_label=["A","B","C","D","E"])
plt.grid(True,axis="y",ls=":",color="gray",alpha=0.2)
plt.show()``````

3、带误差棒的条形图

``````import matplotlib.pyplot as plt
import numpy as np

x = np.arange(5)
y = [1200,2400,1800,2200,1600]
err = [150,100,180,130,100]

bar_width = 0.6
colors = ["#377eb8","#4daf4a","#984ea3","#ff7f00","#e41a1c"]

plt.barh(x,y,bar_width,color=colors,align="center",xerr=err,tick_label=["A","B","C","D","E"])
plt.grid(True,axis="x",ls=":",color="gray",alpha=0.2)
plt.show()``````

4、带误差棒的多数据并列柱状图

``````import matplotlib.pyplot as plt
import numpy as np

x = np.arange(5)
y1 = [100,68,79,91,82]
y2 = [120,75,70,78,85]
err1 = [7,2,6,10,5]
err2 = [5,1,4,8,9]

err_att = dict(elinewidth=2,ecolor="black",capize=3)
bar_width = 0.4

tick_label=["A","B","C","D","E"]

plt.bar(x,y1,bar_width,color="#87ceeb",align="center",yerr=err1,error_kw=err_att,label="NO.1")
plt.bar(x+bar_width,y2,bar_width,color="#cd5c5c",align="center",yerr=err2,error_kw=err_att,label="NO.2")

plt.xticks(x+bar_width/2,tick_label)
plt.grid(True,axis="y",ls=":",color="gray",alpha=0.2)

plt.legend()
plt.show()``````

5、带误差棒的堆积柱状图

``````import matplotlib.pyplot as plt
import numpy as np

x = np.arange(5)
y1 = [1200,2400,1800,2200,1600]
y2 = [1050,2100,1300,1600,1340]
err1 = [150,100,180,130,80]
err2 = [120,110,170,150,120]

err_att = dict(elinewidth=2,ecolor="black",capize=0)
bar_width = 0.6

tick_label=["A","B","C","D","E"]

plt.bar(x,y1,bar_width,color="#6495ed",align="center",yerr=err1,error_kw=err_att,label="NO.1")
plt.bar(x,y2,bar_width,color="#ffa500",align="center",yerr=err2,error_kw=err_att,label="NO.2")

plt.xticks(x,tick_label)
plt.grid(True,axis="y",ls=":",color="gray",alpha=0.2)

plt.legend()
plt.show()``````

## 使用Python统计函数绘制复杂图形matplotlib的更多相关文章

1. 使用Python统计函数绘制简单图形matplotlib

1.bar() -- 绘制柱状图 plt.bar(x,y,align="center",color="b",tick_label=["a", ...

2. Python画各种 3D 图形Matplotlib库

回顾 2D 作图 用赛贝尔曲线作 2d 图.此图是用基于 Matplotlib 的 Path 通过赛贝尔曲线实现的,有对赛贝尔曲线感兴趣的朋友们可以去学习学习,在 matplotlib 中,figur ...

3. Python数据处理——绘制函数图形以及数据拟合

1.多项式拟合 对散点进行多项式拟合并打印出拟合函数以及拟合后的图形import matplotlib.pyplot as pltimport numpy as npx=np.arange(1,17, ...

4. Python 使用 matplotlib绘制3D图形

3D图形在数据分析.数据建模.图形和图像处理等领域中都有着广泛的应用,下面将给大家介绍一下如何在Python中使用 matplotlib进行3D图形的绘制,包括3D散点.3D表面.3D轮廓.3D直线( ...

5. Python绘制3D图形

来自:https://www.jb51.net/article/139349.htm 3D图形在数据分析.数据建模.图形和图像处理等领域中都有着广泛的应用,下面将给大家介绍一下如何使用python进行 ...

6. Python学习（一） —— matplotlib绘制三维轨迹图

在研究SLAM时常常需要对其输出的位姿进行复现以检测算法效果,在ubuntu系统中使用Python可以很好的完成相关的工作. 一. Ubuntu下Python的使用 在Ubuntu下使用Python有 ...

7. Python plot_surface（Axes3D）方法：绘制3D图形

3D 图形需要的数据与等高线图基本相同:X.Y 数据决定坐标点,Z 轴数据决定 X.Y 坐标点对应的高度.与等高线图使用等高线来代表高度不同,3D 图形将会以更直观的形式来表示高度. 为了绘制 3D ...

8. 10个Python 统计报表/图表图形类库

matplotlib,官网:http://matplotlib.sourceforge.net/,Matplotlib 是一个由 John Hunter 等开发的,用以绘制二维图形的 Python 模 ...

9. python之绘制函数pyplot初探

我们想将我们手里的数据通过图形的方式展示出来,这样我们更直观的,更可以发现数据带给我们的信息.今天给大家介绍要给python中pyplot绘制函数.一般我们想将手里的数据绘制成图形,分为四大步:1.准 ...

## 随机推荐

1. Linux备份入门：3种克隆方法详解_Clonezilla

摘自:http://os.51cto.com/art/201006/206871.htm 给你的系统磁盘制作一个镜像拷贝是创建备份的好办法.由于便携式U盘总是有比较低廉的价格,你可以维护数个拷贝以便循 ...

2. 解决Cisco VPN Client：Reason 442: Failed to Enable Virtual Adapter VPN连接问题

大公司里肯定涉及不同地点的办公问题,这样VPN的使用就频繁了,今天遇到一个VPN连接问题,分享给大家,看一眼,以后不在这问题上耗费太多功夫. 在win7上连接vpn时抛出“failed to enab ...

3. （一）backbone - API入门

初探 backbone采用MVC模式,本身提供了模型.控制器和视图从而我们应用程序的骨架便形成. Backbone.js 唯一重度依赖 Underscore.js. 对于 RESTful , hist ...

4. 【Tomcat源码学习】-1.概述

Tomcat是用java语言开发的一个Web服务器,最近花了差不多两周时间对Tomcat 9.0源码进行了一遍学习,由于知识储备有限,也只是理解了一个大概,下面就由我来给大家分享一下我对Tomcat的 ...

5. python性能分析之cProfile模块

cProfile是标准库内建的分析工具的其中一个,另外两个是hotshot和profile -s cumulative -s cumulative开关告诉cProfile对每个函数累计花费的时间进行排 ...

6. vue弹窗组件

文件结构 component.vue <template> <div class="_vuedals" v-show="show"> & ...

7. JoyOI1035 棋盘覆盖

原题链接 对棋盘染色,坐标和为奇数的染黑,偶数为白.这时会发现对于相同颜色的格子,是无法放置骨牌的,这样我们就将所有格子分成两类,然后根据能否放置骨牌连边,最后就是求二分图最大匹配了. 这里我是用的匈 ...

8. 关于TCP/IOCP构架中出现的假死连接解决方案

如果在2台不同的公网机器,对TCP的c/s做过详细的压力测试,那么很不幸,会有很多人发现自己的server端会出现大量的假死连接. 假死连接具体表现如下: 1.在s端机器上,会有一些处于TCP_EST ...

9. js &amp; get recursive ids

js & get recursive ids len = 0; bug for(let i = 0; i < 3; i++) { console.log(`i =`, i); let y ...

10. 用cocos2d 2.1制作一个过河小游戏(4): 游戏主逻辑BaseLayer设计

前段时间一直在忙.没有时间更新博客.今天还是抽点时间把最后一小部分游戏的实现放上来吧. BaseLayer.h: #import <GameKit/GameKit.h> #import & ...