Better Plotly Bar Chart

I’m reading “storytelling with data” by Cole Nussbaumer Knaflic. I plan to write my thoughts and insights from the book once I finish it. For now, I wanted to play with it a bit and create a better bar chart visualization that –

  1. Highlight the category you find most important and assign a special color to it (i.e prominent_color in the code), while the remaining categories used the same color (i.e. latent_color)
  2. Remove grids and make the background and paper colors the same to remove cognitive load.

The implementation is flexible, so if you feel like changing one of the settings(i.e., show the grid lines or center the title) you can pass it via keyword arguments when calling the function.


from typing import Any
import pandas as pd
import plotly.graph_objects as go
import pandas as pd
def barchart(
df: pd.DataFrame, x_col: str, y_col: str,
title: str | None = None,
latent_color : str = 'gray',
prominent_color: str = 'orange',
prominent_value: Any | None = None,
**kwargs: dict,
) -> go.Figure:
"""_summary_
Args:
df (pd.DataFrame): Dataframe to plot
x_col (str): Name of x coloumn
y_col (str): Name of y coloumn
title (str | None, optional): Chart title. Defaults to None.
latent_color (str, optional): Color to use for the values we don't want to highlight. Defaults to 'gray'.
prominent_color (str, optional): Color to use for the value we want to highlight. Defaults to 'orange'.
prominent_value (Any | None, optional): Value of the category we want to highlight. Defaults to None.
Returns:
go.Figure: Plotly figure object
"""
colors = (df[x_col] == prominent_value).replace(False, latent_color).replace(True, prominent_color).to_list()
fig = go.Figure(data=[
go.Bar(
x=df[x_col],
y=df[y_col],
marker_color=colors
)],
layout=go.Layout(
title=title,
xaxis=dict(title=x_col, showgrid=False),
yaxis=dict(title=y_col, showgrid=False),
plot_bgcolor='white',
paper_bgcolor='white'
)
)
fig.update_layout(**kwargs)
return fig
if __name__ == "__main__":
data = {'categories': ['A', 'B', 'C', 'D', 'E'],
'values': [23, 45, 56, 78, 90]}
df = pd.DataFrame(data)
fig = barchart(df, 'categories', 'values', prominent_value='C', title='My Chart', yaxis_showgrid=True)
fig.show()

📚 Book club Q2 2024 – 3 Reading recommendations

This quarter, I read 3 books that relate to France and the resistance during the 2nd world War –

  • Code Name Hélène by Ariel Lawhon. This thriller is based on Nancy Wake’s story. It also reminded me of “Agent Sonya” by Ben Macintyre, which I read a while ago and enjoyed.
  • The Paris Architect by Charles Belfoure – is a touching story with lots of imagination but I was a bit disappointed it was not based on a real story.
  • The Dressmaker’s Secret by Rosalie Ham – this book tells the story of Coco Chanel during WWII from the prism of her personal assistant or more accurately from the granddaughter of her personal assistant who unpacks the story. I didn’t know about her relationships with the Nazis, and it was interesting to learn about them.

I also listened to “Setting the Table” by Danny Meyer, and it was very insightful, it even made me publish a LinkedIn post.

📚 Book club Q1 2024 – 3 Reading recommendations

This year, I track my book reading for the first time. I don’t know if I’ll keep doing it after this year or if I’ll last until the end of the year, but for now, I’m all in. This helps me reflect on my reading and remember the books I enjoyed.

Fundable: Why Some Entrepreneurs Get Funded, And Others Do Not! by Sephi Shapira

This is the best value for my time in a long time. The book is filled with concrete and practical advice, which I immediately found myself using.

Beartown by Fredrik Backman –

That’s the perfect book for me—it has lots of sports and a human story, some gender tension, and two more books in this series (Us Against You and Winners). 

STFU: The Power of Keeping Your Mouth Shut in an Endlessly Noisy World by Dan Lyons

For long parts of this book, it was a paradox to me – why write 200+ pages to say we should all shut up – just do it.

I listened to this book after also listening to Disrupted by Lyons. I like his style and narration. When listening to Disrupted, I thought that it probably damaged his employability, which turned out to be true, as he discussed in STFU.

The book can sometimes be extreme or refer to an extreme crowd (50 out of 50 in the Talkaholics test). However, I liked the book because it discusses many aspects of our lives – friends and family, work, etc.- and a few things I can immediately adapt. For example, I lowered my cell phone usage near my kids and hope to stick with it.

In one of the last chapters, he mentions Never Split the Difference by Chris Voss and Tahl Raz, which I also recently listened to. I listened to Never Split the Difference after a friend told me that she was starting to reread this book, and after listening to it, I completely understood why she wanted to reiterate it.

I read the book to improve my negotiation skills. I’m not sure there is an immediate effect, but as the two books pointed out – I talk less and actively listen more. So I try to pause before I answer, be succinct and hum, and let the other person talk.