Python制作统计图形

0
3
1
1. 云栖社区>
2. 博客>
3. 正文

## Python制作统计图形

jerryhuose 2016-12-17 21:41:53 浏览2375

Python一般使用Matplotlib制作统计图形，用它自己的说法是‘让简单的事情简单，让复杂的事情变得可能’。（你说国外的“码农”咋这么会说，我就整不出来这工整的句子！）用它可以制作折线图，直方图，条形图，散点图，饼图，谱图等等你能想到的和想不到的统计图形，这些图形可以导出为多种具有出版质量的格式。此外，它和ipython结合使用，确实方便，谁用谁知道！

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

pylab.randn(2,3)
array([[ 1.22356117, -0.62786477, -0.02927331],
[ 1.11739661, -1.64112491, 2.24982297]])

np.random.randn(2,3)
array([[-1.41691502, -1.43500335, -0.68452086],
[-0.53925581, -0.18478012, -0.0126745 ]])
pylab.hist([1,1,1,2,3,3])
plt.hist([1,1,1,2,3,3])``````

``````N = 5
ind = np.arange(N)
width = 0.35
menMeans = (20, 35, 30, 35, 27)
menStd = (2, 3, 4, 1, 2)

womenMeans = (25, 32, 34, 20, 25)
womenStd = (3, 5, 2, 3, 3)

plt.bar(ind, menMeans, width, color='r', yerr=menStd)
plt.bar(ind+width, womenMeans, width, color='y', yerr=womenStd)

plt.title('Men-Women')``````

``````x1 = 10 + 5 * np.random.randn(10000)
x2 = 20 + 5 * np.random.randn(10000)

num_bins = 50
plt.hist(x1, num_bins, normed=1, facecolor='green', alpha=0.5)
plt.hist(x2, num_bins, normed=1, facecolor='blue', alpha=0.5)
plt.title('Histogram')
plt.show()``````

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

x = np.linspace(0, 10, num=100)
y = np.sin(x)
plt.plot(x,y)
plt.show()``````

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

x = np.random.randn(1000)
y = np.random.randn(1000)
plt.scatter(x,y)
plt.title('Scatter')
plt.show()``````

jerryhuose
+ 关注

corcosa 12828人浏览