def test_get_segments(): segments = np.tile(np.linspace(0, 1, 256), (2, 1)).T lc = LineCollection([segments]) readback, = lc.get_segments() # these should comeback un-changed! assert np.all(segments == readback)
def draw_graph_edges(figure: plt.Figure, axis: plt.Axes, graph: nx.Graph): """draws graph edges from node to node, colored by weight Parameters ---------- figure: plt.Figure a matplotlib Figure axis: plt.Axes a matplotlib Axes, part of Figure graphs: nx.Graph a networkx graph, assumed to have edges formed like graph.add_edge((0, 1), (0, 2), weight=1.234) Notes ----- modifes figure and axis in-place """ weights = np.array([i["weight"] for i in graph.edges.values()]) # graph is (row, col), transpose to get (x, y) segments = [] for edge in graph.edges: segments.append([edge[0][::-1], edge[1][::-1]]) line_coll = LineCollection(segments, linestyle='solid', cmap="plasma", linewidths=0.3) line_coll.set_array(weights) vals = np.concatenate(line_coll.get_segments()) mnvals = vals.min(axis=0) mxvals = vals.max(axis=0) ppvals = vals.ptp(axis=0) buffx = 0.02 * ppvals[0] buffy = 0.02 * ppvals[1] line_coll.set_linewidth(0.3 * 512 / ppvals[0]) axis.add_collection(line_coll) axis.set_xlim(mnvals[0] - buffx, mxvals[0] + buffx) axis.set_ylim(mnvals[1] - buffy, mxvals[1] + buffy) # invert yaxis for image-like orientation axis.invert_yaxis() axis.set_aspect("equal") divider = make_axes_locatable(axis) cax = divider.append_axes("right", size="5%", pad=0.05) figure.colorbar(line_coll, ax=axis, cax=cax) axis.set_title("PCC neighbor graph")
def set_data(self, zname=None, zdata=None, zcolor=None, plot_type="poly"): if zdata != None: if plot_type is "poly": if zname not in self.clts: #plottables['plotted']:#self.pd.list_data(): clt = PolyCollection( [], alpha=0.5, antialiased=True ) #, rasterized=False, antialiased=False) if zcolor is not None: clt.set_color(colorConverter.to_rgba(zcolor)) self.clts[zname] = clt self.axe.add_collection(self.clts[zname]) self.clts[zname].set_verts(zdata) elif plot_type is "line": if zname not in self.clts: clt = LineCollection( zdata) #, linewidths=(0.5, 1, 1.5, 2), #linestyles='solid', colors=("red", "blue", "green")) if zcolor is not None: clt.set_color(zcolor) else: clt.set_array(arange(len(zdata))) else: self.clts[zname].set_verts(zdata) #self.set_xlim(x.min(), x.max()) #self.set_ylim(ys.min(), ys.max()) elif plot_type is "scatter": self.axe.scatter(zdata, zdata) elif plot_type is "colormap": self.axe.pcolormesh(x, y, z) if 0: x = arange(3) ys = array([x + i for i in arange(5)]) #xdata=arange(len(getattr(self, zname))) data = [list(zip(x, y)) for y in ys] line_segments = LineCollection(data, linewidths=1, linestyles='solid', colors=mycolors) print data print len(data) #print line_segments.properties() #print line_segments.set_hatch("O") #print dir(self.axe) print[p.vertices for p in line_segments.get_paths()] #) print line_segments.get_segments() line_segments.set_array(arange(len(data))) x = arange(3) ys = array([x + i for i in arange(2)]) #xdata=arange(len(getattr(self, zname))) data = [list(zip(x, y)) for y in ys] line_segments.set_verts(data) #self.axe.add_collection(line_segments, autolim=True) clt = self.axe.scatter(x, x) #clt.set_linestyle("solid") print dir(clt) print clt.get_paths() if 0: #clt=QuadMesh(0, 0, [1]) n = 12 x = linspace(-1.5, 1.5, n) y = linspace(-1.5, 1.5, n * 2) X, Y = meshgrid(x, y) print X Qx = cos(Y) - cos(X) Qz = sin(Y) + sin(X) Qx = (Qx + 1.1) Z = sqrt(X**2 + Y**2) / 5 Z = (Z - Z.min()) / (Z.max() - Z.min()) Zm = ma.masked_where(fabs(Qz) < 0.5 * amax(Qz), Z) #ax = fig.add_subplot(121) #self.axe.set_axis_bgcolor("#bdb76b") clt = self.axe.pcolormesh(Z) #print dir(clt) self.axe.set_title('Without masked values')