32 lines
655 B
Python
32 lines
655 B
Python
import matplotlib.pyplot as plt
|
|
from matplotlib.backends.backend_svg import FigureCanvasSVG
|
|
import io
|
|
|
|
|
|
def create_svg_plot():
|
|
# Create a sample plot
|
|
x = [1, 2, 3, 4, 5]
|
|
y = [2, 4, 6, 8, 10]
|
|
|
|
fig, ax = plt.subplots()
|
|
ax.plot(x, y)
|
|
ax.set_title('Sample Plot')
|
|
ax.set_xlabel('X-axis')
|
|
ax.set_ylabel('Y-axis')
|
|
|
|
# Convert the plot to SVG
|
|
svg_output = io.StringIO()
|
|
canvas = FigureCanvasSVG(fig)
|
|
canvas.print_svg(svg_output)
|
|
|
|
# Close the plot to free up resources
|
|
plt.close(fig)
|
|
|
|
# Return the SVG string
|
|
return svg_output.getvalue()
|
|
|
|
# Example usage
|
|
svg_plot = create_svg_plot()
|
|
print(svg_plot)
|
|
|