2

I wonder if there is a neat way to implement a dumbbell plot in python with Plotly. I found that it is possible to construct it line by line by adding shapes (either manually like this, or calculating the values from data). For example:

data = pd.DataFrame(
    {'Age': {
        1: '16-24',
        4: '16-24',
        10: '25-34',
        13: '25-34'
    },
     'Gender': {
        1: 'Men',
        4: 'Women',
        10: 'Men',
        13: 'Women'
    },
     'Medium+High': {
        1: 9.100000000000001,
        4: 26.0,
        10: 15.2,
        13: 19.1
    }
})

fig = px.scatter(data, y="Age", x="Medium+High", color="Gender")
fig.update_layout(shapes=[
    dict(
      type= 'line',
      yref= 'paper', y0= .94, y1= .94,
      xref= 'x', x0= 10, x1= 5.3
     )
])

That would produce the following plot (that would eventually become a dumbbell) first-step-towards-a-dumbell-plot

In R there is a very neat way to add the horizontal lines from the data (similar to how you would add traces). Is there an equivalent way to do this in python?

rpanai
  • 12,515
  • 2
  • 42
  • 64
  • thank @rpanai. Sort of. I mean it is a sufficient solution (iterate over the df to get the the values for the shapes list). I still find the R way much more elegant. Oh well. – Polina Sklyarevsky Jul 09 '20 at 12:45
  • I second that the R solution is much nicer. It will be great to implement it in python. – rpanai Jul 09 '20 at 13:44

0 Answers0