Print

52 of 100: Triangular area chart in matplotlib

At the beginning of the year I challenged myself to create all 100 visualizations using python and matplotlib from the 1 dataset,100 visualizations project and I am sharing with you the code for all the visualizations.

Note: Data Viz Project is copyright Ferdio and available under a Creative Commons Attribution – Non Commercial – No Derivatives 4.0 International license. I asked Ferdio and they told me they used a Design tool to create all the plots.

Collaborate

There are a ton of improvements that can be made on the code, so let me know in the comments any improvements you make and I will update the post accordingly!

To be improved: Heavily hardcoded, need to automate. There is a pure matplotlib implementation, will that too.

This is the original viz that we are trying to recreate in matplotlib:

Import the packages

We will need the following packages:

import mpltern
import matplotlib.pyplot as plt

Generate the data

The data has been hardcoded for now:

xy_ticklabel_color, labels_color, grid_color, datalabels_color ='#757C85',"#101628", "#C8C9C9", "#FFFFFF"

t, l, r = [0,0,1,0,0,1],[0,1,0,0,1,0],[1,0,0,1,0,0]
s=[25000,95000,40000,10000,75000,8000,]
colors =["#9194A3","#90B0F3","#E2AFA5","#2B314D","#5375D4", "#A54836",]

Plot the chart

fig = plt.figure()
ax = fig.add_subplot(projection="ternary",ternary_sum=3.0)

ax.scatter(t, l, r, s=s  , c=colors, )

#set labels
ax.set_tlabel('DK', size=12, color =labels_color, weight = "bold")
ax.set_llabel('SE', size=12, color =labels_color, weight = "bold")
ax.set_rlabel('NO',size=12, color =labels_color, weight = "bold")
ax.laxis.set_label_rotation_mode("horizontal")
ax.raxis.set_label_rotation_mode("horizontal")
ax.tick_params(tick1On=False, tick2On=False,  grid_color='w')
#get rid of tick labels
ax.taxis.set_ticks([])
ax.raxis.set_ticks([])
ax.laxis.set_ticks([])

# Color spines
ax.spines['tside'].set_color(grid_color)
ax.spines['lside'].set_color(grid_color)
ax.spines['rside'].set_color(grid_color)

ax.text(0.5, 1, 0.6, '15', size=12,color = "w", ha='center', va='center')
ax.text(0.2, 1, 0.2, '13', size=12,color = "w", ha='center', va='center')
ax.text(5, 1, 1, '10', size=12,color = "w", ha='center', va='center')
ax.text(14, 1, 1, '4', size=12,color = "w", ha='center', va='center')
ax.text(0.9, 1, 6, '8', size=12,color = "w", ha='center', va='center')
ax.text(0.7, 1, 12, '5', size=12,color = "w", ha='center', va='center')

ax.text(-0.05, 1, 0.9, "'22", size=9,color = "#90B0F3", weight = "light", ha='center', va='center')
ax.text(-0.045, 1, 0.7, "'04", size=9,color = "#5375D4", ha='center', va='center')
ax.text(2, 1, -0.1, "'22", size=9,color = "#E2AFA5", ha='center', va='center')
ax.text(5, 1, -0.2, "'04", size=9,color = "#A54836", ha='center', va='center')
ax.text(1, -0.1, 2.6, "'22", size=9,color = "#9194A3", ha='center', va='center')
ax.text(0.6, -0.1, 2.8, "'04", size=9,color = "#2B314D", ha='center', va='center')

The result:

52 of 100: Triangular area chart in matplotlib
Was this helpful?

Reader Interactions

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents