中文亂碼問題
from matplotlib.font_manager import FontProperties
myfont = FontProperties(fname='/System/Library/Fonts/PingFang.ttc', size=14)
# plt.rcParams['font.sans-serif']=['SimHei'] #用來正常顯示中文標籤
plt.rcParams['axes.unicode_minus']=False #用來正常顯示負號
plt.plot(x, y_precision_product, '*-', label=u"準確率(%)")
plt.plot(x, y_precision_susp, 'm--', label=u"(%)")
plt.ylabel(u"準確率(%)", fontproperties=myfont, fontsize=18)
plt.xlabel(u"閾值", fontproperties=myfont, fontsize=18)
plt.title(u"性能與閾值關係", fontproperties=myfont, fontsize=28, color='blue')
plt.legend(prop=myfont)
繪圖教程鏈接:
python繪製三維圖
二維圖實例
X = np.linspace(-np.pi, np.pi, 256, endpoint=True)
COS = np.cos(X)
EXP = 1 - np.exp(X - 1)
COM = 1 - np.exp(COS - 1)
fig = plt.figure(figsize=(20, 7))
plt.subplot(131)
plt.plot(X, COS, label='cos(x)', linewidth='3')
plt.fill_between(X, COS, where=(0 < X) & (X < np.pi), facecolor='#acc2d9')
# plt.xlim(-np.pi, np.pi)
# plt.xlabel(u"x")
# plt.ylabel(u"y")
plt.xticks(fontsize=16)
plt.yticks(fontsize=16)
plt.grid(True, which='major')
plt.legend(fontsize=14)
plt.subplot(132)
plt.plot(X, EXP, label='1 - np.exp(x - 1)', linewidth='3')
# plt.xlim(-np.pi, np.pi)
# plt.ylim(-8, 2)
# plt.xlabel(u"x")
# plt.ylabel(u"y")
plt.xticks(fontsize=16)
plt.yticks(fontsize=16)
plt.grid(True, which='major')
plt.legend(fontsize=14)
plt.subplot(133)
plt.plot(X, COM, label='1 - np.exp(cos(x) - 1)', linewidth='3')
plt.fill_between(X, COM, where=(0 < X) & (X < np.pi), facecolor='#acc2d9')
# plt.xlim(-np.pi, np.pi)
# plt.xlabel(u"x")
# plt.ylabel(u"y")
plt.xticks(fontsize=16)
plt.yticks(fontsize=16)
plt.grid(True, which='major')
plt.legend(fontsize=14)
plt.show()
顏色表:
三維畫圖實例
in_file = "trace_data.xls"
radar_pos_arr = [[80, 0, 0], [30, 60, 0], [55, 110, 0], [105, 110, 0], [130, 60, 0]]
trace_pos_arr = _read_excel_file(in_file)
# fig = plt.figure(figsize=(18, 7))
# ax = fig.add_subplot(131, projection='3d')
fig = plt.figure(figsize=(10, 7))
# plt.title('Minimize Number of UAV Solution')
ax = fig.add_subplot(111, projection='3d')
X = np.arange(-4, 4, 0.25)
Y = np.arange(-4, 4, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X ** 2 + Y ** 2)
Z = np.sin(R)
x = [k[0] / 1000 for k in trace_pos_arr]
y = [k[1] / 1000 for k in trace_pos_arr]
z = [k[2] / 1000 for k in trace_pos_arr]
ax.set_xlabel('X (km)')
ax.set_ylabel('Y (km)')
ax.set_zlabel('Z (km)')
# 具體函數方法可用 help(function) 查看,如:help(ax.plot_surface)
ax.plot(x, y, z, c='g', label='UAV Trajectory')
x = [k[0] for k in radar_pos_arr]
y = [k[1] for k in radar_pos_arr]
z = [k[2] for k in radar_pos_arr]
# # 遍歷每一個點,使用text將y值顯示
# for i, j in zip(x, y):
# ax.annotate("%d" % (i), xy=(i, j))
for i, j, k in zip(x, y, z):
label = '(%d, %d, %d)' % (i, j, k)
ax.text(i, j, k, label)
ax.scatter(x, y, z, c='r', label='Radars Position')
ax.legend()
plt.show()
例子
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [1, 4, 9, 16]
plt.plot(x, y)
plt.show()
設置折線點的屬性
import numpy as np
import matplotlib.pyplot as plt
# evenly sampled time at 200ms intervals
t = np.arange(0., 5., 0.2)
# red dashes, blue squares and green triangles
plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^-')
plt.show()
解決中文編碼問題
import matplotlib.font_manager as fm
myfont = fm.FontProperties(fname='C:/Windows/Fonts/msyh.ttc')
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [1, 4, 9, 16]
plt.plot(x, y, 'rs-')
plt.xlabel('橫座標軸', fontproperties=myfont, fontsize=30, color='black')
plt.ylabel('縱座標軸', fontproperties=myfont, fontsize=30, color='black')
plt.title("解決中文編碼問題",fontproperties=myfont, fontsize=28, color='black')
plt.show()
在點上面打標籤
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [1, 4, 9, 16]
plt.plot(x, y, 'rs-')
for xy in zip(x, y):
plt.annotate("(%s, %s)" % xy, xy=xy, xytext=(-40, 8), textcoords='offset points')
plt.show()
多條折線添加標註
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y1 = [1, 4, 9, 16]
y2 = [1, 8, 27, 64]
plt.plot(x, y1, 'rs-', label='two')
plt.plot(x, y2, 'bs-', label='three')
plt.legend(loc='lower right')
plt.show()