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

Skip to content

Commit 09e7dd2

Browse files
committed
Merge pull request matplotlib#1042 from abakan/master
Three more plot_directive configuration options
2 parents 967c15a + 72a8ec4 commit 09e7dd2

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

lib/matplotlib/sphinxext/plot_directive.py

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,23 @@
105105
A dictionary containing any non-standard rcParams that should
106106
be applied before each plot.
107107
108+
plot_apply_rcparams
109+
By default, rcParams are applied when `context` option is not used in
110+
a plot directive. This configuration option overrides this behaviour
111+
and applies rcParams before each plot.
112+
113+
plot_working_directory
114+
By default, the working directory will be changed to the directory of
115+
the example, so the code can get at its data files, if any. Also its
116+
path will be added to `sys.path` so it can import any helper modules
117+
sitting beside it. This configuration option can be used to specify
118+
a central directory (also added to `sys.path`) where data files and
119+
helper modules for all code are located.
120+
121+
plot_template
122+
Provide a customized template for preparing resturctured text.
123+
124+
108125
"""
109126
from __future__ import print_function
110127

@@ -284,6 +301,9 @@ def setup(app):
284301
app.add_config_value('plot_basedir', None, True)
285302
app.add_config_value('plot_html_show_formats', True, True)
286303
app.add_config_value('plot_rcparams', {}, True)
304+
app.add_config_value('plot_apply_rcparams', False, True)
305+
app.add_config_value('plot_working_directory', None, True)
306+
app.add_config_value('plot_template', None, True)
287307

288308
app.connect('doctree-read', mark_plot_labels)
289309

@@ -445,7 +465,19 @@ def run_code(code, code_path, ns=None, function_name=None):
445465

446466
pwd = os.getcwd()
447467
old_sys_path = list(sys.path)
448-
if code_path is not None:
468+
if setup.config.plot_working_directory is not None:
469+
try:
470+
os.chdir(setup.config.plot_working_directory)
471+
except OSError as err:
472+
raise OSError(str(err) + '\n`plot_working_directory` option in'
473+
'Sphinx configuration file must be a valid '
474+
'directory path')
475+
except TypeError as err:
476+
raise TypeError(str(err) + '\n`plot_working_directory` option in '
477+
'Sphinx configuration file must be a string or '
478+
'None')
479+
sys.path.insert(0, setup.config.plot_working_directory)
480+
elif code_path is not None:
449481
dirname = os.path.abspath(os.path.dirname(code_path))
450482
os.chdir(dirname)
451483
sys.path.insert(0, dirname)
@@ -564,7 +596,7 @@ def render_figures(code, code_path, output_dir, output_base, context,
564596
ns = {}
565597

566598
for i, code_piece in enumerate(code_pieces):
567-
if not context:
599+
if not context or config.plot_apply_rcparams:
568600
clear_state(config.plot_rcparams)
569601
run_code(code_piece, code_path, ns, function_name)
570602

@@ -588,7 +620,7 @@ def render_figures(code, code_path, output_dir, output_base, context,
588620

589621
results.append((code_piece, images))
590622

591-
if not context:
623+
if not context or config.plot_apply_rcparams:
592624
clear_state(config.plot_rcparams)
593625

594626
return results
@@ -733,7 +765,7 @@ def run(arguments, content, options, state_machine, state, lineno):
733765
src_link = None
734766

735767
result = format_template(
736-
TEMPLATE,
768+
config.plot_template or TEMPLATE,
737769
dest_dir=dest_dir_link,
738770
build_dir=build_dir_link,
739771
source_link=src_link,

0 commit comments

Comments
 (0)