Python数据可视化Matplotlib——Figure画布背景设置

之前在今日头条中更新了几期的Matplotlib教学短视频,在圈内受到了广泛好评,现应大家要求,将视频中的代码贴出来,方便大家学习。

为了使实例图像显得不单调,我们先将绘图代码贴上来,此处代码对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设置单一色彩背景通常有两种方法:

  1. 创建Figure对象时给定facecolor关键字参数值
    fig = plt.figure(facecolor='snow')
  2. 使用Figure对象的set_facecolor方法
fig = plt.figure()
fig.set_facecolor('blueviolet')

方法一代码及图像

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设置复合色彩背景步骤:

  1. 创建色彩数组
    a = [np.linspace(0,1,1600)]*1600
  2. 通过Figure对象的figimage方法中的cmap关键字设定要设定的背景色彩
    fig.figimage(a, cmap= plt.get_cmap('autumn'))

代码及图像:

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设置图像背景步骤:

  1. 将图像文件转换成数组
    bgimg = img.imread('./world.png')
  2. 通过Figure对象的figimage方法将图像设置为背景
    fig.figimage(bgimg)

代码及图像:

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小讲堂

posted @ 2017-08-10 12:24  kallan  阅读(21062)  评论(0编辑  收藏  举报
本文版权归作者和博客园共有,未经作者同意禁止转载,否则保留追究法律责任的权利。