之前在今日头条中更新了几期的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小讲堂

Python数据可视化Matplotlib——Figure画布背景设置的更多相关文章

  1. python数据可视化-matplotlib入门(7)-从网络加载数据及数据可视化的小总结

    除了从文件加载数据,另一个数据源是互联网,互联网每天产生各种不同的数据,可以用各种各样的方式从互联网加载数据. 一.了解 Web API Web 应用编程接口(API)自动请求网站的特定信息,再对这些 ...

  2. Python数据可视化matplotlib和seaborn

    Python在数据科学中的地位,不仅仅是因为numpy, scipy, pandas, scikit-learn这些高效易用.接口统一的科学计算包,其强大的数据可视化工具也是重要组成部分.在Pytho ...

  3. Python数据可视化--matplotlib

    抽象化|具体化: 如盒形图 | 现实中的图 功能性|装饰性:没有装饰和渲染 | 包含艺术性美学上的装饰 深度表达|浅度表达:深入层次的研究探索数据 | 易于理解的,直观的表示 多维度|单一维度:数据的 ...

  4. python数据可视化-matplotlib入门(5)-饼图和堆叠图

    饼图常用于统计学模块,画饼图用到的方法为:pie( ) 一.pie()函数用来绘制饼图 pie(x, explode=None, labels=None, colors=None, autopct=N ...

  5. python数据可视化-matplotlib入门(6)-从文件中加载数据

    前几篇都是手动录入或随机函数产生的数据.实际有许多类型的文件,以及许多方法,用它们从文件中提取数据来图形化. 比如之前python基础(12)介绍打开文件的方式,可直接读取文件中的数据,扩大了我们的数 ...

  6. python数据可视化——matplotlib 用户手册入门:pyplot 画图

    参考matplotlib官方指南: https://matplotlib.org/tutorials/introductory/pyplot.html#sphx-glr-tutorials-intro ...

  7. python数据可视化——matplotlib 用户手册入门:使用指南

    参考matplotlib官方指南: https://matplotlib.org/tutorials/introductory/usage.html#sphx-glr-tutorials-introd ...

  8. Python数据可视化——使用Matplotlib创建散点图

    Python数据可视化——使用Matplotlib创建散点图 2017-12-27 作者:淡水化合物 Matplotlib简述: Matplotlib是一个用于创建出高质量图表的桌面绘图包(主要是2D ...

  9. Python数据可视化基础讲解

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:爱数据学习社 首先,要知道我们用哪些库来画图? matplotlib ...

随机推荐

  1. windows环境下,怎么解决无法使用ping命令

    基本都是因为"环境变量"导致的,查看环境变量path在"Path"中追加"C:\Windows\System32"

  2. laydate时间插件更换皮肤

    <script> ;!function(){ laydate.skin('molv'); laydate({ elem: '#demo' }) }();</script>

  3. docker中执行sed: can't move '/etc/resolv.conf73UqmG' to '/etc/resolv.conf': Device or resource busy错误的处理原因及方式

    错误现象 在docker容器中想要修改/etc/resolv.conf中的namesever,使用sed命令进行执行时遇到错误: / # sed -i 's/192.168.1.1/192.168.1 ...

  4. 算法设计与分析 上机题Mergesort

    #include <iostream>using namespace std; #define N 100 int g_array[N];     //存放输入的数字static int ...

  5. 业余草推荐18个Java开源免费的CMS系统

    1.InfoGlue infoglue是一个高级的.可扩展的.健壮的内容管理系统,完全用Java开发.重要的功能包括完全支持多语言,站点之间良好的重用,以及广泛的集成能力. 该项目主页:http:// ...

  6. Java 基本语法----关键字、标识符

    关键字 关键字的定义和特点 定义:被Java语言赋予了特殊含义,用做专门用途的字符串(单词)特点:关键字中所有字母都为小写 用于定义数据类型的关键字 class interface enum byte ...

  7. Django学习(九)---Templates过滤器及Django shell和Admin增强

    一.Templates过滤器 过滤器属于django模板语言 修改模板中的变量,从而显示不同内容 {{ value | filter }} 举例:{{ list_nums | length}}    ...

  8. Python中创建对象的方法

    源引:Python编程实践 示例类: class Point: __slots__=('x','y') def __init__(self,x,y): self.x=x self.y=y def ma ...

  9. USB的包结构及包分类

    USB的传输总是低位在前,高位在后. USB的传输方向:从设备到主机的数据为输入:从主机到设备的数据叫做输出. 1. 包结构 以同步域开始,紧跟着一个包标识符PID(Packet Identifier ...

  10. C++第四篇--重载_指针_引用

    C++第四篇--重载_指针_引用 1. 基础知识 重载:函数名相同,根据参数不同(类型.数量.顺序不同)调用同名函数 指针和引用:引用就是别名,引用时必须初始化,引用你定义的变量. int a; in ...