def plot_graph(graph, adj): """ Use the package networkx to produce a diagrammatic plot of the graph, with the nodes in the graph colored according to their current labels. Note that only 20 unique colors are available for the current color map, so common colors across nodes may be coincidental. Parameters ---------- graph : Tuple[Node, ...] The graph to plot adj : numpy.ndarray, shape=(N, N) The adjacency-matrix for the graph. Nonzero entries indicate the presence of edges. Returns ------- Tuple[matplotlib.fig.Fig, matplotlib.axis.Axes] The figure and axes for the plot.""" import networkx as nx import numpy as np import matplotlib.cm as cm import matplotlib.pyplot as plt g = nx.Graph() for n, node in enumerate(graph): g.add_node(n) # construct a network-x graph from the adjacency matrix: a non-zero entry at adj[i, j] # indicates that an egde is present between Node-i and Node-j. Because the edges are # undirected, the adjacency matrix must be symmetric, thus we only look ate the triangular # upper-half of the entries to avoid adding redundant nodes/edges g.add_edges_from(zip(*np.where(np.triu(adj) > 0))) # we want to visualize our graph of nodes and edges; to give the graph a spatial representation, # we treat each node as a point in 2D space, and edges like compressed springs. We simulate # all of these springs decompressing (relaxing) to naturally space out the nodes of the graph # this will hopefully give us a sensible (x, y) for each node, so that our graph is given # a reasonable visual depiction pos = nx.spring_layout(g) # make a mapping that maps: node-lab -> color, for each unique label in the graph color = list( iter(cm.Vega20b(np.linspace(0, 1, len(set(i.label for i in graph)))))) color_map = dict(zip(sorted(set(i.label for i in graph)), color)) colors = [ color_map[i.label] for i in graph ] # the color for each node in the graph, according to the node's label # render the visualization of the graph, with the nodes colored based on their labels! fig, ax = plt.subplots() nx.draw_networkx_nodes(g, pos=pos, ax=ax, nodelist=range(len(graph)), node_color=colors) nx.draw_networkx_edges(g, pos, ax=ax, edgelist=g.edges()) return fig, ax
def plot_graph(graph, adj, labels): """ Use the package networkx to produce a diagrammatic plot of the graph, with the nodes in the graph colored according to their current labels. Note that only 20 unique colors are available for the current color map, so common colors across nodes may be coincidental. Parameters ---------- graph : Tuple[Node, ...] The graph to plot adj : numpy.ndarray, shape=(N, N) The adjacency-matrix for the graph. Nonzero entries indicate the presence of edges. Returns ------- Tuple[matplotlib.fig.Fig, matplotlib.axis.Axes] The figure and axes for the plot.""" g = nx.Graph() for n, node in enumerate(graph): g.add_node(n) g.add_edges_from(zip(*np.where(np.triu(adj) > 0))) pos = nx.spring_layout(g) color = list( iter(cm.Vega20b(np.linspace(0, 1, len(set(i.label for i in graph)))))) color_map = dict(zip(sorted(set(i.label for i in graph)), color)) colors = [color_map[i.label] for i in graph] fig, ax = plt.subplots() nx.draw_networkx_nodes(g, pos=pos, ax=ax, nodelist=range(len(graph)), node_color=colors) nx.draw_networkx_edges(g, pos, ax=ax, edgelist=g.edges()) nx.draw_networkx_labels(g, pos=pos, ax=ax, labels=labels) return fig, ax
import numpy as np import scipy.cluster.hierarchy as sch import matplotlib matplotlib.use('Agg') import matplotlib.pylab as plt from matplotlib import cm import fastcluster as fcl import glob import os import cPickle import PIL (npArray, D, Z1, names) = cPickle.load(open("clusterstate.pickle")) im = PIL.Image.fromarray(cm.Vega20b(D, bytes=True)) im.save('heatmap.png')
import matplotlib.pyplot as plt from matplotlib.backends.backend_pdf import PdfPages import numpy as np from matplotlib import cm with open('data-user-groups.csv', 'r') as in_file: data = [] line = in_file.readline() for d in line.strip().split(','): data.append(int(d)) fig = plt.figure(figsize=(8, 4)) ax = fig.add_subplot(111, aspect='equal') labels = ('Watch', 'Dismiss only', 'No activity') cs = cm.Vega20b(np.arange(3) / 3.) patches, texts, autotexts = plt.pie(data, labels=labels, autopct='%1.1f%%', colors=cs, startangle=90) autotexts[0].set_color('w') plt.tight_layout() with PdfPages('user-groups-pie.pdf') as pdf: pdf.savefig(fig) plt.close()