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

Skip to content

Commit d30d57c

Browse files
committed
Added more configuration options.
* plot_apply_rcparams Allows appyling rcParams when context is used. * plot_working_directory Allows running plot directive codes in any directory. * plot_template Allows customization of template used for generating restructured text.
1 parent 967c15a commit d30d57c

1 file changed

Lines changed: 24 additions & 4 deletions

File tree

lib/matplotlib/sphinxext/plot_directive.py

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

@@ -284,6 +299,9 @@ def setup(app):
284299
app.add_config_value('plot_basedir', None, True)
285300
app.add_config_value('plot_html_show_formats', True, True)
286301
app.add_config_value('plot_rcparams', {}, True)
302+
app.add_config_value('plot_apply_rcparams', False, True)
303+
app.add_config_value('plot_working_directory', None, True)
304+
app.add_config_value('plot_template', None, True)
287305

288306
app.connect('doctree-read', mark_plot_labels)
289307

@@ -445,7 +463,9 @@ def run_code(code, code_path, ns=None, function_name=None):
445463

446464
pwd = os.getcwd()
447465
old_sys_path = list(sys.path)
448-
if code_path is not None:
466+
if setup.config.plot_working_directory:
467+
os.chdir(setup.config.plot_working_directory)
468+
elif code_path is not None:
449469
dirname = os.path.abspath(os.path.dirname(code_path))
450470
os.chdir(dirname)
451471
sys.path.insert(0, dirname)
@@ -564,7 +584,7 @@ def render_figures(code, code_path, output_dir, output_base, context,
564584
ns = {}
565585

566586
for i, code_piece in enumerate(code_pieces):
567-
if not context:
587+
if not context or config.plot_apply_rcparams:
568588
clear_state(config.plot_rcparams)
569589
run_code(code_piece, code_path, ns, function_name)
570590

@@ -588,7 +608,7 @@ def render_figures(code, code_path, output_dir, output_base, context,
588608

589609
results.append((code_piece, images))
590610

591-
if not context:
611+
if not context or config.plot_apply_rcparams:
592612
clear_state(config.plot_rcparams)
593613

594614
return results
@@ -733,7 +753,7 @@ def run(arguments, content, options, state_machine, state, lineno):
733753
src_link = None
734754

735755
result = format_template(
736-
TEMPLATE,
756+
config.plot_template or TEMPLATE,
737757
dest_dir=dest_dir_link,
738758
build_dir=build_dir_link,
739759
source_link=src_link,

0 commit comments

Comments
 (0)