2

I was able to create a simple dot plot but need a way to connect these dots.
enter image description here

0

2 Answers 2

3

I have slightly edited the @r-beginners code in order to add the color gradient to the line, the annotations and their color. Regarding color gradient, as exposed here:

This feature is not yet available for 2D line plots, it is currently only available for 3D line plots, see https://github.com/plotly/plotly.js/issues/581. It is however possible to use a colorscale in a 2D plot if you use markers instead of lines, see the example below.

Here is my code:

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

Plot:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

@r-beginners you did almost all the job, I only added some details:-)
2

'Plotly' graphs are at a beginner's level, I haven't used them much. Two features I have not been able to achieve are line plot color gradients and 'Women' and 'Men' labels. It's not very good at this level, but please use it as a reference.

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

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.