Spine Plot in Plotly
Ladies and Gentlemen, gather around because we’re about to embark on a wild and hilarious ride through the world of Spine Charts using Plotly! No, we’re not talking about the backbone of a dinosaur (although that would be pretty cool), we’re talking about a new and improved way to visualize your hierarchical data in a way that’s not only informative but also chuckle-worthy.
What is Spine Plot?
A spine chart, also known as a rib chart or a spinal plot, is a type of bar chart used to visualize data that has a hierarchical structure. The data categories are represented as a set of nested rectangles, where each rectangle represents a subcategory, and the size of the rectangle reflects the value of the subcategory. The hierarchy is represented by the placement of the rectangles along a common axis, which is often referred to as the “spine.” The spine chart provides a clear representation of hierarchical data and is particularly useful for visualizing complex hierarchies or deep levels of categorization.
Above Visualization
The above chart helps us see how wildlife populations are changing since 1970. It separates the wildlife populations into groups and then shows how many are increasing, decreasing, or staying the same. This information is shown as bars in the graph, with the height of the bar representing the size of the population. for more info
This information is crucial in understanding the state of wildlife populations and the impact of environmental and other factors on their survival. The spinal chart serves as a powerful visual tool for making informed decisions and driving conservation efforts in the right direction.
Data Preparation
The data used in the spinal chart will be extracted from a source.
data = """cat inc stable dec
Mannals 47 10 43
Birds 41 7 52
Reptiles 50 6 44
Amphibians 37 6 57
Fishes 45 4 51"""
df = pd.Series(data.split('\n')[1:]).str.split('\t', expand=True)
df.columns = ['Categocy', 'Increase', 'Stable', 'Decrease']
df.sort_values('Increase', inplace = True)
df.reset_index(drop=True,inplace=True)
The x-axis in the spinal chart is split between positive and negative values, with the zero line passing through the middle of the ‘Stable’ category. The ‘Increasing’ population is represented by negative values, while half of the ‘Stable’ population is represented by negative values and the other half by positive values. On the other hand, the ‘Decreasing’ population is represented by positive values.
a = df['Increase']
increase = -1*a.astype(int)
increase = increase.tolist()
a = df['Stable']
a = a.astype(int)
stable_negative = -1*a/2
stable_negative = stable_negative.tolist()
stable_positive = a/2
stable_positive = stable_positive.tolist()
a = df['Decrease']
decrease = a.astype(int)
decrease = decrease.tolist()
Taxonomic_Group = df['Category'].tolist()
Ladies and Gentlemen, get ready to unleash your inner artist because we’re about to go old-school with Plotly! We’re ditching the easy-breezy Plotly Express and diving deep into the depths of Plotly Graph Object to create our very own spine chart. Buckle up and get ready for a wild ride as we put our creativity to the test and bring this chart to life
As we had break the spine chart down into its three components and we will plot each one with precision. No more mess, no more fuss, just three beautiful bars showing the world the changing population of our wildlife friends. This is going to be a work of art!
fig = go.Figure(data=[
go.Bar(name='Stable',
y=Taxonomic_Group,
x=stable_negative,
orientation='h',
marker=dict(color='#C37C36', line=dict(
color='rgba(0, 0, 0, 1.0)', width=0.5)),
hovertemplate=None,
hoverinfo='none'
),
go.Bar(
y=Taxonomic_Group,
x=stable_positive,
orientation='h',
marker=dict(color='#C37C36', line=dict(
color='rgba(0, 0, 0, 1)', width=0.5)),
hoverinfo='none',
showlegend=False
),
go.Bar(name='Increasing',
y=Taxonomic_Group,
x=increase,
orientation='h',
marker=dict(color='#AE565B', line=dict(
color='rgba(0, 0, 0, 1.0)', width=0.5)),
hoverinfo='none',
),
go.Bar(name='Decreasing',
y=Taxonomic_Group,
x=decrease,
orientation='h',
marker=dict(color='#555F76', line=dict(
color='rgba(0, 0, 0, 1.0)', width=0.5)),
hoverinfo='none',
),
])
Hold on to your hats, we’ve got a curveball coming our way! The ribs are in place, but they’re not quite aligned as we expected them to be. Looks like we’ve got to change our bar mode from ‘group’ to ‘relative’ if we want to fix this little hiccup.
fig.update_layout(barmode='relative')
Ta-da! We’ve done it! Our spine chart is now complete, but let’s be real, it’s not exactly a masterpiece just yet. It’s time to add some extra sparkle, some extra sizzle, to take this chart from drab to fab! Let’s add some pizzazz and make this chart shine!
Adding Pizazz
Alright folks, step one in adding some serious pizzaz to this spine chart is getting rid of all the clutter. We’re talking grid lines, x-axis labels and legends. Time to streamline this chart and give it a nice, clean look. And while we’re at it, let’s add some colour to this dull background, because who says graphs can’t be fun and vibrant?
fig.update_xaxes(
showgrid=False,
showticklabels=False,
fixedrange=True,
)
fig.update_yaxes(
showgrid=False,
fixedrange=True,
)
fig.update_layout(
paper_bgcolor='rgb(248, 248, 255)',
plot_bgcolor='rgb(248, 248, 255)',
margin=dict(l=10, r=10, t=140, b=80),
showlegend=False
)
It’s time to add some flavour to this dish! First up, let’s add labels to explain what each bar represents, and what percentage they make up. After all, a picture is worth a thousand words but with annotations, it’s worth a million!
annotations = []
for yd, inc, dec in zip(Taxonomic_Group, increase, decrease):
annotations.append(dict(xref='x', yref='y',
x=inc / 2, y=yd,
text=str(-1*inc) + '%',
font=dict(family='Arial', size=14,
color='rgb(248, 248, 255)'),
showarrow=False))
annotations.append(dict(xref='x', yref='y',
x=dec / 2, y=yd,
text=str(dec) + '%',
font=dict(family='Arial', size=14,
color='rgb(248, 248, 255)'),
showarrow=False))
annotations.append(dict(text='Increasing',
xref="x", yref="paper", textangle=0,
x=-30, y=1.1, showarrow=False,
font=dict(family='Arial', size=19,
color='#AE565B'),
))
annotations.append(dict(text='Decreasing',
xref="x", yref="paper", textangle=0,
x=30, y=1.1, showarrow=False,
font=dict(family='Arial', size=19,
color='#555F76'),
))
annotations.append(dict(text='Stable',
xref="x", yref="paper", textangle=0,
x=0, y=1.1, showarrow=False,
font=dict(family='Arial', size=19,
color='#C37C36'),
))
annotations.append(dict(text='37% of the studied Amphibians<br>populations had increasing numbers',
xref="x", yref="paper", textangle=0,
x=-39, y=0.02, showarrow=True,arrowhead=2,arrowsize=1,arrowwidth=1.5,
ax=-20,ay=30,align="right",
font=dict(family='Arial', size=13,
color='#AE565B'),
))
annotations.append(dict(text='57% of the studied Amphibians<br>populations had declining numbers',
xref="x", yref="paper", textangle=0,
x=39, y=0.02, showarrow=True,arrowhead=2,arrowsize=1,arrowwidth=1.5,
ax=20,ay=30,align="left",
font=dict(family='Arial', size=13,
color='#555F76'),
))
annotations.append(dict(text="WWF (2022) Living Planet Report 2022 – Bending the curve of biodiversity loss.<br>Almond, R.E.A., Grooten M. and Petersen, T. (Eds). WWF, Gland, Switzerland",
xref="paper", yref="paper", font=dict(family="Rockwell, monospace", size=12, color='gray'),
x=0.5, y=-0.35, showarrow=False));
fig.update_layout(annotations=annotations)
It’s time to add the cherry on top! Let’s give this chart a fancy title to make it stand out and truly shine.
fig.update_layout(title=dict(
text='HOW ARE WILDLIFE POPULATION CHANGING',
font=dict(size=25, family='Rockwell, monospace',
color='rgb(67, 67, 67)'),
x=0.5
))
And finally, it’s time to bring all these ribbons together like a spine! We’ll do that with a sneaky little vertical line, just like a spine holds all the ribs in place.
fig.add_vline(x=0,
fillcolor="black", opacity=1,
layer="above", line_width=2,
line_color='rgba(0, 0, 0, 1.0)',
line_dash="solid")
And that’s a wrap folks! We’ve spruced up this spine chart to make even Pablo Picasso proud. With just the right amount of flair and excitement, our masterpiece is complete and ready to be shown off to the world. Don’t be surprised if it takes home the blue ribbon at the next art show.
I hope you’ve found this guide informative and helpful in creating visually appealing data visualizations with Plotly.
Please feel free to leave a comment below. Any suggestions or questions will help us improve future content. Until next time, happy plotting.