如何将极坐标绘制的球放大到不同的半径? Python

huangapple go评论123阅读模式
英文:

how to scale a polar plotted sphere up to a different radius? python

问题

下面是翻译后的代码部分:

# 原始代码
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
r = 0.05
u, v = np.mgrid[0:2 * np.pi:30j, 0:np.pi:20j]
x = np.cos(u) * np.sin(v)
y = np.sin(u) * np.sin(v)
z = np.cos(v)
ax.plot_surface(x, y, z, cmap=plt.cm.YlGnBu_r)
plt.show()
# 无论我将r更改为什么值,绘图总是0.5乘0.5的单位。我希望它的半径为50。

# 第一个球体图片:[first sphere](https://i.stack.imgur.com/fSvZI.png)

# 然后我尝试了这个,它返回了我想要的大小的球体,但我不确定z坐标是否以相同的比例缩放,因为它看起来不太球形。
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams["figure.figsize"] = [10.00, 40.50]
plt.rcParams["figure.autolayout"] = False
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
ax.axes.set_xlim3d(left=-100, right=100)
ax.axes.set_ylim3d(bottom=-100, top=100)
ax.axes.set_zlim3d(bottom=-100, top=100)

r = 50
u, v = np.mgrid[0:2 * np.pi:300j, 0:np.pi:300j]
x = 100 * (np.cos(u) * np.sin(v))
y = 100 * (np.sin(u) * np.sin(v))
z = 100 * (np.cos(v))
ax.plot_surface(x, y, z, rstride=5, cstride=5, cmap=plt.cm.YlGnBu_r)
plt.show()
# 第二个球体图片:[second sphere](https://i.stack.imgur.com/qOGaX.png)

# 我尝试添加更多绘制点并将它们缩放100倍。不确定是不是球体或其他形状。

希望这对你有所帮助。如果需要进一步的翻译或解释,请随时提问。

英文:

So i need to scale up the size of a sphere i plotted with polar coordinates, but I am unsure if Im doing it correctly in a way that scales properly.

#og code
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
r = 0.05
u, v = np.mgrid[0:2 * np.pi:30j, 0:np.pi:20j]
x = np.cos(u) * np.sin(v)
y = np.sin(u) * np.sin(v)
z = np.cos(v)
ax.plot_surface(x, y, z, cmap=plt.cm.YlGnBu_r)
plt.show()
#no matter what I change r to its always .5 by .5 units on a plot. I want it to be a radius of 50. 


first sphere
I then tried this and it returns a sphere the size I want, but I am unsure if the z coordinate is scaling the same rate as the rest because it doesn't look as spherical.

import matplotlib.pyplot as plt
import numpy as np
plt.rcParams["figure.figsize"] = [10.00, 40.50]
plt.rcParams["figure.autolayout"] = False
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
ax.axes.set_xlim3d(left=-100, right=100) 
ax.axes.set_ylim3d(bottom=-100, top=100) 
ax.axes.set_zlim3d(bottom=-100, top=100) 
    
r = 50
u, v = np.mgrid[0:2 * np.pi:300j, 0:np.pi:300j]
x = 100*(np.cos(u) * np.sin(v))
y = 100*(np.sin(u) * np.sin(v))
z = 100*(np.cos(v))
ax.plot_surface(x, y, z, rstride= 5, cstride = 5, cmap=plt.cm.YlGnBu_r)
plt.show()```
[second sphere](https://i.stack.imgur.com/qOGaX.png)


I tried to just add more points to plot and scaling them by multiplying by 100. not sure if spherical or tripping. 

</details>


# 答案1
**得分**: 2

使用 `ax.set_box_aspect()` 来确保所有的坐标轴按相同的比例缩放(沿着X、Y和Z轴的纵横比)。[查看相关文档](https://matplotlib.org/stable/api/_as-gen/matplotlib.axes.Axes.set_box_aspect.html)

您可以通过传递硬编码的值或通过使用 `np.ptp()` 函数动态获取 `x`、`y` 和 `z` 值的总范围来实现这一点。

```python
# 硬编码的值可以是 (200, 200, 200)
ax.set_box_aspect((np.ptp(x), np.ptp(y), np.ptp(z))

ax.plot_surface(x, y, z, rstride= 5, cstride = 5, cmap=plt.cm.YlGnBu_r)
plt.show()

这应该为您提供以下图形

如何将极坐标绘制的球放大到不同的半径? Python

关于半径,您没有使用您定义的 r 变量。使用 r 缩放 xyz 应该有效:

# 保持与前面图形相同的盒子纵横比,以演示缩放效果
ax.set_box_aspect((200, 200, 200))

x = r*(np.cos(u) * np.sin(v))
y = r*(np.sin(u) * np.sin(v))
z = r*(np.cos(v))

在这里,r=50 将生成以下图形:

如何将极坐标绘制的球放大到不同的半径? Python

英文:

Use the ax.set_box_aspect() to make sure all the axes are scaling at the same rate (aspect ratio along X, Y, and Z axis). See the corresponding doc

You can either pass hard-coded values or do it dynamically by retrieving the total range of x, y, and z values with the np.ptp() function.

[...]

# Hard coded values would be (200, 200, 200)
ax.set_box_aspect((np.ptp(x), np.ptp(y), np.ptp(z))

ax.plot_surface(x, y, z, rstride= 5, cstride = 5, cmap=plt.cm.YlGnBu_r)
plt.show()

This should give you the following figure
如何将极坐标绘制的球放大到不同的半径? Python


Regarding the radius, you're not using the r variable you define. Scalling x, y and z with r should work:

# Keep same box aspect as previous figure to demonstrate the scaling effect
ax.set_box_aspect((200, 200, 200))  

x = r*(np.cos(u) * np.sin(v))
y = r*(np.sin(u) * np.sin(v))
z = r*(np.cos(v))

Here, r=50 will generate the following figure:

如何将极坐标绘制的球放大到不同的半径? Python

huangapple
  • 本文由 发表于 2023年3月4日 09:42:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75633167.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定