簡體   English   中英

如何在不使用 plot 的情況下在 matplotlib 中設置圖形樣式

[英]how to style a figure without use plot in matplotlib

我正在創建一個 matplotlib 圖並將其另存為數據 stream。 稍后我將這些字節轉換為 base64 代碼,然后在 html 頁面中將它們顯示為<img/> 我不使用pyplot。 我想知道如何將樣式更改為“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()

我確實找到matplotlib.pyplot.style.use但是沒有找到直接在圖中設置樣式的選項。 這可能嗎?

您可以導入seaborn然后使用seaborn.set_theme()它會更改所有后續情節的主題。

默認 matplotlib 樣式:

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 樣式:

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

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM