import numpy as np
import matplotlib.pyplot as plt
np.random.seed(2)
y = np.random.random(10)
z = np.random.random(10)
x = range(len(y))
plt.grid() # 網格
plt.plot(y, c='g', linewidth=1.5, label='plot 1') # 折線圖
plt.plot(z, c='b', linewidth=1.5, label='plot 2') # 折線圖
plt.xlabel('x') # 設置x軸標籤
plt.ylabel('y') # 設置y軸標籤
plt.xlim(-1, 10) # 設置x軸的範圍
plt.ylim(0, 1) # 設置y軸的範圍
plt.scatter(x, y, marker='o', c='r') # 散點
plt.scatter(x, z, marker='s', c='r') # 散點
plt.legend() # 圖例
plt.title('matplotlab') # 設置標題
plt.show()
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(2)
y = np.random.random(10)
x = range(len(y))
plt.grid() # 網格
plt.plot(y, c='g') # 畫折線圖,顏色爲綠色
plt.xlabel('x') # 設置x軸標籤
plt.ylabel('y') # 設置y軸標籤
plt.xticks(x, ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'g')) # 設置x軸刻度
plt.title('matplotlib')
plt.show()
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(2)
y = np.random.random(10)
z = np.random.random(10)
x = range(len(y))
plt.figure(figsize=(10, 5)) # 子圖
plt.subplot(121)
plt.plot(x, y)
plt.grid()
plt.title('fig 1')
plt.xlabel('x')
plt.ylabel('y')
plt.subplot(122)
plt.bar(x, y)
plt.title('fig 2')
plt.xlabel('x')
plt.ylabel('y')
plt.xticks(x, ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'g'))
plt.show()
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(0)
x = range(10)
y = np.random.random(10)
plt.figure(figsize=(10, 4)) # 設置圖的大小
plt.subplots_adjust(wspace=0.3, hspace=0.2) # 設置子圖間距
plt.subplot(121)
plt.grid() # 設置網格
plt.plot(x, y, marker='o', c='k', lw=2, ls='-', label='linear',
markersize=8,
markerfacecolor='gray',
markeredgecolor='k') # 設置標記形狀、線條顏色、線條寬度、線條類型、標籤、標記大小、標記填充顏色、標記邊界顏色
plt.legend() # 圖例
plt.xlabel('x') # x軸標籤
plt.ylabel('y') # y軸標籤
plt.title('plot') # 標題
plt.xticks(x, range(10)) # x軸刻度
plt.subplot(122)
plt.bar(x, y, color='w', edgecolor='k', hatch='//', label='bar') # 設置柱子顏色、邊界顏色、填充形狀、標籤
plt.legend()
plt.xlabel('x')
plt.ylabel('y')
plt.title('bar')
plt.xticks(x, range(10))
plt.show()
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(1)
x = np.random.random((100, 2))
plt.scatter(x[:, 0], x[:, 1], marker='o', c='g') # 散點圖
plt.xlabel('x')
plt.ylabel('y')
plt.title('scatter')
plt.show()
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(1)
x = np.random.randn(100)
plt.hist(x, bins=10, color='g', density=True, edgecolor='black', alpha=0.8, label='hist') # 直方圖
plt.title('hist')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()
import matplotlib.pyplot as plt
import numpy as np
labels = ["A", "B", "C"]
size = [45, 25, 30]
color = ["red", "green", "blue"]
explode = [0.05, 0, 0]
# 餅圖
plt.pie(size, explode=explode, colors=color, labels=labels, labeldistance=1.1, autopct="%1.1f%%", shadow=False, startangle=90, pctdistance=0.6)
plt.axis("equal")
plt.legend()
plt.show()
import matplotlib.pyplot as plt
import matplotlib
# 設置中文字體和字體正常顯示
matplotlib.rcParams['font.sans-serif'] = ['SimHei']
matplotlib.rcParams['axes.unicode_minus'] = False
labels = ['語文', '數學', '英語', '化學']
data1 = [80, 70, 75, 92]
data2 = [95, 60, 70, 90]
x = range(len(labels))
# 條形圖
r1 = plt.bar(left=x, height=data1, width=0.4, alpha=0.7, color='r', label="張三")
r2 = plt.bar(left=[i + 0.4 for i in x], height=data2, width=0.4, alpha=0.7, color='g', label="李四")
plt.ylim(0, 100)
plt.ylabel("成績")
plt.xticks([index + 0.2 for index in x], labels)
plt.xlabel("學生")
plt.title("成績單")
plt.legend()
plt.show()
import matplotlib.pyplot as plt
import numpy as np
x = [10, 20, 22, 15, 28]
y = ['A', 'B', 'C', 'D', 'E']
fig, ax = plt.subplots()
y_ = np.arange(len(y))
ax.barh(y_, x, color='blue', align='center')
ax.set_yticks(y_)
ax.set_yticklabels(y)
ax.set_xlabel('scores')
ax.set_ylabel('features')
ax.set_title('Title')
plt.show()
曲線平滑
import matplotlib.pyplot as plt
import matplotlib
import numpy as np
from scipy import interpolate
matplotlib.rcParams['font.sans-serif'] = ['SimHei']
matplotlib.rcParams['axes.unicode_minus'] = False
x = np.arange(21) # 隨機生成數據x
np.random.seed(4)
y = np.random.randn(21) # 隨機生成數據y
plt.figure(figsize=(10, 5))
plt.suptitle('曲線平滑', fontsize=20) # 設置標題
plt.subplot(221)
plt.scatter(x, y, marker='o', c='b')
plt.plot(x, y, c='b')
plt.xlabel('x')
plt.ylabel('y')
plt.title('原始數據') # 設置子圖標題
plt.subplot(222)
# 擬合
z = np.polyfit(x, y, 20) # deg=20
f = np.poly1d(z)
plt.plot(x, f(x), c='r')
plt.xlabel('x')
plt.ylabel('y')
plt.title('擬合')
plt.subplot(223)
# 插值
xnew = np.arange(0, 20, 0.1)
func = interpolate.interp1d(x, y, kind='cubic')
ynew = func(xnew)
plt.plot(xnew, ynew, c='g')
plt.xlabel('x')
plt.ylabel('y')
plt.title('插值')
plt.subplot(224)
# 組合
plt.plot(x, y, c='b')
plt.plot(x, f(x), c='r')
plt.plot(xnew, ynew, c='g')
plt.xlabel('x')
plt.ylabel('y')
plt.title('組合圖')
# plt.xlim(0, 20)
plt.subplots_adjust(wspace=0.2, hspace=0.5) # 調整子圖間距
plt.show()