我在绘制球体和曲线时遇到问题 [英] I have a problem with plotting sphere and a curve on it

查看:47
本文介绍了我在绘制球体和曲线时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在球体上绘制曲线,但无法同时绘制它们.我为曲线确定了欧几里得范数 10 的一些点,另外一些点则分别绘制了半径10 的球面./p>

曲线点:

  random_numbers = []basevalues = np.linspace(-0.9,0.9,100)对于范围内的我(len(basevalues)):t = random.random()random_numbers.append(t * 10)xvalues = [random_numbers [i] * np.cos(basevalues [i])for i in range(len(basevalues))]yvalues = [random_numbers [i] * np.sin(basevalues [i])for i in range(len(basevalues))]zvalues = [np.sqrt(100-xvalues [i] ** 2-yvalues [i] ** 2)for i in range(len(basevalues))] 

其中 xvalues yvalues zvalues 是我们的点欧几里得分量.

球形点

  u = np.linspace(0,2 * np.pi,100)v = np.linspace(0,np.pi,100)x = 10 * np.outer(np.cos(u),np.sin(v))y = 10 * np.outer(np.sin(u),np.sin(v))z = 10 * np.outer(np.ones(np.size(u)),np.cos(v)) 

其中 x,y z 是球点的欧几里得分量.

我的问题:

当我尝试绘制曲线而不绘制球体时,它可以工作.但是当我将它们绘制在一起时,它将返回球体.

整个代码如下:

 将matplotlib.pyplot导入为plt将numpy导入为np随机导入#曲线点random_numbers = []basevalues = np.linspace(-0.9,0.9,100)对于范围内的我(len(basevalues)):t = random.random()random_numbers.append(t * 10)xvalues = [random_numbers [i] * np.cos(basevalues [i])for i in range(len(basevalues))]yvalues = [random_numbers [i] * np.sin(basevalues [i])for i in range(len(basevalues))]zvalues = [np.sqrt(100-xvalues [i] ** 2-yvalues [i] ** 2)for i in range(len(basevalues))]#球点u = np.linspace(0,2 * np.pi,100)v = np.linspace(0,np.pi,100)x = 10 * np.outer(np.cos(u),np.sin(v))y = 10 * np.outer(np.sin(u),np.sin(v))z = 10 * np.outer(np.ones(np.size(u)),np.cos(v))#绘制表面和曲线无花果= plt.figure()ax = fig.add_subplot(111,projection ='3d')circ = ax.plot(xvalues,yvalues,zvalues,color ='green',linewidth = 1)sphere = ax.plot_surface(x,y,z,color ='r')ax.set_zlim(-10,10)plt.xlabel("X轴")plt.ylabel("Y轴")plt.show() 

我想发生的事情

我想在球体上绘制曲线,但是在我的代码中却没有发生.我感谢任何提示.

解决方案

使用球形坐标,您可以轻松地做到这一点:

  ##使用球坐标在球上绘制一个圆.将numpy导入为np导入matplotlib.pyplot作为plt#一个完整的领域R = 10theta = np.linspace(0,2 * np.pi,1000)phi = np.linspace(0,np.pi,1000)x_sphere = R * np.outer(np.cos(theta),np.sin(phi))y_sphere = R * np.outer(np.sin(theta),np.sin(phi))z_sphere = R * np.outer(np.ones(np.size(theta)),np.cos(phi))#球体上的完整圆x_circle = R * np.sin(θ)y_circle = R * np.cos(theta)#3D情节无花果= plt.figure()ax = fig.add_subplot(111,projection ='3d')ax.plot_surface(x_sphere,y_sphere,z_sphere,color ='blue',alpha = 0.2)ax.plot(x_circle,y_circle,0,color ='green')plt.show() 

I am trying to plot a curve on a sphere but I can not plot them at the same time. I identified some points with Euclidean norm 10 for my curve, and some other points to plot the sphere of radius 10, respectively as following.

Points for curve:

random_numbers=[]
basevalues=np.linspace(-0.9,0.9,100)
for i in range(len(basevalues)):
    t=random.random()
    random_numbers.append(t*10)
    
xvalues=[random_numbers[i]*np.cos(basevalues[i]) for i in range(len(basevalues))]
yvalues=[random_numbers[i]*np.sin(basevalues[i]) for i in range(len(basevalues))]
zvalues=[np.sqrt(100-xvalues[i]**2-yvalues[i]**2)for i in range(len(basevalues))]

Where xvalues, yvalues and zvalues are our points Euclidean components.

Points for sphere:

u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)
x = 10 * np.outer(np.cos(u), np.sin(v))
y = 10 * np.outer(np.sin(u), np.sin(v))
z = 10 * np.outer(np.ones(np.size(u)), np.cos(v))

Where x,y and z are Euclidean components of sphere points.

My problem:

When I try to plot the curve, without plotting sphere, it works. But when I plot them together, then it just return the sphere.

The whole code is the following:

import matplotlib.pyplot as plt
import numpy as np
import random


#Curve points

random_numbers=[]
basevalues=np.linspace(-0.9,0.9,100)
for i in range(len(basevalues)):
    t=random.random()
    random_numbers.append(t*10)
    
xvalues=[random_numbers[i]*np.cos(basevalues[i]) for i in range(len(basevalues))]
yvalues=[random_numbers[i]*np.sin(basevalues[i]) for i in range(len(basevalues))]
zvalues=[np.sqrt(100-xvalues[i]**2-yvalues[i]**2)for i in range(len(basevalues))]


# Sphere points

u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)
x = 10 * np.outer(np.cos(u), np.sin(v))
y = 10 * np.outer(np.sin(u), np.sin(v))
z = 10 * np.outer(np.ones(np.size(u)), np.cos(v))

# Plot the surface and curve 

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
circ = ax.plot(xvalues,yvalues,zvalues, color='green',linewidth=1)
sphere=ax.plot_surface(x, y, z, color='r')


ax.set_zlim(-10, 10)
plt.xlabel("X axes")
plt.ylabel("Y axes")
plt.show()

What I want to occur:

I would like to plot the curve on the sphere, but it dose not happen in my code. I appreciate any hint.

解决方案

Using spherical coordinates, you can easily do that:

## plot a circle on the sphere using spherical coordinate.
import numpy as np
import matplotlib.pyplot as plt

# a complete sphere
R = 10
theta = np.linspace(0, 2 * np.pi, 1000)
phi = np.linspace(0, np.pi, 1000)
x_sphere = R * np.outer(np.cos(theta), np.sin(phi))
y_sphere = R * np.outer(np.sin(theta), np.sin(phi))
z_sphere = R * np.outer(np.ones(np.size(theta)), np.cos(phi))

# a complete circle on the sphere
x_circle = R * np.sin(theta)
y_circle = R * np.cos(theta)

# 3d plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(x_sphere, y_sphere, z_sphere, color='blue', alpha=0.2)
ax.plot(x_circle, y_circle, 0, color='green')
plt.show()

这篇关于我在绘制球体和曲线时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆