简体   繁体   中英

how to style a figure without use plot in matplotlib

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?

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:

在此处输入图像描述

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')

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM