简体   繁体   English

Plotly:如何将均值和标准差添加到 create_displot 图?

[英]Plotly: How to add mean and standard deviation to a create_displot figure?

I am trying to make changes to the histogram and the normal distribution curve here that is made using the create_distplot fuction in plotly.我正在尝试对直方图和此处使用 create_distplot 函数绘制的正态分布曲线进行更改。 I could not find anything which allows me to add more traces to this plot.我找不到任何可以让我在这个图中添加更多痕迹的东西。 Is there any other way to achieve the same results using any other plotly function?有没有其他方法可以使用任何其他 plotly 函数实现相同的结果?

GAIA = pd.read_csv(r'C:\Users\Admin\Desktop\New folder\6 SEM Python\WORK\Astrometry\DistancePM.csv')
    df = pd.DataFrame(GAIA, columns = ['ra','dec','rest','b_rest','B_rest','pmra','pmra_error','pmdec','pmdec_error','PM'])
    ra = df['ra'].tolist()
    dec = df['dec'].tolist()
    rest = df['rest'].tolist()
    b_rest = df['b_rest'].tolist()
    B_rest = df['B_rest'].tolist()
    pmra = df['pmra'].tolist()
    pmra_E = df['pmra_error'].tolist()
    pmdec = df['pmdec'].tolist()
    pmdec_E = df['pmdec_error'].tolist()
    PM = df['PM'].tolist() 
    PM1 = []
    c = 0
    #Here we are onlly taking the range from tehabove contour plot where there is a 
    #clustering of data points x = [-4.5, 1.5] and y = [3, 1]
    for i in range(len(PM)):
        if (PM[i]<100 and pmra[i]>-4.5 and pmra[i]<1.5 and pmdec[i]>1 and pmdec[i]<3):
            PM1.append(PM[i])
            c+=1
    group_labels = ['Proper Motion']
    color = ['#636EFA', '#EF553B', '#00CC96', '#AB63FA', '#FFA15A', '#19D3F3', '#FF6692', '#B6E880', '#FF97FF', '#FECB52']    
    fig = ff.create_distplot(
        [PM1], 
        group_labels,
        bin_size = 0.05,
        curve_type='normal',
        colors = color
    )
    fig.update_layout(
        title = 'Proper Motion Histogram + Gaussian distribution ',
        xaxis = dict(
            title='Proper Motion'
        ),
        yaxis = dict(
            title='Density'
        ),
        template = 'plotly_dark',
        showlegend = True
    )
    fig.show()
    print(c)[![enter image description here][1]][1]
    
    
      [1]: https://i.stack.imgur.com/lOBvY.png

这就是图的样子

You haven't specified how you'd like to display your added data, so I can only assume that this is what you're looking for:您尚未指定如何显示添加的数据,因此我只能假设这就是您要查找的内容:

在此处输入图片说明

If that's the case you can calculate your measures using np.mean() and add that to the figure using:如果是这种情况,您可以使用np.mean()计算您的度量,并使用np.mean()将其添加到图中:

fig.add_shape(type="line",x0=mean, x1=mean, y0 =0, y1=0.4 , xref='x', yref='y',
               line = dict(color = 'blue', dash = 'dash'))

Here's a complete code snippet with mean +/- one standard deviation as well:这是一个完整的代码片段,平均值也是 +/- 一个标准偏差:

import plotly.figure_factory as ff
import numpy as np
np.random.seed(1)

x = np.random.randn(1000)
hist_data = [x]
group_labels = ['distplot'] # name of the dataset

mean = np.mean(x)
stdev_pluss = np.std(x)
stdev_minus = np.std(x)*-1

fig = ff.create_distplot(hist_data, group_labels)
fig.add_shape(type="line",x0=mean, x1=mean, y0 =0, y1=0.4 , xref='x', yref='y',
               line = dict(color = 'blue', dash = 'dash'))
fig.add_shape(type="line",x0=stdev_pluss, x1=stdev_pluss, y0 =0, y1=0.4 , xref='x', yref='y',
               line = dict(color = 'red', dash = 'dash'))
fig.add_shape(type="line",x0=stdev_minus, x1=stdev_minus, y0 =0, y1=0.4 , xref='x', yref='y',
               line = dict(color = 'red', dash = 'dash'))
fig.show()

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

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