python第二阶段(21)numpy入门基础-可视化之网格

numpy入门基础-可视化之网格

1、调出网格

matplotlib.pyplot.grid(b=None, which='major', axis='both', **kwargs)

b:bool,默认:无,可选。就是是否显示网格线。

  • 是否显示网格线。如果提供了任何kwarg,则假定您要打开网格并将b设置为True。
    如果b为None并且没有kwargs,则将切换线的可见性。

which:默认:major,{‘major’,‘minor’,‘both’}(可选)。要应用更改的网格线。

axis:默认:both,{‘both’,‘x’,‘y’},可选。想绘制哪个方向的网格线。

**kwargs:定义网格的线属性。(用的时候百度搜有效关键字)
如:grid(color=‘r’, linestyle=’-’, linewidth=2)

2、示例

in:

import numpy as np #导入 numpyas
import matplotlib.pyplot as plt #导入 matplotlib.pyplot
import math
x=np.arange(1,100)
s=np.sin(x)
fig=plt.figure()
axl=fig.add_subplot(121)
axl.plot(x,x**x)
plt.grid(b=True,axis='y',color='r',linestyle=':',linewidth=2)#开网格,红色线,虚线,线宽为2,只显示y方向

out:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的八叉树体素网格可视化Python 代码示例: ```python import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d.art3d import Poly3DCollection class Octree: def __init__(self, min_point, max_point, depth=0, max_depth=5): self.min_point = min_point self.max_point = max_point self.depth = depth self.max_depth = max_depth self.children = [None]*8 self.is_leaf = True self.points = [] def insert(self, point): if self.depth == self.max_depth: self.points.append(point) return idx = self.get_octant_idx(point) if self.children[idx] is None: self.children[idx] = self.create_octant(idx) self.is_leaf = False self.children[idx].insert(point) def create_octant(self, idx): x, y, z = self.get_octant_center(idx) min_point = np.array([x, y, z]) max_point = np.array([self.max_point[0] if idx & 4 else x, self.max_point[1] if idx & 2 else y, self.max_point[2] if idx & 1 else z]) return Octree(min_point, max_point, self.depth+1, self.max_depth) def get_octant_center(self, idx): x = (self.min_point[0] + self.max_point[0])/2.0 y = (self.min_point[1] + self.max_point[1])/2.0 z = (self.min_point[2] + self.max_point[2])/2.0 if idx & 4: x = (x + self.max_point[0])/2.0 else: x = (x + self.min_point[0])/2.0 if idx & 2: y = (y + self.max_point[1])/2.0 else: y = (y + self.min_point[1])/2.0 if idx & 1: z = (z + self.max_point[2])/2.0 else: z = (z + self.min_point[2])/2.0 return x, y, z def get_octant_idx(self, point): idx = 0 if point[0] > (self.min_point[0] + self.max_point[0])/2.0: idx |= 4 if point[1] > (self.min_point[1] + self.max_point[1])/2.0: idx |= 2 if point[2] > (self.min_point[2] + self.max_point[2])/2.0: idx |= 1 return idx def get_leaf_nodes(self): if self.is_leaf: return [self] leaf_nodes = [] for child in self.children: if child is not None: leaf_nodes.extend(child.get_leaf_nodes()) return leaf_nodes def plot_cube(cube_definition, ax): x = [cube_definition[0][0], cube_definition[1][0], cube_definition[2][0], cube_definition[3][0], cube_definition[0][0], cube_definition[4][0], cube_definition[5][0], cube_definition[1][0]] y = [cube_definition[0][1], cube_definition[1][1], cube_definition[2][1], cube_definition[3][1], cube_definition[0][1], cube_definition[4][1], cube_definition[5][1], cube_definition[1][1]] z = [cube_definition[0][2], cube_definition[1][2], cube_definition[2][2], cube_definition[3][2], cube_definition[0][2], cube_definition[4][2], cube_definition[5][2], cube_definition[1][2]] verts = [list(zip(x, y, z))] ax.add_collection3d(Poly3DCollection(verts, facecolors='blue', linewidths=1, edgecolors='r', alpha=.25)) def plot_octree(node, ax): if node.is_leaf: return for child in node.children: if child is not None: plot_cube([(child.min_point[0], child.min_point[1], child.min_point[2]), (child.max_point[0], child.min_point[1], child.min_point[2]), (child.max_point[0], child.max_point[1], child.min_point[2]), (child.min_point[0], child.max_point[1], child.min_point[2]), (child.min_point[0], child.min_point[1], child.max_point[2]), (child.max_point[0], child.min_point[1], child.max_point[2]), (child.max_point[0], child.max_point[1], child.max_point[2]), (child.min_point[0], child.max_point[1], child.max_point[2])], ax) plot_octree(child, ax) def main(): points = np.random.rand(100, 3) min_point = np.min(points, axis=0) max_point = np.max(points, axis=0) octree = Octree(min_point, max_point, max_depth=5) for point in points: octree.insert(point) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') plot_octree(octree, ax) ax.scatter(points[:,0], points[:,1], points[:,2], c='r', marker='o') ax.set_xlabel('X Label') ax.set_ylabel('Y Label') ax.set_zlabel('Z Label') plt.show() if __name__ == '__main__': main() ``` 这个代码使用 matplotlib 库绘制了八叉树体素网格,并在其中添加了随机生成的点。您可以根据需要调整点的数量和八叉树的深度。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值