Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 52adc3f

Browse files
committed
Use rc() less often in examples/tutorials.
If a user learns rcParams.update / dict.update, they'll know how to merge any dicts, whereas rc() is a matplotlib-specific API which is hardly shorter (and needing star-unpacking in the case of tutorials/text/usetex.py, which makes things worse overall). Also it's often better to just pass the argument to the single function call (`text(..., usetex=True)`, `imshow(..., origin="lower")`) which avoids unnecessarily touching global state.
1 parent dc1f0d9 commit 52adc3f

File tree

5 files changed

+15
-17
lines changed

5 files changed

+15
-17
lines changed

examples/misc/multipage_pdf.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
pdf.savefig() # saves the current figure into a pdf page
2727
plt.close()
2828

29-
# if LaTeX is not installed or error caught, change to `usetex=False`
30-
plt.rc('text', usetex=True)
29+
# if LaTeX is not installed or error caught, change to `False`
30+
plt.rcParams['text.usetex'] = True
3131
plt.figure(figsize=(8, 6))
3232
x = np.arange(0, 5, 0.1)
3333
plt.plot(x, np.sin(x), 'b-')
@@ -36,7 +36,7 @@
3636
pdf.savefig()
3737
plt.close()
3838

39-
plt.rc('text', usetex=False)
39+
plt.rcParams['text.usetex'] = False
4040
fig = plt.figure(figsize=(4, 5))
4141
plt.plot(x, x ** 2, 'ko')
4242
plt.title('Page Three')

examples/text_labels_and_annotations/mathtext_asarray.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
import matplotlib.mathtext as mathtext
1010
import matplotlib.pyplot as plt
11-
import matplotlib
12-
matplotlib.rc('image', origin='upper')
1311

1412
parser = mathtext.MathTextParser("Bitmap")
1513
parser.to_png('test2.png',

examples/text_labels_and_annotations/usetex_fonteffects.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
are now supported in pdf usetex.
88
"""
99

10-
import matplotlib
1110
import matplotlib.pyplot as plt
12-
matplotlib.rc('text', usetex=True)
1311

1412

1513
def setfont(font):
@@ -22,7 +20,7 @@ def setfont(font):
2220
['Nimbus Roman No9 L ' + x for x in
2321
['', 'Italics (real italics for comparison)',
2422
'(slanted)', '(condensed)', '(extended)']]):
25-
plt.text(0, y, setfont(font) + text)
23+
plt.text(0, y, setfont(font) + text, usetex=True)
2624

2725
plt.ylim(-1, 5)
2826
plt.xlim(-0.2, 0.6)

examples/user_interfaces/embedding_in_wx3_sgskip.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
Thanks to matplotlib and wx teams for creating such great software!
2222
"""
2323

24-
import matplotlib
2524
import matplotlib.cm as cm
2625
import matplotlib.cbook as cbook
2726
from matplotlib.backends.backend_wxagg import (
@@ -36,9 +35,6 @@
3635
ERR_TOL = 1e-5 # floating point slop for peak-detection
3736

3837

39-
matplotlib.rc('image', origin='lower')
40-
41-
4238
class PlotPanel(wx.Panel):
4339
def __init__(self, parent):
4440
wx.Panel.__init__(self, parent, -1)
@@ -64,7 +60,7 @@ def init_plot_data(self):
6460
y = np.arange(100.0) * 2 * np.pi / 50.0
6561
self.x, self.y = np.meshgrid(x, y)
6662
z = np.sin(self.x) + np.cos(self.y)
67-
self.im = ax.imshow(z, cmap=cm.RdBu)
63+
self.im = ax.imshow(z, cmap=cm.RdBu, origin='lower')
6864

6965
zmax = np.max(z) - ERR_TOL
7066
ymax_i, xmax_i = np.nonzero(z >= zmax)

tutorials/text/usetex.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,17 @@
4545
To use LaTeX and select Helvetica as the default font, without editing
4646
matplotlibrc use::
4747
48-
from matplotlib import rc
49-
rc('font', **{'family': 'sans-serif', 'sans-serif': ['Helvetica']})
48+
import matplotlib as mpl
49+
plt.rcParams.update({
50+
"text.usetex": True,
51+
"font.family": "sans-serif",
52+
"font.sans-serif": ["Helvetica"]})
5053
## for Palatino and other serif fonts use:
51-
# rc('font', **{'family': 'serif', 'serif': ['Palatino']})
52-
rc('text', usetex=True)
54+
plt.rcParams.update({
55+
"text.usetex": True,
56+
"font.family": "serif",
57+
"font.serif": ["Palatino"],
58+
})
5359
5460
Here is the standard example,
5561
:file:`/gallery/text_labels_and_annotations/tex_demo`:

0 commit comments

Comments
 (0)