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

Skip to content

Commit 8fa8b81

Browse files
authored
Merge pull request #22008 from oscargus/math_to_image_color_support
Added color keyword argument to math_to_image
2 parents d8594bf + 5bd8024 commit 8fa8b81

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
``math_to_image`` now has a *color* keyword argument
2+
--------------------------------------------------------
3+
4+
To easily support external libraries that rely on the MathText rendering of
5+
Matplotlib to generate equation images, a *color* keyword argument was added
6+
to `~matplotlib.mathtext.math_to_image`.
7+
8+
.. code-block:: python
9+
10+
from matplotlib import mathtext
11+
mathtext.math_to_image('$x^2$', 'filename.png', color='Maroon')

lib/matplotlib/mathtext.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,8 @@ def get_depth(self, texstr, dpi=120, fontsize=14):
563563
return depth
564564

565565

566-
def math_to_image(s, filename_or_obj, prop=None, dpi=None, format=None):
566+
def math_to_image(s, filename_or_obj, prop=None, dpi=None, format=None,
567+
*, color=None):
567568
"""
568569
Given a math expression, renders it in a closely-clipped bounding
569570
box to an image file.
@@ -582,14 +583,16 @@ def math_to_image(s, filename_or_obj, prop=None, dpi=None, format=None):
582583
format : str, optional
583584
The output format, e.g., 'svg', 'pdf', 'ps' or 'png'. If not set, the
584585
format is determined as for `.Figure.savefig`.
586+
color : str, optional
587+
Foreground color, defaults to :rc:`text.color`.
585588
"""
586589
from matplotlib import figure
587590

588591
parser = MathTextParser('path')
589592
width, height, depth, _, _ = parser.parse(s, dpi=72, prop=prop)
590593

591594
fig = figure.Figure(figsize=(width / 72.0, height / 72.0))
592-
fig.text(0, depth/height, s, fontproperties=prop)
595+
fig.text(0, depth/height, s, fontproperties=prop, color=color)
593596
fig.savefig(filename_or_obj, dpi=dpi, format=format)
594597

595598
return depth

lib/matplotlib/tests/test_mathtext.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ def test_mathtext_fallback(fallback, fontlist):
373373
def test_math_to_image(tmpdir):
374374
mathtext.math_to_image('$x^2$', str(tmpdir.join('example.png')))
375375
mathtext.math_to_image('$x^2$', io.BytesIO())
376+
mathtext.math_to_image('$x^2$', io.BytesIO(), color='Maroon')
376377

377378

378379
def test_mathtext_to_png(tmpdir):

0 commit comments

Comments
 (0)