Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit25b5ead

Browse files
committed
BUGFIX: make plot directive naively "responsive"
1 parent2631206 commit25b5ead

File tree

5 files changed

+66
-10
lines changed

5 files changed

+66
-10
lines changed

‎lib/matplotlib/sphinxext/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
frompathlibimportPath
2+
3+
_static_path=Path(__file__).resolve().parent/Path('static')

‎lib/matplotlib/sphinxext/plot_directive.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@
156156
importmatplotlib
157157
frommatplotlib.backend_basesimportFigureManagerBase
158158
importmatplotlib.pyplotasplt
159-
frommatplotlibimport_api,_pylab_helpers,cbook
159+
frommatplotlibimport_api,_pylab_helpers,cbook,sphinxext
160160

161161
matplotlib.use("agg")
162162
align=_api.deprecated(
@@ -254,6 +254,13 @@ def run(self):
254254
raiseself.error(str(e))
255255

256256

257+
def_copy_css_file(app,exc):
258+
ifexcisNoneandapp.builder.format=='html':
259+
src=sphinxext._static_path/Path('plot_directive.css')
260+
dst=app.outdir/Path('_static')
261+
shutil.copy(src,dst)
262+
263+
257264
defsetup(app):
258265
setup.app=app
259266
setup.config=app.config
@@ -269,9 +276,9 @@ def setup(app):
269276
app.add_config_value('plot_apply_rcparams',False,True)
270277
app.add_config_value('plot_working_directory',None,True)
271278
app.add_config_value('plot_template',None,True)
272-
273279
app.connect('doctree-read',mark_plot_labels)
274-
280+
app.add_css_file('plot_directive.css')
281+
app.connect('build-finished',_copy_css_file)
275282
metadata= {'parallel_read_safe':True,'parallel_write_safe':True,
276283
'version':matplotlib.__version__}
277284
returnmetadata
@@ -337,7 +344,6 @@ def split_code_at_show(text):
337344
# Template
338345
# -----------------------------------------------------------------------------
339346

340-
341347
TEMPLATE="""
342348
{{ source_code }}
343349
@@ -374,7 +380,7 @@ def split_code_at_show(text):
374380
)
375381
{%- endif -%}
376382
377-
{{ caption }}
383+
{{ caption }} {# appropriate leading whitespace added beforehand #}
378384
{% endfor %}
379385
380386
.. only:: not html
@@ -383,9 +389,9 @@ def split_code_at_show(text):
383389
.. figure:: {{ build_dir }}/{{ img.basename }}.*
384390
{% for option in options -%}
385391
{{ option }}
386-
{% endfor %}
392+
{% endfor-%}
387393
388-
{{ caption }}
394+
{{ caption }} {# appropriate leading whitespace added beforehand #}
389395
{% endfor %}
390396
391397
"""
@@ -521,7 +527,7 @@ def render_figures(code, code_path, output_dir, output_base, context,
521527
"""
522528
formats=get_plot_formats(config)
523529

524-
#--Try to determine if all images already exist
530+
# Try to determine if all images already exist
525531

526532
code_pieces=split_code_at_show(code)
527533

@@ -624,6 +630,13 @@ def run(arguments, content, options, state_machine, state, lineno):
624630
default_fmt=formats[0][0]
625631

626632
options.setdefault('include-source',config.plot_include_source)
633+
if'class'inoptions:
634+
# classes are parsed into a list of string, and output by simply
635+
# printing the list, abusing the fact that RST guarantees to strip
636+
# non-conforming characters
637+
options['class']= ['plot-directive']+options['class']
638+
else:
639+
options.setdefault('class', ['plot-directive'])
627640
keep_context='context'inoptions
628641
context_opt=Noneifnotkeep_contextelseoptions['context']
629642

@@ -743,8 +756,8 @@ def run(arguments, content, options, state_machine, state, lineno):
743756
errors= [sm]
744757

745758
# Properly indent the caption
746-
caption='\n'.join(' '+line.strip()
747-
forlineincaption.split('\n'))
759+
caption='\n'+'\n'.join(' '+line.strip()
760+
forlineincaption.split('\n'))
748761

749762
# generate output restructuredtext
750763
total_lines= []
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* plot_directive.css
3+
* ~~~~~~~~~~~~
4+
*
5+
* Stylesheet controlling images created using the `plot` directive within
6+
* Sphinx.
7+
*
8+
* :copyright: Copyright 2020-* by the Matplotlib development team.
9+
* :license: Matplotlib, see LICENSE for details.
10+
*
11+
*/
12+
13+
img.plot-directive {
14+
border:0;
15+
max-width:100%;
16+
}

‎lib/matplotlib/tests/test_sphinxext.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,7 @@ def plot_file(num):
5858
assertb'Plot 17 uses the caption option.'inhtml_contents
5959
# check if figure caption made it into html file
6060
assertb'This is the caption for plot 18.'inhtml_contents
61+
# check if the custom classes made it into the html file
62+
assertb'plot-directive my-class my-other-class'inhtml_contents
63+
# check that the multi-image caption is applied twice
64+
asserthtml_contents.count(b'This caption applies to both plots.')==2

‎lib/matplotlib/tests/tinypages/some_plots.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,23 @@ using the :caption: option:
141141

142142
..plot::range4.py
143143
:caption: This is the caption for plot 18.
144+
145+
Plot 19 uses shows that the "plot-directive" class is still appended, even if
146+
we request other custom classes:
147+
148+
..plot::range4.py
149+
:class: my-class my-other-class
150+
151+
Should also have a caption.
152+
153+
Plot 20 shows that the default template correctly prints the multi-image
154+
scenario:
155+
156+
..plot::
157+
:caption: This caption applies to both plots.
158+
159+
plt.figure()
160+
plt.plot(range(6))
161+
162+
plt.figure()
163+
plt.plot(range(4))

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp