pythonpy-shiny

Matplotlib figure as SVG with Shiny for Python


The axis labels and titles in the figure in the app below appears unsharp to me. I assume it's because the figure is rendered as PNG, so I assume that rendering it as SVG will fix the issue. However, I'm not sure how to do that. Any pointers?

from shiny import *
import matplotlib.pyplot as plt

app_ui = ui.page_fluid(
    ui.output_plot("dens_plot"),
    ui.input_slider(id = "n", label = "slider", min = 10, max = 50, value = 10)
)

def server(input, output, session):
    @output
    @render.plot
    def dens_plot():
        xs = list(range(input.n()+1))
        ys = [1]*len(xs)
        fig, ax = plt.subplots()
        ax.stem(xs, ys , markerfmt = " ")
        ax.set_xlabel("X title")
        ax.set_ylabel("Y title")
        return fig

app = App(app_ui, server)

Solution

  • The render.plot uses base64 for PNG always, here is the proof. However, I have an idea to use the render.image with SVG created on the level of pure python code (plt.figsave). Please refer to the attached example app where in the web DOM you will find out the base64 based on SVG.

    Please refer to this simple pyshiny app