首页 > 解决方案 > Matplotlib.hist - 使用阶梯直方图平滑点之间的线

问题描述

我正在尝试平滑点之间的线。增加垃圾箱的数量是一种方法,但在我的真实数据上仍然无法解决问题。

def plotstep_test(x, y, z):

    plt.figure(figsize=[10, 6])
    plt.hist([x, y, z], color=['red','black', 'green'], histtype='step', bins=20)

    plt.gca().spines['right'].set_color('none')
    plt.gca().spines['top'].set_color('none')

    plt.title('', fontsize=14)
    plt.xlabel('', fontsize=14, labelpad=5)
    plt.ylabel('', fontsize=14, labelpad=5)


    plt.xticks(np.arange(0, 1.1, step=0.1), fontsize=12)    
    plt.yticks(fontsize=12)
    plt.tick_params(axis='both', which='both', left=True, bottom=True,labelbottom=True) 


    plt.show()

使用随机数:

plotstep_test(np.random.uniform(size=10), np.random.uniform(size=5), np.random.uniform(size=50))

在此处输入图像描述

标签: pythonmatplotlib

解决方案


It sounds like you might prefer a kernel density estimation to a histogram. Then you'll have complete control over the smoothness of the estimate.

Try seaborn.distplot():

data = np.random.normal(size=100)
import seaborn as sns
sns.distplot(data)

This gives you:

distplot example

If you like the look of the line, you can get the rest of your stuff working no problem I'm sure.

In general, seaborn makes really nice statistical plots. Check out the gallery.


推荐阅读