简体   繁体   English

Plot 平滑 matplotlib 和 seaborn

[英]Plot smoothing matplotlib and seaborn

I am trying to display my data in a nice way such as seen on the seaborn documentation:我正在尝试以一种很好的方式显示我的数据,例如在 seaborn 文档中看到的:

文本

I am not too sure how to proceed.我不太确定如何进行。 I managed to get the values of points and their respective standard deviation but it looks scattered while I just want to show a tendency:我设法获得了点的值及其各自的标准偏差,但它看起来很分散,而我只想显示一种趋势:

文本

I look into here , and there trying to apply the proposed solution but I couldn't make it work.我在此处查看,并在那里尝试应用建议的解决方案,但我无法使其工作。

Here is what I play with:这是我玩的:

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

Does anyone have an idea?有人有想法吗? I am very beginner in using plots.我是使用情节的初学者。 Would it be possible to smooth?可以平滑吗? and get the nice overlay as in the seaborn image instead of the error bars?并获得像 seaborn 图像中的漂亮覆盖而不是误差线?

These might be silly questions.这些可能是愚蠢的问题。

Kind regards,亲切的问候,

You can use fillbetween for smoothed upper and lower curves.您可以使用fillbetween平滑上下曲线。 Choosing a higher sigma would give more smoothness.选择更高的sigma将提供更高的平滑度。

Here is some example code:这是一些示例代码:

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

示例图

Thank you for your help!谢谢您的帮助! I managed to generate the graph that I wanted!我设法生成了我想要的图表!

First, the spline wouldn't work because my data is not sorted.首先,样条线不起作用,因为我的数据没有排序。 Hence, I used gaussian_filter1d proposed by @JohanC and found here as well.因此,我使用了@JohanC提出的gaussian_filter1d并在此处找到。 However, apparently it can alter the data (read comment on here ) so I decided to plot both of the graph together:然而,显然它可以改变数据(在这里阅读评论)所以我决定将 plot 两个图表放在一起:

最终情节

Using this final version:使用这个最终版本:

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

Thank you very much !非常感谢 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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