This blog post has the following structure: first, we'll learn how to draw the little TV screens, in a second step we'll draw the red boxes with letters in it and finally we'll put everything together to produce a design similar to the original one.
from IPython.display import Image
Image(filename="files/Demokratie.jpg")
Basically, they're a couple of colored bars, just like in an histogram. So, as a first step, we'll generate a bar graph from a random vector with six components.
components = rand(6)
components
array([ 0.79713122, 0.00178879, 0.1149884 , 0.71496044, 0.74560478, 0.07908629])
bar(linspace(0, 1, num=6), components, width=0.16, color=['black', 'red', 'gray', 'brown', 'gray', 'green'], edgecolor=['black', 'red', 'gray', 'brown', 'gray', 'green'])
axis('off')
(0.0, 1.2000000000000002, 0.0, 0.80000000000000004)
Let's put this together in a neat little function.
def plot_bars():
components = rand(6)
bar(linspace(0, 1, num=6), components, width=0.16, color=['black', 'red', 'gray', 'brown', 'gray', 'green'], edgecolor=['black', 'red', 'gray', 'brown', 'gray', 'green'])
axis('off')
plot_bars()
We can now try to put a lot of these little plots all over a figure.
grid_x = 10
grid_y = 5
delta = 0.3
# x dimension calculation
a = 1 / ((grid_x + 1) * delta + grid_x)
# loop
for i in range(grid_x):
for j in range(grid_y):
axes([delta * a + i * (delta * a + a), delta * a + j * (delta * a + a), a, a])
plot_bars()
Looks nice already! Let's try to figure out how to round courners. I've found this link about annotating axes that seems to be useful.
ax = gca()
bbox_props = dict(boxstyle="rarrow,pad=0.3", fc="cyan", ec="b", lw=2)
t = ax.text(0.5, 0.5, "dsfsdfsd", ha="center", va="center", rotation=0,
size=15,
bbox=bbox_props)
bb = t.get_bbox_patch()
bb.set_boxstyle("round4", pad=0.6)
import matplotlib.transforms as mtransforms
from matplotlib.patches import FancyBboxPatch
# Bbox object around which the fancy box will be drawn.
bb = mtransforms.Bbox([[0.3, 0.4], [0.5, 0.6]])
def draw_bbox(ax, bb):
# boxstyle=square with pad=0, i.e. bbox itself.
p_bbox = FancyBboxPatch((bb.xmin, bb.ymin),
abs(bb.width), abs(bb.height),
boxstyle="round4,pad=0.05", lw=3,
ec="k", fc="none", zorder=10.,
)
ax.add_patch(p_bbox)
draw_bbox(gca(), bb)
With this in mind, we can go back to what we were doing above and draw a fancy box around our screens.
import matplotlib.transforms as mtransforms
from matplotlib.patches import FancyBboxPatch
def draw_box(ax):
p = 0.2
bb = mtransforms.Bbox([[p, p], [1 - p, 1 - p]])
p_bbox = FancyBboxPatch((bb.xmin, bb.ymin),
abs(bb.width), abs(bb.height),
boxstyle="round4,pad=0.1", lw=4,
ec="k", fc="none", zorder=10.,
)
ax.add_patch(p_bbox)
def draw_other_box(ax):
p = 0.1
bb = mtransforms.Bbox([[2 * p, p], [1 - 2 * p, 1 - p]])
p_bbox = FancyBboxPatch((bb.xmin, bb.ymin),
abs(bb.width), abs(bb.height),
boxstyle="round4,pad=0.1", lw=6,
ec="blue", fc="none", zorder=2.,
fill=True,
color='white')
ax.add_patch(p_bbox)
ax = gca()
draw_box(ax)
draw_other_box(ax)
#axis('off')
grid_x = 10
grid_y = 5
delta = 0.3
# x dimension calculation
a = 1 / ((grid_x + 1) * delta + grid_x)
b = 0.4 * a
# loop
for i in range(grid_x):
for j in range(grid_y):
ax = axes([delta * a + i * (delta * a + a), delta * a + j * (delta * a + a), a, a])
draw_box(ax)
axis('off')
ax = axes([delta * a + i * (delta * a + a) + b / 2., delta * a + j * (delta * a + a) + b / 2., a - b, a - b])
plot_bars()
figure(figsize=(8, 8))
ax = gca()
ax.add_artist(Rectangle((0, 0), 7, 2, color='red'))
ax.set_xlim((-10, 10))
ax.set_ylim((-10, 10))
(-10, 10)
figure(figsize=(8, 8))
ax = gca()
ax.add_artist(Rectangle((0, 0), 2, 7, color='red'))
ax.set_xlim((-10, 10))
ax.set_ylim((-10, 10))
text(0, 3.5, "D", color='white', fontsize='67')
<matplotlib.text.Text at 0x6704e30>