我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用matplotlib.pyplot.Line2D()。
def drawComplex(origData, ripsComplex, axes=[-6,8,-6,6]): plt.clf() plt.axis(axes) plt.scatter(origData[:,0],origData[:,1]) #plotting just for clarity for i, txt in enumerate(origData): plt.annotate(i, (origData[i][0]+0.05, origData[i][1])) #add labels #add lines for edges for edge in [e for e in ripsComplex if len(e)==2]: #print(edge) pt1,pt2 = [origData[pt] for pt in [n for n in edge]] #plt.gca().add_line(plt.Line2D(pt1,pt2)) line = plt.Polygon([pt1,pt2], closed=None, fill=None, edgecolor='r') plt.gca().add_line(line) #add triangles for triangle in [t for t in ripsComplex if len(t)==3]: pt1,pt2,pt3 = [origData[pt] for pt in [n for n in triangle]] line = plt.Polygon([pt1,pt2,pt3], closed=False, color="blue",alpha=0.3, fill=True, edgecolor=None) plt.gca().add_line(line) plt.show()
def drawComplex(data, ph, axes=[-6, 8, -6, 6]): plt.clf() plt.axis(axes) # axes = [x1, x2, y1, y2] plt.scatter(data[:, 0], data[:, 1]) # plotting just for clarity for i, txt in enumerate(data): plt.annotate(i, (data[i][0] + 0.05, data[i][1])) # add labels # add lines for edges for edge in [e for e in ph.ripsComplex if len(e) == 2]: # print(edge) pt1, pt2 = [data[pt] for pt in [n for n in edge]] # plt.gca().add_line(plt.Line2D(pt1,pt2)) line = plt.Polygon([pt1, pt2], closed=None, fill=None, edgecolor='r') plt.gca().add_line(line) # add triangles for triangle in [t for t in ph.ripsComplex if len(t) == 3]: pt1, pt2, pt3 = [data[pt] for pt in [n for n in triangle]] line = plt.Polygon([pt1, pt2, pt3], closed=False, color="blue", alpha=0.3, fill=True, edgecolor=None) plt.gca().add_line(line) plt.show()
def __line(self, neuron1, neuron2): line = plt.Line2D( # (neuron1.x - x_adjustment, neuron2.x + x_adjustment), # (neuron1.y - y_adjustment, neuron2.y + y_adjustment)) (neuron1.x-1, neuron2.x+1), (neuron1.y, neuron2.y)) plt.gca().add_line(line)
def draw(self, renderer): import matplotlib.pylab as plt ax = self.get_axes() if self.flip: y = self._linspace(ax.get_ylim(), ax.get_yscale()) self.set_xdata(self.fn(y)) self.set_ydata(y) else: x = self._linspace(ax.get_xlim(), ax.get_xscale()) self.set_xdata(x) self.set_ydata(self.fn(x)) plt.Line2D.draw(self, renderer) self.set_xdata([]) self.set_ydata([])
def draw_neural_net(self, ax, left, right, bottom, top, layer_sizes): ''' Draw a neural network cartoon using matplotilb. :usage: >>> fig = plt.figure(figsize=(12, 12)) >>> draw_neural_net(fig.gca(), .1, .9, .1, .9, [4, 7, 2]) :parameters: - ax : matplotlib.axes.AxesSubplot The axes on which to plot the cartoon (get e.g. by plt.gca()) - left : float The center of the leftmost node(s) will be placed here - right : float The center of the rightmost node(s) will be placed here - bottom : float The center of the bottommost node(s) will be placed here - top : float The center of the topmost node(s) will be placed here - layer_sizes : list of int List of layer sizes, including input and output dimensionality ''' n_layers = len(layer_sizes) + 1 maxLayerSize = 0 for layer_group in layer_sizes: layer_size = sum(map(lambda x: x[1], layer_group)) maxLayerSize = max(maxLayerSize, layer_size) v_spacing = (top - bottom)/float(maxLayerSize) h_spacing = (right - left)/float(len(layer_sizes) - 1) # Nodes for layerIndex, layer_group in enumerate(layer_sizes): layer_size = sum(map(lambda x: x[1], layer_group)) layer_top = v_spacing*(layer_size - 1)/2. + (top + bottom)/2. currentIndex = 0 for functionIndex, (inputSize, outputSize) in enumerate(layer_group): if outputSize > 0: for nodeIndex in range(outputSize): circle = plt.Circle((layerIndex*h_spacing + left, layer_top - currentIndex*v_spacing), v_spacing/4., color=self.activationFunctionColors[functionIndex], ec='k', zorder=4) ax.add_artist(circle) currentIndex += 1 else: # Null nodes, ignore and keep going continue """ # Edges for n, (layer_size_a, layer_size_b) in enumerate(zip(layer_sizes[:-1], layer_sizes[1:])): layer_top_a = v_spacing*(layer_size_a - 1)/2. + (top + bottom)/2. layer_top_b = v_spacing*(layer_size_b - 1)/2. + (top + bottom)/2. for m in xrange(layer_size_a): for o in xrange(layer_size_b): line = plt.Line2D([n*h_spacing + left, (n + 1)*h_spacing + left], [layer_top_a - m*v_spacing, layer_top_b - o*v_spacing], c='k') ax.add_artist(line) """
def _make_edge(self, edge, u, layer_u, v, layer_v): if layer_u == layer_v: lon1 = self._substrate.node[u]['Longitude'] lat1 = self._substrate.node[u]['Latitude'] lon2 = self._substrate.node[v]['Longitude'] lat2 = self._substrate.node[v]['Latitude'] self._background_map.drawgreatcircle(lon1, lat1, lon2, lat2, color="k", ax=self._layer_axes[layer_u], linewidth=self._edge_weights[edge], # alpha = 0.8, # dashes=(4,1), zorder=1) return data_coordinates_u = self._nodes_to_data_coordinates[layer_u][u] data_coordinates_v = self._nodes_to_data_coordinates[layer_v][v] x_u, y_u = self._transform_from_data_to_figure_coordinates(data_coordinates_u, self._layer_axes[layer_u]) x_v, y_v = self._transform_from_data_to_figure_coordinates(data_coordinates_v, self._layer_axes[layer_v]) dx = abs(x_u - x_v) if x_u < x_v: x_left, y_left = x_u, y_u x_right, y_right = x_v, y_v else: x_left, y_left = x_v, y_v x_right, y_right = x_u, y_u n = 30 x_values = np.linspace(x_left, x_right, n, endpoint=True) pi = 2 * np.arcsin(1.0) y_values = np.linspace(y_left, y_right, n, endpoint=True) if self._curved_edges and u != self._extended_graph.super_source and v != self._extended_graph.super_sink: y_values += 0.8 * MAP_MARGIN * np.sin((x_values - x_left) * pi / dx) return plt.Line2D(x_values, y_values, transform=self._fig.transFigure, linestyle="-", color="k", linewidth=self._edge_weights[edge], zorder=1.0)