MThmYmQ2ZjJmNjJkYWU2MGRjN2JjY2M3ZGU5NzRmNWE2ZDg0OGYwMDBhZmY2ZjdiYjI5MjRlMTdjN2IxYWFhNg.msfhb3x7.eyJ1cmwiOiIvcGwvY291cnNlX2luc3RhbmNlLzIvaW5zdGFuY2VfcXVlc3Rpb24vMTczLyIsImF1dGhuX3VzZXJfaWQiOiIxIn0
Skip to main content Accessibility guide
PrairieLearn Go home
  • XC 101, SectionA
  • Assessments
  • Gradebook
  • C4
Load from disk
  • Dev User student
    View type
    ✓ Staff view staff ✓ Student view student ✓ Student view without access restrictions student
    Effective user
    Customize…
    Course Requests Settings Log out

Regenerate assessment instance

Warning: Regenerating the assessment instance will select a new set of questions from this assessment. Any progress on the current assessment instance will be lost.

Are you sure you want to regenerate the assessment instance?

Course staff: Regenerate your assessment instance to pick up changes to the assessment or to get a fresh set of questions.

C4.36. Element pl-graph: Displaying graphs from data

This question was created to show the graph rendering capabilities of the pl-graph element.

Here's a static graph that was created by hand with DOT markup:

1
2
3
4
5
6
<pl-graph>
digraph G {
  A -> B
  B -> C
}
</pl-graph>
G A A B B A->B C C B->C

Here's a graph with randomly-generated edge weights. Because here the graph is specified in the question.html, parameters can be templated using Mustache.

1
2
3
4
5
6
<pl-graph>
digraph G {
  A -> B [label="{{ params.weight1 }}"]
  B -> C [label="{{ params.weight2 }}"]
}
</pl-graph>
G A A B B A->B  8 C C B->C  1

Here's a graph created from a randomly-generated adjacency matrix. By default, weights will be displayed when they are not 0 or 1. The matrix and labels can be specified with params-name and params-name-labels, respectively.

1
2
3
4
mat = np.random.random((3, 3))
mat = mat / la.norm(mat, 1, axis=0)
data['params']['labels'] = pl.to_json(['A', 'B', 'C'])
data['params']['matrix'] = pl.to_json(mat)
1
2
<pl-matrix-latex params-name="matrix"></pl-matrix-latex>
<pl-graph params-name="matrix" params-name-labels="labels"></pl-graph>
\begin{bmatrix} 0.38 & 0.44 & 0.73\\ 0.19 & 0.36 & 0.26\\ 0.43 & 0.21 & 0.01\\\end{bmatrix}
A A A->A 0.38 B B A->B 0.19 C C A->C 0.43 B->A 0.44 B->B 0.36 B->C 0.21 C->A 0.73 C->B 0.26 C->C 0.01

To force no weights being displayed, the attribute weights can be set to "false". Here is the same graph with no edge weights.

A A A->A B B A->B C C A->C B->A B->B B->C C->A C->B C->C

To force the graph to be undirected, the attribute directed can be set to "false". Here is an undirected graph. The input matrix must be symmetric in this case.

\begin{bmatrix} 0.38 & 0.44 & 0.73\\ 0.44 & 0.36 & 0.26\\ 0.73 & 0.26 & 0.01\\\end{bmatrix}
A A A--A 0.38 B B B--A 0.44 B--B 0.36 C C C--A 0.73 C--B 0.26 C--C 0.01

The formatting of the weight labels can also be changed with the weights-digits and weights-presentation-type attributes. This example has weights-digits="5".

A A A->A 0.38363 B B A->B 0.18932 C C A->C 0.42704 B->A 0.43556 B->B 0.35608 B->C 0.20836 C->A 0.72517 C->B 0.26024 C->C 0.01459

Here is another graph with binary 0 or 1 weights. These weights will not be displayed unless specified with weights="true".

\begin{bmatrix} 0 & 1 & 1\\ 0 & 0 & 0\\ 1 & 0 & 0\\\end{bmatrix}
0 0 2 2 0->2 2->0 1 1 1->0

Here is a graph with negative weights. These weights will not be displayed unless specified with negative-weights="true". In the input matrix, to hide an edge, simply set the corresponding entry to None.

1
2
3
array([[None, 2, -1.5],
       [-1.1, -1.4, None],
       [None, 4, -2]], dtype=object)
A A B B A->B -1.10 B->A 2.00 B->B -1.40 C C B->C 4.00 C->A -1.50 C->C -2.00

The pl-graph element has the ability to be extended on a course level to create graphs based on custom inputs. This course extension located in elementExtensions/pl-graph/edge-inc-matrix adds the capability of rendering edge-incidence matrices as graphs.

\begin{bmatrix} -1 & 0 & 1 & 0\\ 0 & -1 & 1 & 0\\ 1 & 0 & 0 & -1\\ 0 & 1 & -1 & 0\\\end{bmatrix}
0 0 2 2 0->2 1 1 2->1 1->2 3 3 3->0

For more information on how to create extensions, check the extension documentation and graph element documentation.

Here's a graph created from a randomly-generated networkx graph. To specify the use of a networkx graph, set params-type="networkx". The graph can be specified with params-name. To make it accessible to screen readers, set aria-label to a description of the graph; since a static label can't describe a randomly-generated graph, that description is generated in server.py from the graph data.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
random_graph = nx.gnm_random_graph(5, 6)

for in_node, out_node, edge_data in random_graph.edges(data=True):
    edge_data['label'] = random.choice(string.ascii_lowercase)

data['params']['random-graph'] = pl.to_json(random_graph)

edges = ", ".join(
    f"{in_node} to {out_node} (labeled {edge_data['label']})"
    for in_node, out_node, edge_data in random_graph.edges(data=True)
)
data['params']['random_graph_alt'] = (
    f"Undirected graph with {random_graph.number_of_nodes()} nodes "
    f"and {random_graph.number_of_edges()} edges: {edges}."
)
1
<pl-graph params-type="networkx" params-name="random-graph" aria-label="{{ params.random_graph_alt }}"></pl-graph>
0 0 2 2 0--2 o 3 3 0--3 g 2--3 e 4 4 3--4 o 1 1 1--2 g 1--3 i

Rendering directed graphs and multigraphs is supported natively with networkx. Here, we also set rankdir="LR" in the constructor for the graph.

1 1 2 2 1->2 1->2 3 3 1->3 2->1 2->3 3->2

The color of each node or edge in a networkx graph can be set by assigning the 'color' attribute in the associated data dictionary for each object. In the example below, we've set the color of each edge to 'blue' and used different colors for nodes in different layers.

0 0 5 5 0--5 6 6 0--6 7 7 0--7 8 8 0--8 9 9 0--9 10 10 5--10 11 11 5--11 12 12 5--12 13 13 5--13 6--10 6--11 6--12 6--13 7--10 7--11 7--12 7--13 8--10 8--11 8--12 8--13 9--10 9--11 9--12 9--13 14 14 10--14 15 15 10--15 16 16 10--16 11--14 11--15 11--16 12--14 12--15 12--16 13--14 13--15 13--16 1 1 1--5 1--6 1--7 1--8 1--9 2 2 2--5 2--6 2--7 2--8 2--9 3 3 3--5 3--6 3--7 3--8 3--9 4 4 4--5 4--6 4--7 4--8 4--9 17 17 14--17 18 18 14--18 15--17 15--18 16--17 16--18 19 19 17--19 20 20 17--20 21 21 17--21 22 22 17--22 18--19 18--20 18--21 18--22 23 23 19--23 24 24 19--24 25 25 19--25 26 26 19--26 20--23 20--24 20--25 20--26 21--23 21--24 21--25 21--26 22--23 22--24 22--25 22--26 27 27 23--27 28 28 23--28 29 29 23--29 24--27 24--28 24--29 25--27 25--28 25--29 26--27 26--28 26--29

The source-file-name attribute allows loading graph content from an external file. This is particularly useful for graphs with complex DOT syntax, such as record-based nodes that use angle brackets <>, which can cause issues when embedded directly in HTML.

1
<pl-graph source-file-name="record-graph.dot"></pl-graph>

The file record-graph.dot contains:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
// Example of record-based nodes
// Reference: https://graphviz.org/doc/info/shapes.html#record
digraph structs {
    node [shape=record];
    struct1 [label="<f0> left|<f1> middle|<f2> right"];
    struct2 [label="<f0> one|<f1> two"];
    struct3 [label="hello\nworld |{ b |{c|<here> d|e}| f}| g | h"];
    struct1:f1 -> struct2:f0;
    struct1:f2 -> struct3:here;
}
structs struct1 left middle right struct2 one two struct1:f1->struct2:f0 struct3 hello world b c d e f g h struct1:f2->struct3:here
Additional attempts available with new variants

Correct answer

Collection 4

Assessment overview
Total points: 0/360
Score:
0%

Question C4.36

Value: 2
All variants: Open (current)
Total points: — /10
Auto-graded question

This form is only for reporting errors in the question itself. Do not use this form if you just don't know how to answer the question.

Previous question Next question

 Personal Notes

No attached notes

Attached files will be saved here for your reference. These files act as personal notes and can be used for your own review purposes. They are not used for grading.

Max file size: 10 MB

Attached personal notes will be saved here for your reference. These notes can be used for your own review purposes. They are not used for grading.

Staff information

Student details

Dev User
dev@example.com

Question

QID:
element/graph
Title:
Element pl-graph: Displaying graphs from data

Variant

Started at:
2026-08-04 16:38:05 (CDT)
Duration:
0s
Show/Hide answer
{}

Assessment instance

Assessment:
gallery/elements
Started at:
2026-08-04 16:37:17 (CDT)
Duration:
0s
View log
This box is not visible to students.