现在有n*3的numpy数组储存n个数据的三维坐标,和长度n的一维数组储存对应的应力,筛选出Z轴坐标介于a与b之间的点,用python画出这些坐标对应的应力三维图,应力大小用rainbow图区分,同时保证xyz轴的比例尺是一样的,图美观
时间: 2024-02-25 17:57:00 浏览: 79
好的,针对您的问题,我来给出一个Python的代码实现:
首先,我们需要导入一些库,包括numpy、matplotlib等:
```python
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
```
接着,我们可以生成一个n*3的随机numpy数组,和一个长度为n的一维数组,来模拟您的数据:
```python
n = 100
coords = np.random.rand(n, 3)
stress = np.random.rand(n)
```
接下来,我们可以根据Z轴坐标的范围a和b,筛选出符合条件的点的索引:
```python
a, b = 0.3, 0.7
idx = np.where((coords[:, 2] > a) & (coords[:, 2] < b))[0]
```
然后,我们可以根据这些索引,从coords和stress中获取符合条件的数据:
```python
coords_filt = coords[idx, :]
stress_filt = stress[idx]
```
接下来,我们可以根据stress_filt的大小,来生成一个颜色映射(cmap):
```python
cmap = cm.get_cmap('rainbow')
colors = cmap(stress_filt)
```
然后,我们可以绘制三维图形:
```python
fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(111, projection='3d')
ax.scatter(coords_filt[:, 0], coords_filt[:, 1], coords_filt[:, 2], c=colors)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_xlim([0, 1])
ax.set_ylim([0, 1])
ax.set_zlim([0, 1])
ax.set_box_aspect([1, 1, 1])
plt.show()
```
这样,我们就完成了绘图,其中,figsize参数可以调整图形的大小,projection参数指定了绘制三维图形,c参数指定了每个点的颜色,set_xlabel等方法可以设置坐标轴的标签,set_xlim等方法可以设置坐标轴的范围,set_box_aspect方法可以保证xyz轴比例尺相同。
完整代码如下:
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)