import json from http.server import BaseHTTPRequestHandler, HTTPServer import numpy from matplotlib import pyplot class MyJSONHandler(BaseHTTPRequestHandler): def do_POST(self): content_length = int(self.headers.get('Content-Length', 0)) body = self.rfile.read(content_length) try: data = json.loads(body) # 处理 JSON 数据 response = {} self.send_response(200) self.send_header("Content-Type", "application/json") self.end_headers() self.wfile.write(json.dumps(response).encode()) draw0(data) except json.JSONDecodeError: self.send_response(400) self.end_headers() self.wfile.write(b"Invalid JSON") def http_init(): httpd = HTTPServer(('', 4050), MyJSONHandler) httpd.serve_forever() def draw_old(): # 网格大小 width, height = 16, 16 start_x, start_y = 3, 3 # 创建颜色数组(RGB) colors = numpy.ones((height, width, 3)) # 填充几个格子的颜色作为示例 colors[0, 0] = [0, 0, 0] # 黑色 colors[3, 3] = [1, 0, 0] # 红色 colors[5, 5] = [0, 1, 0] # 绿色 colors[10, 10] = [0, 0, 1] # 蓝色 figure, axes = pyplot.subplots(figsize=(6, 6)) axes.imshow(colors, extent=(0, width, 0, height), origin='lower') # 绘制网格线 for x in range(start_x, start_x + width): axes.axvline(x, color='black', linewidth=0.5) for y in range(start_y, start_y + height): axes.axhline(y, color='black', linewidth=0.5) axes.set_xlim(start_x, start_x + width) axes.xaxis.set_ticks_position('top') axes.xaxis.set_label_position('top') axes.set_ylim(start_y + height, start_y) pyplot.show() def draw0(data): # 设定网格尺寸 center_x, center_z = 100, 100 width, height = 50, 50 start_x, start_z = center_x - 25, center_z - 25 end_x, end_z = start_x + width, start_z + height # 创建一个空白色网格 colors = numpy.ones((height, width, 3)) # 3代表RGB # 示例:为某些区块上色,比如(0,0)为红色 (5,5)为绿色 # colors[4, 4] = [1, 0, 0] # 红色 # colors[5, 5] = [0, 1, 0] # 绿色 # colors[6, 6] = [0, 1, 1] # 蓝色 cell_size = 50 figsize = cell_size / 1.54 font_size = cell_size / 2 figure, axes = pyplot.subplots(figsize=(figsize, figsize)) pyplot.imshow(colors, extent=(0, width, 0, height), origin='lower') # 绘制网格线 for x in range(start_x, end_x): pyplot.axvline(x, color='black', linewidth=0.5) for y in range(start_z, end_z): pyplot.axhline(y, color='black', linewidth=0.5) # 在指定格子内加文字(如坐标或自定义信息) for item in data: print(item) axes.text(float(item.get('x')) + 0.5, float(item.get('z')) + 0.5, item.get('level'), ha='center', va='center', fontsize=font_size) axes.set_xlim(start_x, end_x) axes.xaxis.set_ticks_position('top') axes.xaxis.set_label_position('top') axes.set_ylim(end_z, start_z) pyplot.show() if __name__ == '__main__': http_init() # data = [ # { # 'x': 80, # 'z': 80, # 'level': 32, # }, # { # 'x': 90, # 'z': 90, # 'level': 32, # }, # ] # draw0(data)