8000 Improve custom figure example (#14499) · matplotlib/matplotlib@e1f0101 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e1f0101

Browse files
authored
Improve custom figure example (#14499)
Improve custom figure example
2 parents 9d20afd + 8ee355c commit e1f0101

File tree

2 files changed

+44
-15
lines changed

2 files changed

+44
-15
lines changed

.flake8

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ per-file-ignores =
219219
examples/style_sheets/plot_solarizedlight2.py: E501
220220
examples/subplots_axes_and_figures/axes_margins.py: E402
221221
examples/subplots_axes_and_figures/axes_zoom_effect.py: E402
222+
examples/subplots_axes_and_figures/custom_figure_class.py: E402
222223
examples/subplots_axes_and_figures/demo_constrained_layout.py: E402
223224
examples/subplots_axes_and_figures/demo_tight_layout.py: E402
224225
examples/subplots_axes_and_figures/secondary_axis.py: E402
Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,55 @@
11
"""
2-
===================
3-
Custom Figure Class
4-
===================
2+
========================
3+
Custom Figure subclasses
4+
========================
55
6-
You can pass a custom Figure constructor to figure if you want to derive from
7-
the default Figure. This simple example creates a figure with a figure title.
6+
You can pass a `.Figure` subclass to `.pyplot.figure` if you want to change
7+
the default behavior of the figure.
8+
9+
This example defines a `.Figure` subclass ``WatermarkFigure`` that accepts an
10+
additional parameter ``watermark`` to display a custom watermark text. The
11+
figure is created using the ``FigureClass`` parameter of `.pyplot.figure`.
12+
The additional ``watermark`` parameter is passed on to the subclass
13+
constructor.
814
"""
915

1016
import matplotlib.pyplot as plt
1117
from matplotlib.figure import Figure
18+
import numpy as np
19+
1220

21+
class WatermarkFigure(Figure):
22+
"""A figure with a text watermark."""
1323

14-
class MyFigure(Figure):
15-
def __init__(self, *args, figtitle='hi mom', **kwargs):
16-
"""
17-
custom kwarg figtitle is a figure title
18-
"""
24+
def __init__(self, *args, watermark=None, **kwargs):
1925
super().__init__(*args, **kwargs)
20-
self.text(0.5, 0.95, figtitle, ha='center')
26+
27+
if watermark is not None:
28+
bbox = dict(boxstyle='square', lw=3, ec='gray',
29+
fc=(0.9, 0.9, .9, .5), alpha=0.5)
30+
self.text(0.5, 0.5, watermark,
31+
ha='center', va='center', rotation=30,
32+
fontsize=40, color='gray', alpha=0.5, bbox=bbox)
33+
34+
35+
x = np.linspace(-3, 3, 201)
36+
y = np.tanh(x) + 0.1 * np.cos(5 * x)
37+
38+
plt.figure(FigureClass=WatermarkFigure, watermark='draft')
39+
plt.plot(x, y)
2140

2241

23-
fig = plt.figure(FigureClass=MyFigure, figtitle='my title')
24-
ax = fig.subplots()
25-
ax.plot([1, 2, 3])
42+
#############################################################################
43+
#
44+
# ------------
45+
#
46+
# References
47+
# """"""""""
48+
#
49+
# The use of the following functions, methods, classes and modules is shown
50+
# in this example:
2651

27-
plt.show()
52+
import matplotlib
53+
matplotlib.pyplot.figure
54+
matplotlib.figure.Figure
55+
matplotlib.figure.Figure.text

0 commit comments

Comments
 (0)
0