鱼C论坛

 找回密码
 立即注册
查看: 1370|回复: 4

[已解决]求助

[复制链接]
发表于 2020-8-8 17:51:09 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
老铁们,写了一个画图的程序,老是提示我 'Y' is not defined,能帮我看看是哪一步做错了吗?
#引入第三方库
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

#建立三维数据
θ=np.arange(0,np.pi,0.1)
φ=np.arange(0,2*np.pi,0.1)
θ,φ = np.meshgrid(θ,φ)

#球函数字典
dic1={
        0:np.sin(φ),
        1:(np.cos(θ))*(np.sin(φ)),
        2:((3*(np.cos(2*θ))+1)/4)*(np.sin(φ))
        }
dic2={
        1:(np.cos(θ))*(np.sin(φ)),
        2:3*(np.sin(θ))*(np.cos(θ))*(np.sin(φ)),
        4:3*(np.sin(θ))*(np.sin(θ))*(np.sin(φ))
        }

#定义三维坐标轴
fig=plt.figure()
ax=Axes3D(fig)

def main():
        l=input('请输入阶数l:')
        m=input('请输入m:')
        x = int(l)
        y = int(m)
        t=x*y
        Y= sp_main(t)


#球函数的阶数
def sp_main(t):
        if t==0:
                Y=dic1.get(t)
                Y= np.maximum(Y, 0)
               
        else:
                Y=dic2.get(t)
                Y= np.maximum(Y, 0)
        return Y       




#作图
plt.rcParams['font.sans-serif']=['SimHei']
ax.set_xlabel('仰角θ')
ax.set_ylabel('方位角φ')
ax.set_zlabel('球函数Y')

ax.set_xlim(0,np.pi)
ax.set_ylim(0,2*np.pi)
ax.set_zlim(0,1)
ax.plot_surface(θ, φ, Y)

plt.show()

if __name__ == "__main__":
    main()
最佳答案
2020-8-8 18:27:45
snowJR 发表于 2020-8-8 18:19
这个为什么没有图像出来?

你写的代码,问我咯?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-8-8 17:53:54 | 显示全部楼层
ax.plot_surface(θ, φ, Y)
这一行的Y的确没有被定义过

在球函数的阶数中出现过 Y,但是那是局部变量。
此外就没有见过Y的定义了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-8 18:00:03 | 显示全部楼层
  1. #引入第三方库
  2. import matplotlib.pyplot as plt
  3. from mpl_toolkits.mplot3d import Axes3D
  4. import numpy as np

  5. #建立三维数据
  6. θ=np.arange(0,np.pi,0.1)
  7. φ=np.arange(0,2*np.pi,0.1)
  8. θ,φ = np.meshgrid(θ,φ)


  9. #球函数字典
  10. dic1={
  11.     0:np.sin(φ),
  12.     1:(np.cos(θ))*(np.sin(φ)),
  13.     2:((3*(np.cos(2*θ))+1)/4)*(np.sin(φ))
  14. }


  15. dic2={
  16.     1:(np.cos(θ))*(np.sin(φ)),
  17.     2:3*(np.sin(θ))*(np.cos(θ))*(np.sin(φ)),
  18.     4:3*(np.sin(θ))*(np.sin(θ))*(np.sin(φ))
  19. }


  20. #定义三维坐标轴
  21. fig=plt.figure()
  22. ax=Axes3D(fig)


  23. #球函数的阶数
  24. def sp_main(t):
  25.     if t==0:
  26.         Y=dic1.get(t)
  27.         Y= np.maximum(Y, 0)
  28.            
  29.     else:
  30.         Y=dic2.get(t)
  31.         Y= np.maximum(Y, 0)
  32.     return Y      



  33. def main():
  34.     l=input('请输入阶数l:')
  35.     m=input('请输入m:')
  36.     x = int(l)
  37.     y = int(m)
  38.     t=x*y
  39.     Y= sp_main(t)
  40.     #作图
  41.     plt.rcParams['font.sans-serif']=['SimHei']
  42.     ax.set_xlabel('仰角θ')
  43.     ax.set_ylabel('方位角φ')
  44.     ax.set_zlabel('球函数Y')

  45.     ax.set_xlim(0,np.pi)
  46.     ax.set_ylim(0,2*np.pi)
  47.     ax.set_zlim(0,1)
  48.     ax.plot_surface(θ, φ, Y)

  49.     plt.show()

  50. if __name__ == "__main__":
  51.     main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-8-8 18:19:27 | 显示全部楼层

这个为什么没有图像出来?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-8 18:27:45 | 显示全部楼层    本楼为最佳答案   
snowJR 发表于 2020-8-8 18:19
这个为什么没有图像出来?

你写的代码,问我咯?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-25 03:52

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表