簡體   English   中英

Plot 平滑 matplotlib 和 seaborn

[英]Plot smoothing matplotlib and seaborn

我正在嘗試以一種很好的方式顯示我的數據,例如在 seaborn 文檔中看到的:

文本

我不太確定如何進行。 我設法獲得了點的值及其各自的標准偏差,但它看起來很分散,而我只想顯示一種趨勢:

文本

我在此處查看,並在那里嘗試應用建議的解決方案,但我無法使其工作。

這是我玩的:

Final_array =         Mean       Std
0   0.739269  0.157892
1   0.807382  0.160464
2   0.800024  0.137239
3   0.825854  0.132472
4   0.864854  0.070544
..       ...       ...
95  0.797202  0.101961
96  0.747578  0.143394
97  0.751472  0.158651
98  0.587009  0.198987
99  0.728447  0.104601

sns.set(style="darkgrid", palette="muted", color_codes=True)
fig, ax = plt.subplots(figsize=(7,5))
y_pos = np.arange(Final_array.shape[0])
ax.errorbar(y_pos, Final_array[:,0], yerr=Final_array[:,1], elinewidth=0.5)
plt.show()

有人有想法嗎? 我是使用情節的初學者。 可以平滑嗎? 並獲得像 seaborn 圖像中的漂亮覆蓋而不是誤差線?

這些可能是愚蠢的問題。

親切的問候,

您可以使用fillbetween平滑上下曲線。 選擇更高的sigma將提供更高的平滑度。

這是一些示例代碼:

import matplotlib.pyplot as plt
import numpy as np
from scipy.ndimage.filters import gaussian_filter1d

x = np.linspace(0, 100, 100)
y = 0.95 - ((50 - x) / 200) ** 2
err = (1 - y) / 2
y += np.random.normal(0, err / 10, y.size)

upper = gaussian_filter1d(y + err, sigma=3)
lower = gaussian_filter1d(y - err, sigma=3)

fig, ax = plt.subplots(ncols=2)

ax[0].errorbar(x, y, err, color='dodgerblue')

ax[1].plot(x, y, color='dodgerblue')
ax[1].fill_between(x, upper, lower, color='crimson', alpha=0.2)

plt.show()

示例圖

謝謝您的幫助! 我設法生成了我想要的圖表!

首先,樣條線不起作用,因為我的數據沒有排序。 因此,我使用了@JohanC提出的gaussian_filter1d並在此處找到。 然而,顯然它可以改變數據(在這里閱讀評論)所以我決定將 plot 兩個圖表放在一起:

最終情節

使用這個最終版本:

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.ndimage.filters import gaussian_filter1d

Final_array =         Mean       Std
0   0.739269  0.157892
1   0.807382  0.160464
2   0.800024  0.137239
3   0.825854  0.132472
4   0.864854  0.070544
..       ...       ...
95  0.797202  0.101961
96  0.747578  0.143394
97  0.751472  0.158651
98  0.587009  0.198987
99  0.728447  0.104601

sns.set(style="darkgrid", palette="muted", color_codes=True)
fig, ax = plt.subplots(figsize=(7,5))
y_pos = np.arange(Final_array.shape[0])

# Smoothing
Final_array_smooth = gaussian_filter1d(Final_array[:,0], sigma=2)

# Error formating
upper_err = gaussian_filter1d(Final_array[:,0] + (Final_array[:,1]/2), sigma=5)
lower_err = gaussian_filter1d(Final_array[:,0] - (Final_array[:,1]/2), sigma=5)

ax.plot(y_pos, Final_array[:,0], '--', linewidth=0.7, color='k', alpha=0.45)
ax.plot(y_pos, Final_array_smooth)
ax.fill_between(y_pos, upper_err, lower_err, color='crimson', alpha=0.2)

ax.set_ylim(np.min(Final_array[:,0])-(np.min((Final_array[:,0])*20)/100), np.max(Final_array[:,0])+(np.max((Final_array[:,0])*10)/100))
plt.show()

非常感謝 !

暫無
暫無

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

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