【问题标题】:using graphics library for 3D function将图形库用于 3D 功能
【发布时间】:2021-07-29 15:59:10
【问题描述】:


你好

我需要使用图形库来绘制函数
z=f(x,y)
什么时候 :
z=cos(r) * 0.1 ^ -r

r = sqrt (x^2 + y^2)

我试着写代码,出错了,有人知道为什么吗?

from graphics import *
from math import *
def D3_graph(x,y,z,d):
    win = GraphWin("3D_graph",1000, 1000)
    win.setBackground(color_rgb(0,0,0))
    point =Circle(Point(500,500),300)
    for x_a in range(-7,7):
        sqrt_cul = sqrt((x-x_a)**2 + y**2)
        z=cos(sqrt_cul)*d**-(sqrt_cul)
        win.plot(x_a,y+z,"Blue") 
        win.plot(x_a,-y+z,"Blue")
    c.draw(win)
    win.getMouse()
    win.close()
D3_graph(300,300,100,0.1)

【问题讨论】:

    标签: python math graphics draw


    【解决方案1】:

    我相信您的项目中使用了graphics.py(不链接到非原始文件)。

    可能有一些明显的问题

    • 当 x, y = 300; d = 0.1,那么 d**-r 将是 0.1^-424,大于 1e+309,因此浮点溢出。值的大小太大而无法绘制。
    • c.draw(win) 但变量 c 之前没有声明。

    其他问题是

    • 我猜 graphics.py 是一个 2D 绘图界面,不适用于 3D。而且它只是用于教学和演示,而不是用于复杂的绘图。
    • [-300, 300] 中 x、y 的范围太大。我认为 [-1.2, 1.2] 可能更好。

    我个人更喜欢 matplotlib 工作区。绘制此图的代码可能是(另见 matplotlib 3D 表面 tutorial

    import matplotlib.pyplot as plt
    import numpy as np
    
    x = np.linspace(-1.15, 1.15, 300)
    y = np.linspace(-1.15, 1.15, 300)
    x, y = np.meshgrid(x, y)
    r = np.sqrt(x**2 + y**2)
    z = np.cos(r) * 0.1**-r
    
    fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
    surf = ax.plot_surface(x, y, z)
    fig.show()
    

    结果图可能是

    【讨论】:

    猜你喜欢
    • 2014-10-12
    • 2011-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-06
    • 1970-01-01
    • 1970-01-01
    • 2020-11-09
    相关资源
    最近更新 更多