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 8ee355c

Browse filesBrowse files
committed
Improve custom figure example
1 parent efafb6c commit 8ee355c
Copy full SHA for 8ee355c

File tree

Expand file treeCollapse file tree

2 files changed

+44
-15
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+44
-15
lines changed

‎.flake8

Copy file name to clipboardExpand all lines: .flake8
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ per-file-ignores =
223223
examples/style_sheets/plot_solarizedlight2.py: E501
224224
examples/subplots_axes_and_figures/axes_margins.py: E402
225225
examples/subplots_axes_and_figures/axes_zoom_effect.py: E402
226+
examples/subplots_axes_and_figures/custom_figure_class.py: E402
226227
examples/subplots_axes_and_figures/demo_constrained_layout.py: E402
227228
examples/subplots_axes_and_figures/demo_tight_layout.py: E402
228229
examples/subplots_axes_and_figures/secondary_axis.py: E402
+43-15Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.