简体   繁体   English

使用 3D function 的图形库

[英]using graphics library for 3D function


hello你好

I need to use graphics library to draw the function我需要使用图形库来绘制 function
z=f(x,y) z=f(x,y)
when:什么时候:
z=cos(r) * 0.1 ^ -r z=cos(r) * 0.1 ^ -r
and
r = sqrt ( x^2 + y^2) r = sqrt ( x^2 + y^2)

I tried to write the code and got an error,Anyone know why?我尝试编写代码并得到一个错误,有人知道为什么吗?

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)

I believe that graphics.py (does not link to not the original file) is used in your project.我相信您的项目中使用了graphics.py (不链接到非原始文件)。

Some evident problems maybe一些明显的问题可能

  • When x, y = 300;当x,y = 300; d = 0.1, then d**-r will be 0.1^-424, which is larger than 1e+309 and thus float overflows. d = 0.1,则 d**-r 将是 0.1^-424,大于 1e+309,因此浮点溢出。 the magnitude of value is too large to be plotted.值的大小太大而无法绘制。
  • c.draw(win) but variable c is not declared before. c.draw(win)但变量c之前未声明。

Other problems are其他问题是

  • I guess graphics.py is a 2D plot interface, not for 3D.我猜 graphics.py 是一个 2D plot 接口,不适用于 3D。 And it is only usage is for teaching and demonstrating, not for complicated plotting.它只是用于教学和演示,而不是用于复杂的绘图。
  • the range of x, y in [-300, 300] is too large. [-300, 300] 中 x, y 的范围太大。 I think [-1.2, 1.2] is perhaps better.我认为 [-1.2, 1.2] 可能更好。

I personally prefer a matplotlib workround.我个人更喜欢 matplotlib 工作区。 Code to plot this figure could be (also see matplotlib 3D surface tutorial )代码到 plot 这个图可以是(另见 matplotlib 3D 表面教程

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()

The result figure could be结果图可能是示例图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM