注解
Click here 下载完整的示例代码
自定义图形子类¶
你可以通过 Figure
子类到 pyplot.figure
如果要更改地物的默认行为。
此示例定义了 Figure
子类 WatermarkFigure
它接受一个附加参数 watermark
显示自定义水印文本。图形是使用 FigureClass
参数 pyplot.figure
. 额外的 watermark
参数传递给子类构造函数。
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
import numpy as np
class WatermarkFigure(Figure):
"""A figure with a text watermark."""
def __init__(self, *args, watermark=None, **kwargs):
super().__init__(*args, **kwargs)
if watermark is not None:
bbox = dict(boxstyle='square', lw=3, ec='gray',
fc=(0.9, 0.9, .9, .5), alpha=0.5)
self.text(0.5, 0.5, watermark,
ha='center', va='center', rotation=30,
fontsize=40, color='gray', alpha=0.5, bbox=bbox)
x = np.linspace(-3, 3, 201)
y = np.tanh(x) + 0.1 * np.cos(5 * x)
plt.figure(FigureClass=WatermarkFigure, watermark='draft')
plt.plot(x, y)
出:
[<matplotlib.lines.Line2D object at 0x7faa0a8b29b0>]
工具书类¶
以下函数、方法、类和模块的使用如本例所示:
import matplotlib
matplotlib.pyplot.figure
matplotlib.figure.Figure
matplotlib.figure.Figure.text
出:
<function Figure.text at 0x7faa00cddea0>
关键词:matplotlib代码示例,codex,python plot,pyplot Gallery generated by Sphinx-Gallery