<｜begin▁of▁sentence｜>### Instruction:
As a network engineer, you may want to visually represent a directed graph with nodes and weighted edges, similar to setting up a network diagram. How would you generate a visualization of the graph so that the edge labels are rotated to align with the edges? This can be very useful for clearly understanding the connections and their corresponding weights within the network. If you have the data structure containing the graph information, you could make use of NetworkX to achieve this.
### Response:
import matplotlib.pyplot as plt
import networkx as nx

# Create a graph
G = nx.DiGraph()
G.add_edge('A', 'B', weight=1)
G.add_edge('B', 'C', weight=2)
G.add_edge('C', 'A', weight=3)

# Draw the graph
pos = nx.spring_layout(G)  # position the nodes using the spring layout
edge_labels = nx.get_edge_attributes(G, 'weight')

# Custom function to get label rotation
def get_label_rotation(source, target, pos):
    x1, y1 = pos[source]
    x2, y2 = pos[target]
    angle = np.degrees(np.arctan2(y2 - y1, x2 - x1))
    return angle

rotations = {(u, v): get_label_rotation(u, v, pos) for u, v in G.edges()}
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, label_pos=0.5, rotate=False)
nx.draw(G, pos, with_labels=True, node_color='lightblue')
for edge, rotation in rotations.items():
    text = plt.gca().get_xticklabels()[0]
    text.set_rotation(rotation)
plt.show()
<|EOT|>
