86 of 100: Venn diagram 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: This is heavily hardcoded, need to review. The shading is missing as well as the rounding of the boxes.
This is the original viz that we are trying to recreate in matplotlib:

Import the packages
We will need the following packages:
import matplotlib.pyplot as plt
import numpy as np
Generate the data
I hardcoded everything, so we will go straight to the plot.
Plot the chart
import matplotlib.pyplot as plt
import numpy as np
colors = [ '#D6DADE', '#D6DADE', '#D6DADE', "#A54836","#5375D4","#2B314D", ]
height = [30, 10, 10, 15, 15, 15]
width = [70, 52, 52, 17, 25, 16]
#the lower left corner of the square
all = [1, 1]
after = [10, 4]
before = [12,17]
dk = [6, 7]
sw = [25, 9]
no = [52, 8]
squares = [all,after, before,dk,sw,no]
fig, ax = plt.subplots(figsize=(6,6), facecolor = "#FFFFFF" )
for square, h, w, col in zip(squares, height, width, colors):
x = square[0]
y = square[1]
ax.plot([x, x + w, x + w, x, x],
[y, y, y + h, y + h, y],
color=col, linestyle='solid')
xtext = [5, 14, 13, 8, 27, 54]
ytext = [30.5, 26.5, 3.5, 21.5, 23.5, 22.5]
text = ["All", "Before 2004", "After 2004", "Denmark", "Sweden", "Norway"]
for xt, yt, t, c in zip(xtext, ytext, text, colors):
ax.annotate( t, xy= (xt, yt), color = c, size = 12,
bbox=dict(facecolor='w', edgecolor='w', boxstyle='round,pad=0.3'))
# Generate 6 plots in the precise location
x = [0.28, 0.48, 0.68 ]*3
y = [0.53, 0.56, 0.53, 0.33, 0.36,0.33]
sites = [4,13,5,6,2,3]
colors = ["#A54836","#5375D4","#2B314D",]*3
for x,y,c, site in zip(x,y, colors, sites):
ax_scatter= fig.add_axes([x, y, 0.09, 0.07])
data = np.random.randn(site)
width = 0.8 # the maximum width of each 'row' in the scatter plot
xpos = 0 # the centre position of the scatter plot in x
counts, edges = np.histogram(data, bins=4)
centres = (edges[:-1] + edges[1:]) / 2.
yvals = centres.repeat(counts)
max_offset = width / counts.max()
offsets = np.hstack((np.arange(cc) - 0.5 * (cc - 1)) for cc in counts)
xvals = xpos + (offsets * max_offset)
ax_scatter.scatter(xvals, yvals, s=40, c= c,clip_on=False)
ax_scatter.set_axis_off()
ax.set_axis_off()
The result:

Reader Interactions