We start by extracting the data from a previous project. The country column is converted to a categorical type, which simplifies handling in plotnine. To enable alternating row colors in the final visualization, we also create a new column called color_mod. Additional columns are created to define the positions for the segments and text labels.
Next, we create another DataFrame that contains information for all the text elements, such as text color, font size, and font weight, for the title and headers.
The data= argument is set to textdata_df, which contains all the custom label and styling details for the title and column headers.
What’s happening here?
geom_segment(): Since I couldn’t find a way to apply border radius, I used geom_segment() with lineend="round" as the best available workaround. Thick lines serve as cell backgrounds.
scale_color_identity(): Uses the color values directly from the color_mod column, without applying a scale.
scale_y_discrete(): Reorders the country axis and tweaks padding to add space above and below the table.
scale_size_identity(): Similar to color scaling, this instructs plotnine to use the font sizes specified in the fontsize column without transformation.
watermark(): Embeds a logo. Since there’s no native figure size parameter in plotnine, I manually scaled the output.
Custom Theme
We apply a tailored theme with themify() to refine the figure’s appearance:
This post showcases how plotnine can be used to create table-like visualizations. I’m genuinely impressed by its capabilities — it’s surprisingly fun to approach a table as a figure.
Throughout this exploration, I learned a lot from this repository, which won the Plotnine Contest 2024. It’s a fantastic example of what’s possible with the library.
It would be exciting to explore how plotnine and Great Tables might work together to enable even richer visual storytelling — I’m looking forward to diving into that next.
Disclaimer
This table is intended as a self-practice project, and the data in the table may not be 100% accurate. Please refer to the original source if you require verified data.
This post was drafted by me, with AI assistance to refine the content.