# matplotlib 可以創建出版書籍中的繪圖工具包
import matplotlib.pyplot as plt
import numpy as np
# 正態直方圖
import scipy
from scipy import stats
# 創建一維數組
arr = np.random.rand(100)
arr = np.arange(100)
# 折線圖
plt.plot(arr)
plot.show()
x = np.linspace(-5, 15, 50)
# 正態分佈線圖
plt.plot(x, stats.norm.pdf(x=x, loc=5, scale=2))
# hist直方圖
# 1.數據 2.bins 柱子的數量(寬度 值越大寬度越窄)
# loc 均值 標準差 alpha 透明度
plt.hist(stats.norm.rvs(loc=5, scale=2, size=1000), bins=50, normed=True, color='r', alpha=0.7)
# 這是一個簡單的直方圖
# 1.準備數據
arr = np.random.randn(100)
plt.hist(arr, bins=50, normed=True, color='b', alpha=0.7)
# 散點圖
x = np.arange(50)
y = x+5 * np.random.randn(50)
plt.scatter(x, y)
# 正弦餘弦圖
x = np.linspace(-2*np.pi, 2*np.pi, 100)
c = np.cos(x)
s = np.sin(x)
t = np.tan(x)
plt.plot(x, c)
plt.plot(x, s)
plt.plot(x, t)
# 柱狀圖
x = np.arange(5)
y1 = np.random.randint(1, 25, size=5)
y2 = np.random.randint(1, 20, size=5)
plt.bar(x, y1, 0.25, color='r')
# 在指定的子圖上進行繪圖
ax = plt.subplot(1, 1, 1)
width = 0.25
ax.bar(x, y1, width, color='r')
ax.bar(x+width, y2, width, color='g')
# 指定x軸標籤及位置
ax.set_xticks(x+width-(width/2))
ax.set_xticklabels(['a', 'b', 'c', 'd', 'e'])
ax2 = plt.subplot(2, 2, 2)
ax2.bar(x, y1+5, 0.25, color='g')
# 矩陣圖
m = np.random.rand(10,15)
# plt.cm.gray 灰色 plt.cm.ocean 海洋色
plt.imshow(m, camp=plt.cm.ocean)
# 顏色值
plt.colorbar()
plt.subplots() 獲取一個指定行和列的圖表數組
# 返回的是一個元組, 元組中存放figure對象和子圖數組
fig, sp_arr = plt.subplots(2, 2)
sp_arr[0, 0].hist(np.random.randn(100), bins=10, color='b', alpha=0.5)
sp_arr[0, 1].plot(np.random.randn(100))
sp_arr[1, 0].scatter(np.arange(50), x+5*np.random.randn(50))
fig, axes = plt.subplots(2)
axes[0].plot(np.random.randint(0, 100, 50), 'rp')
axes[1].plot(np.random.randint(0, 100, 50), color='r', linstyle='dashed', marker="*")
fig, ax = plt.subplots(1)
# 設置x軸刻度範圍
ax.set_xlim([0,800])
# 設置顯示的刻度
ax.set_xticks(range(0, 500, 100))
# 設置y軸刻度標籤
ax.set_yticklabels(['', 'python', 'java', 'php'])
# 設置座標軸標籤
ax.set_xlabel('numbers')
ax.set_ylabel('categray')
# 設置圖示標題
ax.set_title('jobs_number')
# label 設置圖例
ax.plot(np.random.randn(1000).cumsum(), label='line1')
ax.plot(np.random.randn(1000).cumsum(), label='line2')
ax.plot(np.random.randn(1000).cumsum(), label='line3')
# 加載圖例
# best 選取最優擺放位置
ax.legend(loc='best')
plt.show()