mirror of
https://github.com/Estom/notes.git
synced 2026-04-05 03:48:56 +08:00
matplotlib & pandas
This commit is contained in:
55
Python/matplotlab/gallery/mplot3d/voxels_numpy_logo.md
Normal file
55
Python/matplotlab/gallery/mplot3d/voxels_numpy_logo.md
Normal file
@@ -0,0 +1,55 @@
|
||||
# 三维体素绘制Numpy的Logo
|
||||
|
||||
演示使用坐标不均匀的ax.voxels
|
||||
|
||||

|
||||
|
||||
```python
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
|
||||
# This import registers the 3D projection, but is otherwise unused.
|
||||
from mpl_toolkits.mplot3d import Axes3D # noqa: F401 unused import
|
||||
|
||||
|
||||
def explode(data):
|
||||
size = np.array(data.shape)*2
|
||||
data_e = np.zeros(size - 1, dtype=data.dtype)
|
||||
data_e[::2, ::2, ::2] = data
|
||||
return data_e
|
||||
|
||||
# build up the numpy logo
|
||||
n_voxels = np.zeros((4, 3, 4), dtype=bool)
|
||||
n_voxels[0, 0, :] = True
|
||||
n_voxels[-1, 0, :] = True
|
||||
n_voxels[1, 0, 2] = True
|
||||
n_voxels[2, 0, 1] = True
|
||||
facecolors = np.where(n_voxels, '#FFD65DC0', '#7A88CCC0')
|
||||
edgecolors = np.where(n_voxels, '#BFAB6E', '#7D84A6')
|
||||
filled = np.ones(n_voxels.shape)
|
||||
|
||||
# upscale the above voxel image, leaving gaps
|
||||
filled_2 = explode(filled)
|
||||
fcolors_2 = explode(facecolors)
|
||||
ecolors_2 = explode(edgecolors)
|
||||
|
||||
# Shrink the gaps
|
||||
x, y, z = np.indices(np.array(filled_2.shape) + 1).astype(float) // 2
|
||||
x[0::2, :, :] += 0.05
|
||||
y[:, 0::2, :] += 0.05
|
||||
z[:, :, 0::2] += 0.05
|
||||
x[1::2, :, :] += 0.95
|
||||
y[:, 1::2, :] += 0.95
|
||||
z[:, :, 1::2] += 0.95
|
||||
|
||||
fig = plt.figure()
|
||||
ax = fig.gca(projection='3d')
|
||||
ax.voxels(x, y, z, filled_2, facecolors=fcolors_2, edgecolors=ecolors_2)
|
||||
|
||||
plt.show()
|
||||
```
|
||||
|
||||
## 下载这个示例
|
||||
|
||||
- [下载python源码: voxels_numpy_logo.py](https://matplotlib.org/_downloads/voxels_numpy_logo.py)
|
||||
- [下载Jupyter notebook: voxels_numpy_logo.ipynb](https://matplotlib.org/_downloads/voxels_numpy_logo.ipynb)
|
||||
Reference in New Issue
Block a user