【问题标题】:Is there a way to create range plot in python using plotly?有没有办法使用 plotly 在 python 中创建范围图?
【发布时间】:2020-08-03 04:08:18
【问题描述】:

我能够创建一个简单的点图,但需要一种方法来连接这些点。

【问题讨论】:

    标签: python matplotlib plotly-python


    【解决方案1】:

    我稍微编辑了@r-beginners code,以便将颜色渐变添加到线条、注释及其颜色。关于颜色渐变,曝光here:

    此功能尚不适用于 2D 线图,目前 仅适用于 3D 线图,请参阅 https://github.com/plotly/plotly.js/issues/581。然而这是可能的 如果您使用标记而不是线条,则在 2D 绘图中使用色标, 请参阅下面的示例。

    这是我的代码:

    import pandas as pd
    import numpy as np
    import plotly.graph_objects as go
    
    df = pd.DataFrame({'Grade': ["Doctorate Degree",
                                 "Master's Degree",
                                 "Bachelor's Degree",
                                 "Associate Degree",
                                 "Some college, no degree",
                                 "Hight School",
                                 "9th 12th(no degree)",
                                 "Less Than 9th Grade"],
                       'Women': [91, 69, 54, 38, 35, 29, 22, 21],
                       'Men': [151, 99, 84, 56, 53, 47, 37, 34]})
    
    w_lbl = list(map(lambda x: str(x), df['Women']))
    m_lbl = list(map(lambda x: str(x), df['Men']))
    
    fig = go.Figure()
    
    for i in range(0, len(df)):
    
        fig.add_trace(go.Scatter(x = np.linspace(df['Women'][i], df['Men'][i], 1000),
                                 y = 1000*[df['Grade'][i]],
                                 mode = 'markers',
                                 marker = {'color': np.linspace(df['Women'][i], df['Men'][i], 1000),
                                           'colorscale': ['#E1A980', '#8DAEA6'],
                                           'size': 8}))
    
    fig.add_trace(go.Scatter(x = df['Women'],
                             y = df['Grade'],
                             marker = dict(color = '#CC5600', size = 14),
                             mode = 'markers+text',
                             text = w_lbl,
                             textposition = 'middle left',
                             textfont = {'color': '#CC5600'},
                             name = 'Woman'))
    
    fig.add_trace(go.Scatter(x = df['Men'],
                             y = df['Grade'],
                             marker = dict(color = '#237266', size = 14),
                             mode = 'markers+text',
                             text = m_lbl,
                             textposition = 'middle right',
                             textfont = {'color': '#237266'},
                             name = 'Men'))
    
    fig.add_annotation(x = df.iloc[-1, df.columns.get_loc('Women')] - 5,
                       y = df.iloc[-1, df.columns.get_loc('Grade')],
                       text = 'Women',
                       font = {'color': '#CC5600',
                               'size': 15},
                       showarrow = False)
    
    fig.add_annotation(x = df.iloc[-1, df.columns.get_loc('Men')] + 5,
                       y = df.iloc[-1, df.columns.get_loc('Grade')],
                       text = 'Men',
                       font = {'color': '#237266',
                               'size': 15},
                       showarrow = False)
    
    fig.update_layout(title = "Sample plotly",
                      showlegend = False)
    
    fig.show()
    

    剧情:

    【讨论】:

    • @r-beginners 你几乎完成了所有工作,我只添加了一些细节:-)
    【解决方案2】:

    'Plotly' 图表处于初学者的水平,我用的不多。我无法实现的两个功能是线图颜色渐变以及“女性”和“男性”标签。这个级别不是很好,但请作为参考。

    import pandas as pd
    import numpy as np
    import io
    
    data = '''
     Grade Women Men
    0 "Less Than 9th Grade" 21 34
    1 "9th 12th(no degree)" 22 37
    2 "Hight School" 29 47
    3 "Some college, no degree" 35 53
    4 "Associate Degree" 38 56
    5 "Bachelor's Degree" 54 84
    6 "Master's Degree" 69 99
    7 "Doctorate Degree" 91 151
    '''
    
    df = pd.read_csv(io.StringIO(data), sep='\s+')
    df.sort_values('Men', ascending=False, inplace=True, ignore_index=True)
    
    import plotly.graph_objects as go
    import plotly.figure_factory as ff
    
    w_lbl = [str(s) for s in df['Women'].tolist()]
    m_lbl = [str(s) for s in df['Men'].tolist()]
    
    fig = go.Figure()
    
    for i in range(0,8):
        fig.add_trace(go.Scatter(
            x=[df['Women'][i],df['Men'][i]],
            y=[df['Grade'][i],df['Grade'][i]],
            orientation='h',
            line=dict(color='rgb(244,165,130)', width=8),
                 ))
    
    fig.add_trace(go.Scatter(
        x=df['Women'],
        y=df['Grade'],
        marker=dict(color='#CC5700', size=14),
        mode='markers+text',
        text=w_lbl,
        textposition='middle left',
        name='Woman'))
    
    fig.add_trace(go.Scatter(
        x=df['Men'],
        y=df['Grade'],
        marker=dict(color='#227266', size=14),
        mode='markers+text',
        text=m_lbl,
        textposition='middle right',
        name='Men'))
    
    fig.update_layout(title="Sample plotly", showlegend=False)
    
    
    fig.show()
    

    【讨论】:

      猜你喜欢
      • 2018-10-17
      • 2022-11-10
      • 2020-04-05
      • 1970-01-01
      • 2011-05-10
      • 2021-12-06
      • 1970-01-01
      • 1970-01-01
      • 2021-11-01
      相关资源
      最近更新 更多