以前在今日頭條中更新了幾期的Matplotlib教學短視頻,在圈內受到了普遍好評,現應你們要求,將視頻中的代碼貼出來,方便你們學習。python
爲了使實例圖像顯得不單調,咱們先將繪圖代碼貼上來,此處代碼對Figure背景設置無影響。數組
import numpy as np import matplotlib.pyplot as plt import matplotlib.cm as cm import matplotlib.image as img from matplotlib.font_manager import FontProperties # 顯示數學公式 def add_math_background(fig): ax = fig.add_axes([0.3, 0.25, 0.5, 0.5]) text = [] text.append( (r"$W^{3\beta}_{\delta_1 \rho_1 \sigma_2} = " r"U^{3\beta}_{\delta_1 \rho_1} + \frac{1}{8 \pi 2}" r"\int^{\alpha_2}_{\alpha_2} d \alpha^\prime_2 " r"\left[\frac{ U^{2\beta}_{\delta_1 \rho_1} - " r"\alpha^\prime_2U^{1\beta}_{\rho_1 \sigma_2} " r"}{U^{0\beta}_{\rho_1 \sigma_2}}\right]$", (0.6, 0.3), 20)) text.append((r"$\frac{d\rho}{d t} + \rho \vec{v}\cdot\nabla\vec{v} " r"= -\nabla p + \mu\nabla^2 \vec{v} + \rho \vec{g}$", (0.45, 0.7), 20)) text.append((r"$\int_{-\infty}^\infty e^{-x^2}dx=\sqrt{\pi}$", (0.25, 0.4), 25)) text.append((r"$F_G = G\frac{m_1m_2}{r^2}$", (0.75, 0.6), 30)) for eq, (x, y), size in text: ax.text(x, y, eq, ha='center', va='center', color="#11557c", alpha=0.25, transform=ax.transAxes, fontsize=size) ax.set_axis_off() return ax # 顯示Matplotlib小講堂 def add_matplotlib_text(ax,color): font=FontProperties(fname=r"/Library/Fonts/Songti.ttc", size=85) ax.text(0.55, 0.6, 'matplotlib', color=color,size=35, ha='right', va='bottom', alpha=1.0, transform=ax.transAxes) ax.text(0.55, 0.45, u'小講堂', color=color,fontproperties=font, ha='center', va='center', alpha=1.0, transform=ax.transAxes) # 極座標圖像 def add_polar_bar(fig): ax = fig.add_axes([0.25, 0.4, 0.2, 0.2], projection='polar') ax.axesPatch.set_alpha(0.05) ax.set_axisbelow(True) N = 7 arc = 2. * np.pi theta = np.arange(0.0, arc, arc/N) radii = 10 * np.array([0.2, 0.6, 0.8, 0.7, 0.4, 0.5, 0.8]) width = np.pi / 4 * np.array([0.4, 0.4, 0.6, 0.8, 0.2, 0.5, 0.3]) bars = ax.bar(theta, radii, width=width, bottom=0.0) for r, bar in zip(radii, bars): bar.set_facecolor(cm.jet(r/10.)) bar.set_alpha(0.6) for label in ax.get_xticklabels() + ax.get_yticklabels(): label.set_visible(False) for line in ax.get_ygridlines() + ax.get_xgridlines(): line.set_lw(0.8) line.set_alpha(0.9) line.set_ls('-') line.set_color('0.5') ax.set_yticks(np.arange(1, 9, 2)) ax.set_rmax(9) def pltfig(fig,color='#11557c'): main_axes = add_math_background(fig) add_polar_bar(fig) add_matplotlib_text(main_axes,color) if __name__ == '__main__': fig = plt.figure(figsize=(16, 8)) pltfig(fig) plt.show()
Figure設置單一色彩背景一般有兩種方法:微信
fig = plt.figure(facecolor='snow')
fig = plt.figure() fig.set_facecolor('blueviolet')
方法一代碼及圖像app
if __name__ == '__main__': fig = plt.figure(figsize=(16, 8),facecolor='snow') pltfig(fig) plt.show()
方法二代碼及圖像學習
if __name__ == '__main__': fig = plt.figure(figsize=(16, 8)) fig.set_facecolor('blueviolet') pltfig(fig) plt.show()
Figure設置複合色彩背景步驟:spa
a = [np.linspace(0,1,1600)]*1600
fig.figimage(a, cmap= plt.get_cmap('autumn'))
代碼及圖像:3d
if __name__ == '__main__': fig = plt.figure(figsize=(16, 8)) a = [np.linspace(0,1,1600)]*1600 fig.figimage(a, cmap= plt.get_cmap('autumn')) pltfig(fig) plt.show()
Figure設置圖像背景步驟:code
代碼及圖像:orm
if __name__ == '__main__': fig = plt.figure(figsize=(16, 8)) bgimg = img.imread('./world.png') fig.figimage(bgimg) pltfig(fig) plt.show()
視頻地址視頻
想觀看Matplotlib教學視頻,瞭解更多Matplotlib實用技巧可關注
微信公衆帳號: MatplotlibClass
今日頭條號:Matplotlib小講堂