
正文
python打印函数曲线,python如何画函数曲线
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
python中print函数的用法
print()函数用于打印输出,是python中最常见的一个内置函数。
print()函数的语法如下:
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)。
将"objects"打印输出至"file参数"指定的文本流,以"sep参数"分隔开并在末尾加上"end参数"。"sep"、"end "、"file"和"flush"必须以关键字参数的形式给出。flush关键字参数是在phthon3.3版后增加的。
所有非关键字参数都会被转换为字符串,就像是执行了str()一样,并会被写入到流,以“sep参数“且在末尾加上“end参数“。“sep参数“和“end参数“都必须为字符串;它们也可以为“None“,这意味着使用默认值。如果没有给出“objects参数“,则print()将只写入“end参数“。
ython print()函数:
print()方法用于打印输出,最常见的一个函数。
在Python3.3版增加了flush关键字参数。
print在Python3.x是一个函数,但在Python2.x版本不是一个函数,只是一个关键字。
相关问答
Q1: Python输出函数print()总结
print() 方法用于打印输出,是python中最常见的一个函数。
该函数的语法如下:
参数的具体含义如下:
objects --表示输出的对象。输出多个对象时,需要用 , (逗号)分隔。
sep -- 用来间隔多个对象。
end -- 用来设定以什么结尾。默认值是换行符 \n,我们可以换成其他字符。
file -- 要写入的文件对象。
无论什么类型的数据,包括但不局限于:数值型,布尔型,列表变量,字典变量...都可以直接输出。
在C语言中,我们可以使用printf("%-.4f",a)之类的形式,实现数据的的格式化输出。
在python中,我们同样可以实现数据的格式化输出。我们可以先看一个简单的例子:
和C语言的区别在于,Python中格式控制符和转换说明符用%分隔,C语言中用逗号。
接下来我们仔细探讨一下格式化输出
(1).%字符:标记转换说明符的开始。
%字符的用法可参考上例,不再赘述。
最小字段宽度:转换后的字符串至少应该具有该值指定的宽度。如果是*(星号),则宽度会从值元组中读出。
点(.)后跟精度值:如果需要输出实数,精度值表示出现在小数点后的位数。如果需要输出字符串,那么该数字就表示最大字段宽度。如果是*,那么精度将从元组中读出。
可参考C语言的实现方式。
注:字段宽度中,小数点也占一位。
转换标志:-表示左对齐;+表示在数值前要加上正负号;" "(空白字符)表示正数之前保留空格();0表示转换值若位数不够则用0填充。
具体的我们可以看一下例子:
格式字符 说明 格式字符 说明
%s 字符串采用str()的显示 %x 十六进制整数
%r 字符串(repr())的显示 %e 指数(基底写e)
%c 单个字符 %E 指数(基底写E)
%b 二进制整数 %f,%F 浮点数
%d 十进制整数 %g 指数(e)或浮点数(根据显示长度)
%i 十进制整数 %G 指数(E)或浮点数(根据显示长度)
%o 八进制整数 %% 字符%
在python中,输出函数总是默认换行,比如说:
而显然,这种输出太占“空间”,我们可以进行如下改造:
参考文本第一部分对end参数的描述:end -- 用来设定以什么结尾。默认值是换行符 \n,我们可以换成其他字符。
Q2: 如何用python完成:用自顶向下设计方法编写程序:在屏幕上打印三角函数y = sin(x)的图像。
I wrote this in Tkinter for you, in case you don't know Tkinter, it is a built-in module for most python versions.
If you want a commandline version, you can ask me, but tell you what, since those values are all
float numbers, so it's hard to get a precise graph in commandline window.
Well, in this version, I enlarged each element's position by 40 and then change them to integer, guess this is an endurable loss of precision.
#
from math import radians
from math import sin
from Tkinter import *
pos = []
xPos = 0
centerX = 0
centerY = 0
for deg in range(-360, 361, 10):
pos.append([xPos, int(40*(sin(radians(deg))))]) #1000 too big for my screen
xPos+=1
if deg == 0:
centerX = xPos-1
centerY = pos[-1][1]
root = Tk()
root.title('trianble graph from -180 to 180')
width, height = 550, 450
mHei = height/2
mWid = width/2
canvas = Canvas(root, width=width, height=height)
canvas.create_line(0, mHei, width, mHei) #x axis
canvas.create_line(mWid, 0, mWid, height) #y axis
xStep = (width-150)/len(pos)
yStep = (height-150)/len(pos)
radius = 3
# the middle point (sin(0) is first drawn and used as position reference for all
canvas.create_oval(mWid-radius, mHei-radius, mWid+radius, mHei+radius, fill='green')
print pos
print xStep, yStep, centerX, centerY
#exit(0)
for i in pos:
if i[0] == centerX: #center processed already.
continue
x = mWid + xStep*(i[0]-centerX)
# y is smaller, the bigger the value, so use minus
y = mHei - yStep*(i[1]-centerY)
canvas.create_oval(x-radius, y-radius, x+radius, y+radius, fill='green')
canvas.pack()
root.mainloop()
Q3: Python如何画函数的曲线
输入以下代码导入我们用到的函数库。
import numpy as np
import matplotlib.pyplot as plt
x=np.arange(0,5,0.1);
y=np.sin(x);
plt.plot(x,y)
采用刚才代码后有可能无法显示下图,然后在输入以下代码就可以了:
plt.show()
Q4: 用Python或MATLAB如何画三元平方和函数曲线???
Python代码 import numpy as npimport matplotlib.pyplot as pltfrom mpl_toolkits.mplot3d import Axes3Dfig = plt.figure()ax = Axes3D(fig)X = np.arange(-4, 4, 0.25)Y = np.arange(-4, 4, 0.25)X, Y = np.meshgrid(X, Y)R = np.sqrt(X**2 + Y**2)Z = np.sin(R)ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='hot')plt.show() 运行结果:







