def showterr(): cproj = w.geometry.cells["proj"] lproj = np.dstack([ cproj[w.geometry.links["cells"][...,0]], w.geometry.links["proj"], cproj[w.geometry.links["cells"][...,1]] ]) #cave_proj = w.geometry.cells["proj"][caves] #pathcells = w.cells[["cell","level"]][path] #path_proj = w.geometry.cells["proj"][pathcells["cell"]] river_under_water = w.linksincells(np.nonzero(w.cells["terrain"] == w.terrainlookup["Water"])[0], both = True) entitydict = w.entities_location entitykeys = np.array(list(entitydict.keys())) items = w.entities_location.items() sites = np.array([i[0] for i in items]) glyphs = np.array(['$'+i[1][0].glyph+'$' for i in items]) entitycolormap = {entity.AbbeyEarth: 'brown', entity.AbbeyWater: 'blue', entity.AbbeyAir: 'y', entity.AbbeyFire: 'r', entity.AbbeyAether: 'cyan', entity.Settlement: 'w', entity.DeepSettlement: '0.25', entity.RuinedSettlement: 'grey', entity.CaveMouth: 'k', entity.Troop: 'magenta'} for i in range(w.levels): i_entities = w.cells["level"][sites] == i i_sites = sites[i_entities] #i_glyphs = list(glyphs[i_entities]) entities_proj = w.geometry.cells["proj"][w.cells["cell"][i_sites]] entities = xmath.vget(i_sites, entitydict) entities_c = [entitycolormap[type(e[0])] for e in entities] #print() #i_road = roadcells[w.cells["level"][roadcells] == i] #road_proj = w.geometry.cells["proj"][w.cells["cell"][i_road]] river_links = w.links["link"][(np.isfinite(w.links["river"])) & (w.links["level"] == i) &~river_under_water] river = lproj[river_links].transpose(0,2,1) badriver = np.abs(river[...,0,0]-river[...,2,0]) > 200 lhs = (river[...,0] <0) index = lhs & badriver[...,np.newaxis] river[index,0] = river[index,0] + 360 road_links = (w.links["link"][(w.links["road"] < 2) & (w.links["level"] == i)] ) road = lproj[road_links].transpose(0,2,1) badroad = np.abs(road[...,0,0]-road[...,2,0]) > 200 lhs = (road[...,0] <0) index = lhs & badroad[...,np.newaxis] road[index,0] = road[index,0] + 360 color = terrain["color"][w.cells["terrain"][w.cells["level"]==i]] colorrgb=[colorConverter.to_rgb(name) for name in color] index = (w.cells["level"] == i) fig = plt.figure() fig.set_size_inches(15,6) ax = plt.axes() ax.set_aspect("equal") ax.set_xlim(-180, 180) ax.set_ylim(-90, 90) coll = PolyCollection(flatpolys, edgecolors='none', array = w.cells["elevation"][index], cmap=mpl.cm.jet) coll.set(array=None, facecolors=colorrgb) ax.add_collection(coll) #plt.colorbar(coll) line_segments = LineCollection(road, linestyles = 'solid', color="0.75") # array=w.links["road"] # [(np.isfinite(w.links["road"])) & # (w.links["level"] == i)]) #ax.add_collection(line_segments) line_segments = LineCollection(river, linestyles = 'solid', color = "b") # array=w.links["river"] # [(np.isfinite(w.links["river"])) & # (w.links["level"] == i)]) ax.add_collection(line_segments) ax.plot() ax.scatter(x=entities_proj[...,0],y=entities_proj[...,1], c=entities_c) plt.show()
class Render: def __init__(self, vertices, faces, figure): self.theta = [90, 180, 90] self.translate = [0, 0, -3.5] self.theta_ = [90, 180, 90] self.translate_ = [0, 0, -3.5] self.mouse_ = [0, 0] self.vector_ = [0, 0] self.V = np.array(vertices) self.F = np.array(faces) self.press = False self.fig = figure self.fig.set(facecolor="slategrey") x, y = figure.get_size_inches() * figure.dpi x_lim = x / y self.ax = self.fig.add_axes([0, 0, 1, 1], xlim=[-x_lim, +x_lim], ylim=[-1, +1], frameon=False) render = self.render(self.theta, self.translate) self.collection = PolyCollection(render[0], closed=True, facecolor=render[1]) self.ax.add_collection(self.collection) def draw(self, x, y): render = self.render(self.theta, self.translate) x_lim = x / y self.ax.set_xlim(-x_lim, +x_lim) self.collection.set(verts=render[0]) self.collection.set(facecolor=render[1]) self.fig.model_canvas.draw_idle() def on_move(self, event): if not self.press or not event.inaxes: return self.vector_ = [ event.xdata - self.mouse_[0], event.ydata - self.mouse_[1] ] self.theta[0] = self.theta_[0] + (self.vector_[1] * -100) self.theta[2] = self.theta_[2] + (self.vector_[0] * 100) self.draw(*self.fig.get_size_inches() * self.fig.dpi) def on_press(self, event): if not event.inaxes or self.press: return self.press = True self.theta_ = [deg for deg in self.theta] self.translate_ = [vec for vec in self.translate] self.mouse_ = [event.xdata, event.ydata] def on_release(self, _): self.press = False self.theta_ = [deg for deg in self.theta] self.translate_ = [vec for vec in self.translate] self.mouse_ = [0, 0] self.vector_ = [0, 0] def render(self, rotate_vector, translate_vector): v, f = self.V, self.F v = (v - (v.max(0, initial=0) + v.min(0, initial=0)) / 2) / max(v.max(0, initial=0) - v.min(0, initial=0)) mvp = perspective(25, 1, 1, 100) @ translate( translate_vector[0], translate_vector[1], translate_vector[2]) @ x_rotate(rotate_vector[0]) @ y_rotate( rotate_vector[1]) @ z_rotate(rotate_vector[2]) v = np.c_[v, np.ones(len(v))] @ mvp.T v /= v[:, 3].reshape(-1, 1) v = v[f] t = v[:, :, :2] z = -v[:, :, 2].mean(axis=1) z_min, z_max = z.min(), z.max() z = (z - z_min) / (z_max - z_min) c = plt.get_cmap("magma")(z) i = np.argsort(z) # t, c = return t[i, :], c[i, :]
def candlestick2(ax, opens, closes, highs, lows, width=1, colorup='red', colordown='cyan', #alpha=0.75, alpha=1, ): """ 根据matplotlib.finance.candlestick2修改 Represent the open, close as a bar line and high low range as a vertical line. ax : an Axes instance to plot to width : the bar width in points colorup : the color of the lines where close >= open colordown : the color of the lines where close < open alpha : bar transparency return value is lineCollection, barCollection """ # note this code assumes if any value open, close, low, high is # missing they all are missing delta = width/2. barVerts = [ ( (i-delta, open), (i-delta, close), (i+delta, close), (i+delta, open) ) for i, open, close in zip(xrange(len(opens)), opens, closes) if open != -1 and close!=-1 ] rangeSegments = [ ((i, low), (i, high)) for i, low, high in zip(xrange(len(lows)), lows, highs) if low != -1 ] r,g,b = colorConverter.to_rgb(colorup) colorup = r,g,b,alpha r,g,b = colorConverter.to_rgb(colordown) colordown = r,g,b,alpha colord = { True : colorup, False : colordown, } r,g,b = colorConverter.to_rgb('black') fcolorup = r,g,b,alpha fcolord = { True : fcolorup, False : colordown, } colors = [colord[open<close] for open, close in zip(opens, closes) if open!=-1 and close !=-1] fcolors = [fcolord[open<close] for open, close in zip(opens, closes) if open!=-1 and close !=-1] assert(len(barVerts)==len(rangeSegments)) useAA = 0, # use tuple here lw = 0.5, # and here rangeCollection = LineCollection(rangeSegments, #colors = ( (0,0,0,1), ), colors = colors, linewidths = lw, antialiaseds = useAA, ) barCollection = PolyCollection(barVerts, facecolors = fcolors, #edgecolors = ( (0,0,0,1), ), edgecolors = colors, antialiaseds = useAA, linewidths = lw, ) minx, maxx = 0, len(rangeSegments) miny = min([low for low in lows if low !=-1]) maxy = max([high for high in highs if high != -1]) corners = (minx, miny), (maxx, maxy) ax.update_datalim(corners) ax.autoscale_view() # add these last ax.add_collection(rangeCollection) ax.add_collection(barCollection) rangeCollection.set(zorder=1) barCollection.set(zorder=2) #bar要覆盖掉range,避免range的线条颜色在bar中显示 return rangeCollection, barCollection