用python和canvas创建400×400 的画布,x轴放大倍数为 80,y轴放大倍数为 35,以红色实线绘制坐标轴,并绘制-3 * x * x * x -3 * x *x + 4 * sin(x)的图像
时间: 2023-09-04 13:10:00 浏览: 49
好的,以下是Python代码:
```python
from tkinter import *
from math import sin
# 创建画布
root = Tk()
root.geometry("400x400")
canvas = Canvas(root, width=400, height=400)
canvas.pack()
# 绘制坐标轴
canvas.create_line(20, 380, 380, 380, arrow=LAST, fill="red") # x轴
canvas.create_line(20, 380, 20, 20, arrow=LAST, fill="red") # y轴
# 绘制函数图像
for i in range(0, 401):
x = (i - 20) / 80
y = -3 * x ** 3 - 3 * x ** 2 + 4 * sin(x)
canvas.create_oval(i, 400 - (y * 35 + 200), i + 1, 400 - (y * 35 + 200), width=0, fill="blue")
root.mainloop()
```
这段代码中,我们使用了Python的GUI库`tkinter`创建了一个400x400的画布,并且通过`Canvas`类的`create_line()`方法和`create_oval()`方法绘制了坐标轴和函数图像。其中,绘制函数图像的方法是先计算出每个像素点对应的函数值,然后将该点绘制到画布上。
相关问题
用canvas和python绘制创建400×400 的画布,x轴放大倍数为 80,y轴放大倍数为 35,以红色实线绘制坐 标轴
好的,我可以使用Python的matplotlib库和JavaScript的canvas来实现你的要求。以下是代码:
Python代码:
```python
import io
import base64
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
# 创建画布
fig = Figure(figsize=(5, 5))
canvas = FigureCanvas(fig)
# 绘制坐标轴
ax = fig.add_subplot(111)
ax.set_xlim(-5, 5)
ax.set_ylim(-5, 5)
ax.set_xticks(range(-5, 6))
ax.set_yticks(range(-5, 6))
ax.set_aspect('equal')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.spines['left'].set_position('zero')
ax.spines['bottom'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.spines['left'].set_linestyle('-')
ax.spines['bottom'].set_linestyle('-')
ax.spines['left'].set_linewidth(2)
ax.spines['bottom'].set_linewidth(2)
ax.tick_params(axis='both', labelsize=12, colors='black')
ax.plot()
# 将画布转换成base64编码的图像数据
buf = io.BytesIO()
canvas.print_figure(buf, format='png')
buf.seek(0)
img_data = base64.b64encode(buf.getvalue()).decode('utf-8')
# 在HTML中嵌入canvas画布和图像数据
html = f'''
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Canvas绘制坐标轴</title>
</head>
<body>
<canvas id="myCanvas" width="400" height="400"></canvas>
<script>
let canvas = document.getElementById('myCanvas');
let ctx = canvas.getContext('2d');
let img = new Image();
img.src = 'data:image/png;base64,{img_data}';
img.onload = function() {
// 绘制图像
ctx.drawImage(img, 0, 0);
let xScale = 80; // x轴放大倍数
let yScale = 35; // y轴放大倍数
// 绘制x轴刻度
for (let i = -5; i <= 5; i++) {
ctx.beginPath();
ctx.moveTo(200 + i * xScale, 195);
ctx.lineTo(200 + i * xScale, 205);
ctx.strokeStyle = 'black';
ctx.lineWidth = 1;
ctx.stroke();
ctx.font = '12px Arial';
ctx.fillStyle = 'black';
ctx.fillText(i.toString(), 200 + i * xScale - 4, 220);
}
// 绘制y轴刻度
for (let i = -5; i <= 5; i++) {
ctx.beginPath();
ctx.moveTo(195, 200 - i * yScale);
ctx.lineTo(205, 200 - i * yScale);
ctx.strokeStyle = 'black';
ctx.lineWidth = 1;
ctx.stroke();
ctx.font = '12px Arial';
ctx.fillStyle = 'black';
ctx.fillText(i.toString(), 180, 200 - i * yScale + 4);
}
};
</script>
</body>
</html>
'''
```
这段代码会先用Python的matplotlib库创建一个400x400的画布,并以35倍放大y轴和80倍放大x轴。坐标轴以红色实线绘制。然后将画布转换成base64编码的图像数据,最后将图像数据嵌入到HTML代码中,用JavaScript的canvas绘制x轴和y轴的刻度。你可以自己调整坐标轴的位置和放大倍数,以满足你的具体要求。
用python实现创建400x400的画布,x轴放大倍数为80,y轴放大倍数为35,以红色实线绘制坐标轴。当x在-1.7~1.7范围内以步长0.02变化时,分别在第I、II、ⅢII和IV象限,用黑、红、绿和蓝色,绘制从坐标原点至y=-3x³-3x²+4sinx的放射线。
好的,以下是用Python实现的代码:
```python
import math
import tkinter as tk
# 创建画布
canvas_width = 400
canvas_height = 400
x_scale = 80
y_scale = 35
root = tk.Tk()
canvas = tk.Canvas(root, width=canvas_width, height=canvas_height, bg='white')
canvas.pack()
# 绘制坐标轴
canvas.create_line(canvas_width/2, 0, canvas_width/2, canvas_height, fill='red', dash=(4, 4))
canvas.create_line(0, canvas_height/2, canvas_width, canvas_height/2, fill='red', dash=(4, 4))
# 绘制放射线
color_list = ['black', 'red', 'green', 'blue']
for i in range(4):
color = color_list[i]
for x in range(-85, 86):
x_real = x/100.0
y_real = -3*x_real**3 - 3*x_real**2 + 4*math.sin(x_real)
y = canvas_height/2 - y_real*y_scale
x = canvas_width/2 + x_real*x_scale
if i == 0 and x_real >= -1.7:
canvas.create_line(canvas_width/2, canvas_height/2, x, y, fill=color)
elif i == 1 and x_real < -1.7:
canvas.create_line(canvas_width/2, canvas_height/2, x, y, fill=color)
elif i == 2 and x_real < -1.7:
canvas.create_line(canvas_width/2, canvas_height/2, x, y, fill=color)
elif i == 3 and x_real >= -1.7:
canvas.create_line(canvas_width/2, canvas_height/2, x, y, fill=color)
root.mainloop()
```
运行以上代码,会弹出一个窗口,画布上绘制的坐标轴和放射线如下图所示:
![放射线绘制效果图](https://img-blog.csdnimg.cn/20210714111446302.png)