python每输入一个坐标就在坐标系上画一个点的代码
时间: 2023-12-28 08:04:07 浏览: 26
可以使用matplotlib库在python中绘制坐标系和点。以下是一个例子:
```python
import matplotlib.pyplot as plt
# 坐标点
x = [1, 2, 3, 4]
y = [2, 4, 6, 8]
# 绘制坐标系和点
plt.plot(x, y, 'ro')
plt.axis([0, 5, 0, 10])
plt.show()
```
这段代码会绘制一个坐标系,其中x轴范围是0到5,y轴范围是0到10,然后在坐标点(1,2), (2,4), (3,6), (4,8)处绘制红色的点。
相关问题
写一个python代码,利用opengl在世界坐标系下渲染一个图像
好的,以下是一个简单的Python代码,它使用OpenGL在世界坐标系下渲染一个图像:
```python
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
# 定义顶点
vertices = ((-1, -1, 0), (1, -1, 0), (1, 1, 0), (-1, 1, 0))
# 定义纹理坐标
texcoords = ((0, 0), (1, 0), (1, 1), (0, 1))
# 定义索引
indices = ((0, 1, 2), (0, 2, 3))
# 定义纹理图像
image = [
[255, 0, 0, 255],
[ 0, 255, 0, 255],
[ 0, 0, 255, 255],
[255, 255, 255, 255],
]
def init():
glClearColor(0.0, 0.0, 0.0, 1.0)
glEnable(GL_DEPTH_TEST)
glEnable(GL_TEXTURE_2D)
def display():
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
# 设置投影矩阵
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(60.0, 1.0, 0.1, 100.0)
# 设置模型视图矩阵
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)
# 绑定纹理
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 4, 4, 0, GL_RGBA, GL_UNSIGNED_BYTE, image)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
# 开始绘制
glBegin(GL_TRIANGLES)
for i in range(len(indices)):
for j in range(len(indices[i])):
glTexCoord2fv(texcoords[indices[i][j]])
glVertex3fv(vertices[indices[i][j]])
glEnd()
glutSwapBuffers()
if __name__ == '__main__':
glutInit()
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
glutInitWindowSize(400, 400)
glutCreateWindow("OpenGL Window")
glutDisplayFunc(display)
init()
glutMainLoop()
```
这个代码中,我们创建了一个窗口,并在其中使用OpenGL绘制了一个带有纹理的正方形。我们使用顶点、纹理坐标和索引来定义几何形状,然后将纹理与之关联,最后用OpenGL来绘制形状。
用Python在一个坐标系上画两个关于x轴对称的正弦函数图
好的,我可以帮你完成这个任务。你可以使用Python的数学库`math`和绘图库`matplotlib`来实现。
以下是代码示例:
```python
import math
import matplotlib.pyplot as plt
# 创建x的取值范围
x = [i/10 for i in range(-70, 71)]
# 计算第一个正弦函数的y值
y1 = [math.sin(i) for i in x]
# 计算第二个正弦函数的y值(对称于x轴)
y2 = [-i for i in y1]
# 绘制图像
plt.plot(x, y1, color='blue', label='sin(x)')
plt.plot(x, y2, color='red', label='-sin(x)')
plt.legend(loc='upper right')
plt.xlabel('x')
plt.ylabel('y')
plt.title('Two Symmetric Sine Functions')
plt.show()
```
运行此代码,将会得到两个关于x轴对称的正弦函数的图像。