<bdo id='GDQsI'></bdo><ul id='GDQsI'></ul>

    <small id='GDQsI'></small><noframes id='GDQsI'>

    <legend id='GDQsI'><style id='GDQsI'><dir id='GDQsI'><q id='GDQsI'></q></dir></style></legend>

    <tfoot id='GDQsI'></tfoot>

    1. <i id='GDQsI'><tr id='GDQsI'><dt id='GDQsI'><q id='GDQsI'><span id='GDQsI'><b id='GDQsI'><form id='GDQsI'><ins id='GDQsI'></ins><ul id='GDQsI'></ul><sub id='GDQsI'></sub></form><legend id='GDQsI'></legend><bdo id='GDQsI'><pre id='GDQsI'><center id='GDQsI'></center></pre></bdo></b><th id='GDQsI'></th></span></q></dt></tr></i><div id='GDQsI'><tfoot id='GDQsI'></tfoot><dl id='GDQsI'><fieldset id='GDQsI'></fieldset></dl></div>
    2. 如何在每个波段/bin中以数据百分比作为标签绘制正态分布?

      How to plot normal distribution with percentage of data as label in each band/bin?(如何在每个波段/bin中以数据百分比作为标签绘制正态分布?)
      • <bdo id='ITXi2'></bdo><ul id='ITXi2'></ul>

            <tbody id='ITXi2'></tbody>

            <i id='ITXi2'><tr id='ITXi2'><dt id='ITXi2'><q id='ITXi2'><span id='ITXi2'><b id='ITXi2'><form id='ITXi2'><ins id='ITXi2'></ins><ul id='ITXi2'></ul><sub id='ITXi2'></sub></form><legend id='ITXi2'></legend><bdo id='ITXi2'><pre id='ITXi2'><center id='ITXi2'></center></pre></bdo></b><th id='ITXi2'></th></span></q></dt></tr></i><div id='ITXi2'><tfoot id='ITXi2'></tfoot><dl id='ITXi2'><fieldset id='ITXi2'></fieldset></dl></div>

            • <legend id='ITXi2'><style id='ITXi2'><dir id='ITXi2'><q id='ITXi2'></q></dir></style></legend>

              <small id='ITXi2'></small><noframes id='ITXi2'>

              <tfoot id='ITXi2'></tfoot>
                本文介绍了如何在每个波段/bin中以数据百分比作为标签绘制正态分布?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                在绘制数据的正态分布图时,我们如何使用 matplotlib/seaborn 或 plotly 在每个条带宽度为 1 个标准差的每个 bin 中放置如下图所示的标签?

                While plotting normal distribution graph of data, how can we put labels like in image below for percentage of data in each bin where each band has a width of 1 standard deviation using matplotlib/seaborn or plotly ?

                目前,我的绘图是这样的:

                Currently, im plotting like this:

                hmean = np.mean(data)
                hstd = np.std(data)
                pdf = stats.norm.pdf(data, hmean, hstd)
                plt.plot(data, pdf)
                

                推荐答案

                虽然我已经标记了四分位数之间的百分比,但这段代码可能有助于对标准差做同样的事情.

                Although I've labelled the percentages between the quartiles, this bit of code may be helpful to do the same for the standard deviations.

                import numpy as np
                import scipy
                import pandas as pd
                from scipy.stats import norm
                import matplotlib.pyplot as plt
                from matplotlib.mlab import normpdf
                
                # dummy data
                mu = 0
                sigma = 1
                n_bins = 50
                s = np.random.normal(mu, sigma, 1000)
                
                fig, axes = plt.subplots(nrows=2, ncols=1, sharex=True)
                
                #histogram
                n, bins, patches = axes[1].hist(s, n_bins, normed=True, alpha=.1, edgecolor='black' )
                pdf = 1/(sigma*np.sqrt(2*np.pi))*np.exp(-(bins-mu)**2/(2*sigma**2))
                
                median, q1, q3 = np.percentile(s, 50), np.percentile(s, 25), np.percentile(s, 75)
                print(q1, median, q3)
                
                #probability density function
                axes[1].plot(bins, pdf, color='orange', alpha=.6)
                
                #to ensure pdf and bins line up to use fill_between.
                bins_1 = bins[(bins >= q1-1.5*(q3-q1)) & (bins <= q1)] # to ensure fill starts from Q1-1.5*IQR
                bins_2 = bins[(bins <= q3+1.5*(q3-q1)) & (bins >= q3)]
                pdf_1 = pdf[:int(len(pdf)/2)]
                pdf_2 = pdf[int(len(pdf)/2):]
                pdf_1 = pdf_1[(pdf_1 >= norm(mu,sigma).pdf(q1-1.5*(q3-q1))) & (pdf_1 <= norm(mu,sigma).pdf(q1))]
                pdf_2 = pdf_2[(pdf_2 >= norm(mu,sigma).pdf(q3+1.5*(q3-q1))) & (pdf_2 <= norm(mu,sigma).pdf(q3))]
                
                #fill from Q1-1.5*IQR to Q1 and Q3 to Q3+1.5*IQR
                axes[1].fill_between(bins_1, pdf_1, 0, alpha=.6, color='orange')
                axes[1].fill_between(bins_2, pdf_2, 0, alpha=.6, color='orange')
                
                print(norm(mu, sigma).cdf(median))
                print(norm(mu, sigma).pdf(median))
                
                #add text to bottom graph.
                axes[1].annotate("{:.1f}%".format(100*norm(mu, sigma).cdf(q1)), xy=((q1-1.5*(q3-q1)+q1)/2, 0), ha='center')
                axes[1].annotate("{:.1f}%".format(100*(norm(mu, sigma).cdf(q3)-norm(mu, sigma).cdf(q1))), xy=(median, 0), ha='center')
                axes[1].annotate("{:.1f}%".format(100*(norm(mu, sigma).cdf(q3+1.5*(q3-q1)-q3)-norm(mu, sigma).cdf(q3))), xy=((q3+1.5*(q3-q1)+q3)/2, 0), ha='center')
                axes[1].annotate('q1', xy=(q1, norm(mu, sigma).pdf(q1)), ha='center')
                axes[1].annotate('q3', xy=(q3, norm(mu, sigma).pdf(q3)), ha='center')
                
                axes[1].set_ylabel('probability')
                
                #top boxplot
                axes[0].boxplot(s, 0, 'gD', vert=False)
                axes[0].axvline(median, color='orange', alpha=.6, linewidth=.5)
                axes[0].axis('off')
                
                plt.subplots_adjust(hspace=0)
                plt.show()
                

                这篇关于如何在每个波段/bin中以数据百分比作为标签绘制正态分布?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
                Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
                Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
                Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
                Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)
                Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)

                    <i id='jPyQx'><tr id='jPyQx'><dt id='jPyQx'><q id='jPyQx'><span id='jPyQx'><b id='jPyQx'><form id='jPyQx'><ins id='jPyQx'></ins><ul id='jPyQx'></ul><sub id='jPyQx'></sub></form><legend id='jPyQx'></legend><bdo id='jPyQx'><pre id='jPyQx'><center id='jPyQx'></center></pre></bdo></b><th id='jPyQx'></th></span></q></dt></tr></i><div id='jPyQx'><tfoot id='jPyQx'></tfoot><dl id='jPyQx'><fieldset id='jPyQx'></fieldset></dl></div>

                    <small id='jPyQx'></small><noframes id='jPyQx'>

                          <bdo id='jPyQx'></bdo><ul id='jPyQx'></ul>

                            <tbody id='jPyQx'></tbody>
                          <tfoot id='jPyQx'></tfoot>

                        • <legend id='jPyQx'><style id='jPyQx'><dir id='jPyQx'><q id='jPyQx'></q></dir></style></legend>