import numpy as np
import matplotlib
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm
from mpl_toolkits.mplot3d import Axes3D複製代碼
myfont = fm.FontProperties(fname="字體文件路徑")複製代碼
matplotlib.rcParams['axes.unicode_minus']=False複製代碼
x = np.linspace(-np.pi, np.pi, 256, endpoint=True) # 從-π到π 等間隔取256個點
y_cos, y_sin = np.cos(x), np.sin(x) # 對應x的cos與sin值複製代碼
plt.figure(figsize=(8, 6), dpi=80) # figsize定義畫布大小,dpi定義畫布分辨率
plt.title("簡單折線圖", fontproperties=myfont) # 設定標題,中文須要指定字體
plt.grid(True) # 是否顯示網格複製代碼
# 設置X軸
plt.xlabel("X軸", fontproperties=myfont) # 軸標籤
plt.xlim(-4.0, 4.0) # 軸範圍
plt.xticks(np.linspace(-4, 4, 9, endpoint=True)) # 軸刻度
# 設置Y軸
plt.ylabel("Y軸", fontproperties=myfont)
plt.ylim(-1.0, 1.0)
plt.yticks(np.linspace(-1, 1, 9, endpoint=True))複製代碼
線類型有幾種:"g+-", "r*-", "b.-", "yo-",第一個字表明顏色,第二個字符表明節點樣式,第三個字符表明連線樣式程序員
plt.plot(x, y_cos, "b--", linewidth=2.0, label="cos示例") # 前兩個參數是座標值,第三個參數爲線類型,linewidth爲線寬,label爲圖例文字
plt.plot(x, y_sin, "g-", linewidth=2.0, label="sin示例")複製代碼
plt.legend(loc="upper left", prop=myfont, shadow=True) # loc能夠是upper、lower和left, right, center的組合複製代碼
plt.show()複製代碼
plt.fill_between(x, -1, y_sin, where=True, color="blue", alpha=0.25)
plt.show()複製代碼
# 生成測試數據
x = np.linspace(0, 1, 1000)
y = np.linspace(0, 1, 1000)
z = np.sin(x * 2 * np.pi) / (y + 0.1)
# 生成畫布(兩種形式)
fig = plt.figure()
ax = fig.gca(projection="3d", title="plot title")
# ax = fig.add_subplot(111, projection="3d", title="plot title")
# 畫三維折線圖
ax.plot(x, y, z, color="red", linestyle="-")
# 設置座標軸圖標
ax.set_xlabel("X Label")
ax.set_ylabel("Y Label")
ax.set_zlabel("Z Label")
# 圖形顯示
plt.show()複製代碼
# 生成測試數據
means_men = np.array((20, 35, 30, 35, 27))
means_women = np.array((25, 32, 34, 20, 25))複製代碼
plt.figure(figsize=(8, 6), dpi=80) # figsize定義畫布大小,dpi定義畫布分辨率
plt.title("簡單柱狀圖", fontproperties=myfont) # 設定標題,中文須要指定字體
plt.grid(True) # 是否顯示網格複製代碼
index = np.arange(len(means_men)) [0,1,2,3,4]
bar_height = 0.35 # 柱寬度
plt.xlim(0, 45) # 軸範圍
plt.xlabel("Scores") # 軸標籤
plt.ylabel("Group")
plt.yticks(index + (bar_height / 2), ("A", "B", "C", "D", "E")) # 軸刻度複製代碼
# 繪製橫向柱狀圖
plt.barh(index, means_men, height=bar_height, alpha=0.2, color="b", label="Men")
plt.barh(index + bar_height, means_women, height=bar_height, alpha=0.8, color="r", label="Women")複製代碼
plt.legend(loc="upper right", shadow=True)複製代碼
for i, v in zip(index, means_men):
plt.text(v + 0.3, i, v, ha="left", va="center")
for i, v in zip(index, means_women):
plt.text(v + 0.3, i + bar_height, v, ha="left", va="center")複製代碼
plt.show()複製代碼
index = np.arange(len(means_men)) [0,1,2,3,4]
bar_height = 0.35 # 柱寬度
plt.ylim(0, 45)
plt.ylabel("Scores")
plt.xlabel("Group")
plt.xticks(index + (bar_height / 2), ("A", "B", "C", "D", "E"))複製代碼
# 繪製豎向柱狀圖
plt.bar(index-bar_height/2, means_men, width=bar_height, alpha=0.4, color="b", label="Men")
plt.bar(index+bar_height/2, means_women, width=bar_height, alpha=0.4, color="r", label="Women")複製代碼
plt.show()複製代碼
# 生成測試數據
data = np.array([
[1, 4, 2, 5, 2],
[2, 1, 1, 3, 6],
[5, 3, 6, 4, 1]
])
# 設置標題
plt.title("層次柱狀圖", fontproperties=myfont)
# 設置相關參數
index = np.arange(len(data[0]))
color_index = ["r", "g", "b"]
# 聲明底部位置
bottom = np.array([0, 0, 0, 0, 0])
# 依次畫圖,並更新底部位置
for i in range(len(data)):
plt.bar(index, data[i], width=0.5, color=color_index[i], bottom=bottom, alpha=0.7, label="標籤 %d" % i)
bottom += data[i]
# 設置圖例位置
plt.legend(loc="upper left", prop=myfont, shadow=True)
# 圖形顯示
plt.show()複製代碼
# 生成測試數據
mu, sigma = 100, 15
x = mu + sigma * np.random.randn(10000)
# 設置標題
plt.title("直方圖", fontproperties=myfont)
# 畫直方圖, 並返回相關結果
n, bins, patches = plt.hist(x, bins=50, density=1, cumulative=False, color="green", alpha=0.6, label="直方圖")
# # 根據直方圖返回的結果, 畫折線圖
y = mlab.normpdf(bins, mu, sigma)
plt.plot(bins, y, "r--", label="線條")
# 設置圖例位置
plt.legend(loc="upper left", prop=myfont, shadow=True)
# 圖形顯示
plt.show()複製代碼
# 生成測試數據(位置數據)
xpos = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
ypos = [2, 3, 4, 5, 1, 6, 2, 1, 7, 2]
zpos = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
# 生成測試數據(柱形參數)
dx = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
dy = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
dz = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# 生成畫布(兩種形式)
fig = plt.figure()
ax = fig.gca(projection="3d", title="plot title")
# 設置座標軸圖標
ax.set_xlabel("X Label")
ax.set_ylabel("Y Label")
ax.set_zlabel("Z Label")
# 畫三維柱狀圖
ax.bar3d(xpos, ypos, zpos, dx, dy, dz, alpha=0.5)
# 圖形顯示
plt.show()複製代碼
# 生成測試數據
sizes = [15, 30, 45, 10] # 數值
labels = ["Frogs", "中文", "Dogs", "Logs"] # 標籤
colors = ["yellowgreen", "gold", "lightskyblue", "lightcoral"] # 顏色複製代碼
plt.figure(figsize=(8, 6), dpi=80) # figsize定義畫布大小,dpi定義畫布分辨率
plt.title("簡單餅狀圖", fontproperties=myfont) # 設定標題,中文須要指定字體複製代碼
explode = [0, 0.05, 0, 0]複製代碼
patches, l_text, p_text = plt.pie(sizes, explode=explode, labels=labels, colors=colors, autopct="%1.1f%%", shadow=True, startangle=90) # autopct設置顯示百分比的格式,startangle設置圖像轉動方向
for text in l_text:
text.set_fontproperties(myfont) # 設置字體,避免中文亂碼複製代碼
plt.show()複製代碼
N = 1000
x = np.random.randn(N)
y = x + np.random.randn(N)*0.5複製代碼
plt.figure(figsize=(8, 6), dpi=80) # figsize定義畫布大小,dpi定義畫布分辨率
plt.title("簡單散點圖", fontproperties=myfont) # 設定標題,中文須要指定字體複製代碼
plt.scatter(x, y, s=5, c="red", marker="o") # s表示點的大小,c表示點的顏色,marker表示點的形狀複製代碼
plt.show()複製代碼
# 生成測試數據
x = np.random.random(100)
y = np.random.random(100)
z = np.random.random(100)
color = np.random.random(100)
scale = np.random.random(100) * 100
# 生成畫布(兩種形式)
fig = plt.figure()
fig.suptitle("三維散點圖", fontproperties=myfont)
ax = fig.add_subplot(111, projection="3d")
# 設置座標軸圖標
ax.set_xlabel("X Label")
ax.set_ylabel("Y Label")
ax.set_zlabel("Z Label")
# 設置座標軸範圍
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.set_zlim(0, 1)
# 畫三維散點圖
ax.scatter(x, y, z, s=scale, c=color, marker=".")
# 圖形顯示
plt.show()複製代碼
labels = np.array(["A組", "B組", "C組", "D組", "E組", "F組"])
data = np.array([68, 83, 90, 77, 89, 73])
theta = np.linspace(0, 2 * np.pi, len(data), endpoint=False) # 每一個維度的角度值複製代碼
plt.subplot(111, polar=True) # 3個數字,前兩位表示把畫布分爲幾行幾列,後一位表示花在哪一個位置上
plt.title("雷達圖", fontproperties=myfont)複製代碼
plt.ylim(0, 100) # 軸範圍複製代碼
plt.thetagrids(theta * (180 / np.pi), labels=labels, fontproperties=myfont)複製代碼
plt.show()複製代碼
想進一步瞭解編程開發相關知識,與我一同成長進步,請關注個人公衆號「松果倉庫」,共同分享宅&程序員的各種資源,謝謝!!!編程