<｜begin▁of▁sentence｜>### Instruction:
Given a graph where nodes are colored as 'red', 'blue', 'green', and 'yellow' and edges have a 'weight' attribute, how can we construct a matrix where rows and columns are ordered according to node colors and cells contain the sum of edge weights between nodes of the corresponding colors?
### Response:
import networkx as nx

# Create a graph
G = nx.Graph()
# Add edges with attributes
G.add_edge(0, 1, color='red', weight=2)
G.add_edge(1, 2, color='blue', weight=3)
G.add_edge(2, 0, color='green', weight=1)
G.add_edge(0, 3, color='red', weight=4)

# Set node attributes
color_map = {0: 'red', 1: 'blue', 2: 'green', 3: 'yellow'}
nx.set_node_attributes(G, color_map, 'color')

# Construct an attribute matrix based on edge weights
M, ordering = nx.attr_matrix(G, edge_attr='weight', node_attr='color', rc_order=['red', 'blue', 'green', 'yellow'])

print('Matrix:\n', M)
print('Ordering:', ordering)
<|EOT|>
