Skip to content

Commit add11ba

Browse files
dag chapter
1 parent f9bb10a commit add11ba

20 files changed

+422
-63
lines changed

figures/poetryplots/dag-argue.pdf

8.44 KB
Binary file not shown.

figures/poetryplots/df-plot.pdf

0 Bytes
Binary file not shown.

figures/poetryplots/heat-basic.pdf

0 Bytes
Binary file not shown.

figures/poetryplots/heat-cbar.pdf

0 Bytes
Binary file not shown.

figures/poetryplots/heat-log.pdf

0 Bytes
Binary file not shown.

figures/poetryplots/hockey-heat.pdf

-30 Bytes
Binary file not shown.

figures/poetryplots/mpl-table.pdf

0 Bytes
Binary file not shown.

figures/poetryplots/speedometer.pdf

0 Bytes
Binary file not shown.

figures/poetryplots/speedometers.pdf

0 Bytes
Binary file not shown.

figures/poetryplots/violin-bar.pdf

0 Bytes
Binary file not shown.

figures/poetryplots/violin-cal.pdf

0 Bytes
Binary file not shown.

figures/poetryplots/violin-streak.pdf

0 Bytes
Binary file not shown.

main.pdf

16.4 KB
Binary file not shown.

main.tex

+3
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ \section{Activity Calendar}
146146
\section{Heatmaps}
147147
\input{tex/poetry/heat.tex}
148148

149+
\section{Directed Graphs}
150+
\input{tex/poetry/dag.tex}
151+
149152
\section{Speedometer}
150153
\input{tex/poetry/speedometer.tex}
151154

python/.ipynb_checkpoints/Poetry-Figure-Dev-checkpoint.ipynb

+175-35
Large diffs are not rendered by default.

python/Poetry-Figure-Dev.ipynb

+168-28
Large diffs are not rendered by default.

python/dag-argue.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
fig, ax = plt.figure(), plt.axes()
2+
ax.set_aspect(1)
3+
ax.set_xlim(-4,4)
4+
ax.set_ylim(-3,3)
5+
ax.axis('off')
6+
7+
persuasive = make_node((0,1.5), 1, 'Persuasiveness')
8+
logic = make_node((2.5,-1.5), 1, 'Logic')
9+
decibels = make_node((-2.5,-1.5), 1, 'Decibels')
10+
11+
directed_edge(logic, persuasive)
12+
directed_edge(decibels, persuasive)

python/dag-edge.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
def directed_edge(c1, c2, ax = None):
2+
"""Draw an arrow from c1 to c2."""
3+
4+
if ax is None:
5+
ax = plt.gca()
6+
7+
center1 = c1.center
8+
center2 = c2.center
9+
10+
length = np.linalg.norm(np.array(center1) - np.array(center2))
11+
12+
r1 = c1.get_radius()
13+
r2 = c2.get_radius()
14+
15+
x1, x2 = center1[0], center2[0]
16+
y1, y2 = center1[1], center2[1]
17+
18+
# Find start and end of arrow based on circle radii
19+
# based on linear weights from convex combos
20+
tail_weight = r1 / length
21+
tail_x = (1-tail_weight)*x1 + tail_weight*x2
22+
tail_y = (1-tail_weight)*y1 + tail_weight*y2
23+
24+
head_weight = r2/ length
25+
head_x = (1-head_weight)*x2 + head_weight*x1
26+
head_y = (1-head_weight)*y2 + head_weight*y1
27+
28+
ax.annotate('', xy = (head_x, head_y),
29+
xytext = (tail_x, tail_y),
30+
arrowprops = dict(headwidth = 14,
31+
linewidth = .1,
32+
width = 2))

python/dag-node.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def make_node(center, radius = 'auto', label = '', ax = None):
2+
"""Plot labeled circle object to represent a node.
3+
Rrun after any changes to axes limits, aspect changes, etc if using radius = 'auto'."""
4+
if ax is None:
5+
ax = plt.gca()
6+
t = ax.text(center[0], center[1], label, ha = 'center', va = 'center')
7+
if radius == 'auto':
8+
plt.gcf().canvas.draw()
9+
box = t.get_window_extent().transformed(ax.transData.inverted())
10+
width = box.x1 - box.x0
11+
radius = (width/2)*1.2
12+
c = plt.Circle(center, radius,
13+
facecolor = (.9,.99,.9),
14+
edgecolor = 'black')
15+
ax.add_artist(c)
16+
return c

tex/poetry/dag.tex

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
%\subsection{Directed Graphs}
2+
Directed graphs arise in many settings. For plotting a large graph, like follower-following relationships in a social network, you might be best served making use of packages like networkx and nxviz. In other cases, you might do better working by hand. The \link{https://graphviz.readthedocs.io/en/stable/index.html}{graphviz} library is one possible solution. Here, we'll work directly with matplotlib. One directed graph use case might be in illustrating the directed acyclic graph (or DAG) representing a causal theory. In the directed acyclic graph framework (mostly associated with Judea Pearl's work in causal inference), a directed edge from $X$ to $Y$ means $X$ causes $Y$, at least in part. This lends itself well to plotting. Below we'll make a plot, using \code{plt.Circle()} to draw nodes and and creating edges with \code{ax.annotate()}.
3+
4+
Here, we create the nodes as circle objects and then draw the edges using \code{ax.annotate()}. Below, the circular nodes are created with the function \code{make_node()}. Then, the directed edge is drawn with \code{directed_edge()}. This doesn't allow for an edge from one node back to itself.
5+
6+
\pyfile{dag-node.py}
7+
8+
\pyfile{dag-edge.py}
9+
10+
The DAG plotted below describes the theory that the persuasiveness of an argument is caused by its logical soundness and the decibel level at which it is communicated.
11+
12+
\pyfile{dag-argue.py}
13+
14+
\begin{center}
15+
\includegraphics[width = 0.7\textwidth]{figures/poetryplots/dag-argue.pdf}
16+
\end{center}

0 commit comments

Comments
 (0)