﻿ Python使用统计函数绘制简单图形实例代码 - 军军小站|张军博客

# Python使用统计函数绘制简单图形实例代码

Matplotlib 是 Python 的绘图库。 它可与 NumPy 一起使用，提供了一种有效的 MatLab 开源替代方案。 它也可以和图形工具包一起使用，如 PyQt 和 wxPython。

Windows 系统安装 Matplotlib

```
python -m pip install -U pip setuptools
python -m pip install matplotlib
```

Linux 系统安装 Matplotlib

Debian / Ubuntu：

```
sudo apt-get install python-matplotlib
```

Fedora / Redhat：

```
sudo yum install python-matplotlib
```

Mac OSX 系统安装 Matplotlib

Mac OSX 可以使用 pip 命令来安装：

```
sudo python -mpip install matplotlib
```

```
\$ python -m pip list | grep matplotlib
matplotlib (1.3.1)
```

1.函数bar()--用于绘制柱状图

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

mpl.rcParams["font.sans-serif"]=["SimHei"]
mpl.rcParams["axes.unicode_minus"]=False

x=[1,2,3,4,5,6,7,8]
y=[3,1,4,5,8,9,7,2]
plt.bar(x,y,align="center",color="c",tick_label=["q","a","c","e","r","j","b","p"],hatch="/")
plt.xlabel("箱子编号")
plt.ylabel("箱子重量(kg)")
plt.show()
```

2.

2、函数barh()--用于绘制条形图

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

mpl.rcParams["font.sans-serif"]=["SimHei"]
mpl.rcParams["axes.unicode_minus"]=False

x=[1,2,3,4,5,6,7,8]
y=[3,1,4,5,8,9,7,2]
plt.barh(x,y,align="center",color="c",tick_label=["q","a","c","e","r","j","b","p"],hatch="/")
plt.xlabel("箱子编号")
plt.ylabel("箱子重量(kg)")
plt.show()
```

3.

3、函数hist()--用于绘制直方图

```
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
mpl.rcParams["font.sans-serif"]=["SimHei"]
mpl.rcParams["axes.unicode_minus"]=False

#set test scores
boxWeight = np.random.randint(0,10,100)
x = boxWeight
#plot histogram
bins = range(0,11,1)
plt.hist(x,bins=bins,
color="g",
histtype="bar",
rwidth=1,
alpha=0.6)
#set x,y-axis label
plt.xlabel("箱子重量(kg)")
plt.ylabel("销售数量(个)")
plt.show()
```

4.函数pie()--用于绘制饼图

```
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rcParams["font.sans-serif"]=["SimHei"]
mpl.rcParams["axes.unicode_minus"]=False

kinds ="简易箱","保温箱","行李箱","密封箱"
colors = ["#e41a1c","#377eb8","#4daf4a","#984ea3"]
soldNums = [0.05,0.45,0.15,0.35]
#pie chart
plt.pie(soldNums,
labels=kinds,
autopct="%3.1f%%",
startangle=60,
colors=colors)
plt.title("不同类型箱子的销售数量占比")
plt.show()
```

5.函数polar()--用于绘制极线图

```
import matplotlib.pyplot as plt
import numpy as np
barSlices = 12
theta = np.linspace(0.0,2*np.pi,barSlices,endpoint=False)
r = 30*np.random.rand(barSlices)
plt.polar(theta,r, #theta每个标记所在射线与极径的夹角，r每个标记到原点的距离
color="chartreuse",
linewidth=2,
marker="*",
mfc="b",
ms=10)
plt.show()
```

6.函数scatter()--用于绘制气泡图

```
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
a = np.random.randn(100)
b = np.random.randn(100)
#colormap:RdYlBu
plt.scatter(a,b,s=np.power(10*a+20*b,2),#s散点标记的大小
c=np.random.rand(100),#c散点标记的颜色
cmap=mpl.cm.RdYlBu,#将浮点数映射成颜色的颜色映射表
marker='o')
plt.show()
```

7.函数stem()--用于绘制棉棒图

```
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.5,2*np.pi,20)
y = np.random.randn(20)
plt.stem(x,y,linefmt="-.",markerfmt="o",basefmt="-")
linefmt棉棒的样式、markerfmt棉棒末端的样式、basefmt指定基线的样式
plt.show()
```

8.函数boxplot()--用于绘制箱型图

```
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
mpl.rcParams["font.sans-serif"]=["SimHei"]
mpl.rcParams["axes.unicode_minus"]=False

x = np.random.randn(1000)
plt.boxplot(x)
plt.xticks([1],["随机数生成器AlphaRM"])
plt.ylabel("随机数值")
plt.title("随机数生成器抗干扰能力的稳定性")
plt.grid(axis="y",ls=":",lw=1,color="gray",alpha=0.4)
plt.show()
```

9.函数errorbar()--用于绘制误差棒图

```
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.1,0.6,6)
y = np.exp(x)
plt.errorbar(x,y,fmt="bo:",yerr=0.2,xerr=0.02)
plt.xlim(0,0.7)
plt.show()
```

QQ号联系： 360901061

【本文对您有帮助就好】