Python 复平面绘图实例

yipeiwu_com6年前Python基础

前言

在学校太闲,就写了这个程序,可以在复平面绘制曲线,画圆什么的很轻松,f(z) = e^(1j * z),螺旋线,函数图象等都可以。

效果图

说明

此程序使用turtle绘图,可以用作画函数图像,假设你想画函数g(x)的图像,那么就输入f(z) = z + 1j * g(z),g(z)是含z的表达式。

表达式可支持Python math库,random库,time库的所有函数,具体使用方法请自行探索。

代码

# Python 2.x

import turtle
from random import *
from math import *
from time import *

def printf(f):
 global z
 turtle.goto(f.real * 20, f.imag * 20)
 print("f(" + str(z) + ") = " + str(f))
 z += d

def format(s):
 s = s.replace("^","**")
 return s

def evale(s):
 n = eval(s)
 return n

delay = input("delay = ") * 1e-3
turtle.setup(1280, 720)
turtle.speed(10)
turtle.pensize(2)
turtle.goto(-640, 0)
turtle.goto(640, 0)
turtle.goto(0, 0)
turtle.goto(0, 360)
turtle.goto(0, -360)
turtle.goto(0, 0)

while True:
 express = format(raw_input("f(z) = "))
 d = input("d = ")
 min = input("min = ")
 max = input("max = ")
 z = min
 f = evale(express)
 turtle.pencolor(random(), random(), random())
 turtle.penup()
 printf(f)
 turtle.pendown()

 while z < max:
 f = evale(express)
 printf(f)

注意

这个程序使用Python 2.x,若需要Python 3.x的程序,请自行修改。

以上这篇Python 复平面绘图实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python3分析处理声音数据的例子

将音频文件拷贝到程序所在目录即可。 如下所示: #!/usr/bin/env python # encoding: utf-8 """ @Company:华中科技大学电气学院聚变与等...

python利用lxml读写xml格式的文件

之前在转换数据集格式的时候需要将json转换到xml文件,用lxml包进行操作非常方便。 1. 写xml文件 a) 用etree和objectify from lxml import...

pyqt4教程之实现windows窗口小示例分享

复制代码 代码如下:import sysfrom PyQt4 import QtGui, QtCoreclass Window( QtGui.QMainWindow): &nb...

Python version 2.7 required, which was not found in the registry

Python version 2.7 required, which was not found in the registry

安装PIL库的时候,直接提示:Python version 2.7 required, which was not found in the registry。 如图: 大意是说找不到...

python学习之编写查询ip程序

python学习之编写查询ip程序

公司服务器上的ip最少的也有100多个,有时候查到一个站的Ip, 不想通过OA去查,自己就用自己最近学的python知识,结合数据库,编写了一python小程序。实现只要输入主ip就能查...