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

5 interesting things (15/01/2024)

SQL as API – I saw several efforts to expose RDBMS as API over the years. This post suggests another engel – exposing an API that accepts SQL. Consider this a brain teaser.

https://valentin.willscher.de/posts/sql-api/

SomeEstimates –  For me, the loss of trust described in the post is the most harassing implication of a culture where estimates are often missed –

“Another negative outcome is a loss of trust between developers and management since a constant sense of urgency is tantamount to no sense of urgency at all.”

https://www.shaiyallin.com/post/someestimates

How to Make Anthropic’s Claude Models Consistently Generate Valid JSON – Gettign valid and consistent JSON from LLM is an issue. Prompt engineering, as described in this post, can solve some of those issues; the json_repair package mentioned there can solve additional problems. With the GPT store announced this week and the evolving models, I believe this will be solved soon in one way or another.

https://levelup.gitconnected.com/how-to-make-anthropics-claude-models-consistently-generate-valid-json-d74ce037ca46

Bonus – https://github.com/mangiucugna/json_repair

My PostgreSQL wishlist – Another brain teaser. The items I most relate to are having created_at and updated_at columns created and maintained automatically and being immutable. I’m curious to follow the comments on this post.

https://ryanguill.com/postgresql/sql/2024/01/08/postgresql-wishlist.html

Everyday storytelling for engineers. The CAO Method – Although storytelling has become an overused buzzword in the last few years (I thought it was already over the hill). This post is important not due to the specific method but to the recognition that ICs practice storytelling every day, and mastering this skill can affect your promotion, career path, tasks you get, etc. 

https://tonyfreed.substack.com/p/everyday-storytelling-for-engineers