1 | initial version |
Certain particular Sage objects may provide a method to obtain an svg representation or a tikz representation.
For plots such as the one in the question though, Sage delegates the plotting to matplotlib and no direct method to get the svg as a string is provided.
The obvious workaround is to save to a file, and then read the file into a string.
To illustrate, define a function and its plot:
sage: f = lambda x, y: sin(x + y) + cos(x + y)
sage: p = plot_slope_field(f, (-3, 3), (-3, 3))
Save the plot into a file (see note at the end):
sage: filename = 'slope_field.svg'
sage: p.save(filename)
Read the saved file:
sage: with open(filename, 'r') as f:
....: s = f.read()
Check the result by printing the initial and final fragment:
sage: print(s[:200], "...", s[-200:], sep='\n')
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!-- Created with matplotlib (https://matplo
...
uare;stroke-linejoin:miter;stroke-width:0.8;"/>
</g>
</g>
</g>
<defs>
<clipPath id="p5eaf43e8f8">
<rect height="306.92" width="422.3" x="24.170313" y="7.2"/>
</clipPath>
</defs>
</svg>
Note: to create the file as a temporary file that will get cleaned up automatically when Sage exits:
sage: filename = tmp_filename(ext='.svg')
It is of course also possible to delete the file oneself, without waiting for Sage to exit.
2 | No.2 Revision |
Certain particular classes of Sage objects may provide a method to obtain
an svg representation methods
to return svg or a tikz representation.representations as strings.
For plots such as the one in the question though, Sage delegates the plotting
to matplotlib and no and saving are delegated to matplotlib, who produces the
svg string. No direct method to get the svg as a string is provided.provides the svg string.
The obvious workaround is to save to a file, and then read the file into a string.
To illustrate, define a function and its plot:plot the corresponding slope field:
sage: f = lambda x, y: sin(x + y) + cos(x + y)
sage: p = plot_slope_field(f, (-3, 3), (-3, 3))
Save the plot into a file (see note at the end):end of this answer):
sage: filename = 'slope_field.svg'
sage: p.save(filename)
Read the saved file:
sage: with open(filename, 'r') as f:
....: s = f.read()
Check the result by printing the an initial and a final fragment:
sage: print(s[:200], print(s[:155], "...", s[-200:], s[-30:], sep='\n')
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!-- Created with matplotlib (https://matplo
...
uare;stroke-linejoin:miter;stroke-width:0.8;"/>
</g>
</g>
</g>
<defs>
<clipPath id="p5eaf43e8f8">
<rect height="306.92" width="422.3" x="24.170313" y="7.2"/>
</clipPath>
</defs>
</svg>
Note: to create the file as a temporary file that will get cleaned up automatically when Sage exits:
sage: filename = tmp_filename(ext='.svg')
It is of course also possible to delete the file oneself, without waiting for Sage to exit.
sage: os.remove(filename)