Home > front end >  how to style a figure without use plot in matplotlib
how to style a figure without use plot in matplotlib

Time:08-12

I am creating a matplotlib Figure and save it as a data stream. Somewhere later I convert these bytes into as base64 code and then display them as an <img/> in a html page. I don't use pyplot. I was wondering how I can change the style to "seaborn"?

from matplotlib.figure import Figure
import io

def plot_data(self, x_data, y_data):
        stream = io.BytesIO()
        fig = Figure()
        ax = fig.subplots()
        ax.plot(t_RMS, x_RMS)

        ax.set_xlabel('x data')
        ax.set_ylabel('y data')
        ax.set_title('my data')

        fig.savefig(stream, format='png')
        return stream.getvalue()

I did find matplotlib.pyplot.style.use however have not found an option to style it on the figure directly. Is this possible?

CodePudding user response:

You can import seaborn and then use seaborn.set_theme() and it changes the theme of all your subsequent plots.

Default matplotlib style:

from matplotlib.figure import Figure

x = [1,2,3,4,5]
y = [1,2,3,4,5]

fig = Figure()
ax = fig.subplots()
ax.plot(x,y)
fig.savefig('MATPLOTLIB.png')

Output:

enter image description here

Seaborn style:

import seaborn as sns
sns.set_theme()

x = [1,2,3,4,5]
y = [1,2,3,4,5]

fig = Figure()
ax = fig.subplots()
ax.plot(x,y)
fig.savefig('SEABORN.png')

enter image description here

  • Related