Python使用turtle绘制垂直条形统计图
时间: 2024-04-22 09:19:17 浏览: 25
使用Python的turtle库可以很方便地绘制垂直条形统计图。下面是一个简单的示例代码:
```python
import turtle
def draw_bar(height):
turtle.begin_fill()
turtle.left(90)
turtle.forward(height)
turtle.write(str(height))
turtle.right(90)
turtle.forward(40)
turtle.right(90)
turtle.forward(height)
turtle.left(90)
turtle.end_fill()
data = [120, 56, 309, 220, 156] # 统计数据
turtle.color("blue")
turtle.penup()
turtle.goto(-200, -200)
turtle.pendown()
for height in data:
draw_bar(height)
turtle.done()
```
这段代码首先导入了turtle库,然后定义了一个`draw_bar`函数,用于绘制每个条形的高度。接下来,定义了一个数据列表`data`,其中包含了每个条形的高度。然后,使用turtle库的函数来绘制每个条形,并在条形上方标注对应的高度。最后,调用`turtle.done()`函数来显示绘制结果。
相关问题
Python使用turtle绘制各种语言垂直条形统计图
Python的turtle库是一个绘图库,可以通过简单的命令来绘制各种图形,包括垂直条形统计图。下面是使用turtle库绘制垂直条形统计图的步骤:
1. 导入turtle库:
```python
import turtle
```
2. 创建一个画布和画笔:
```python
window = turtle.Screen()
pen = turtle.Turtle()
```
3. 设置画笔的颜色和粗细:
```python
pen.color("blue")
pen.pensize(3)
```
4. 定义一个函数来绘制一个条形:
```python
def draw_bar(height):
pen.begin_fill()
pen.left(90)
pen.forward(height)
pen.write(str(height))
pen.right(90)
pen.forward(40)
pen.right(90)
pen.forward(height)
pen.left(90)
pen.end_fill()
pen.forward(10)
```
5. 定义一个列表来存储每个条形的高度:
```python
data = [100, 200, 150, 300, 250]
```
6. 循环遍历列表,调用绘制条形的函数:
```python
for height in data:
draw_bar(height)
```
7. 隐藏画笔,并保持窗口不关闭:
```python
pen.hideturtle()
turtle.done()
```
这样就可以使用turtle库绘制垂直条形统计图了。你可以根据需要修改数据和样式来绘制不同的图形。
python使用turtle绘制sinx图形
可以使用 Python 自带的 `turtle` 模块来绘制 `sin(x)` 的图形。以下是一段示例代码,用于绘制一个 `sin(x)` 的图形:
```python
import turtle
import math
# 设置画笔
pen = turtle.Turtle()
pen.penup()
pen.goto(-300, 0)
pen.pendown()
# 设置x轴
pen.forward(600)
pen.right(180)
pen.forward(600)
pen.right(180)
# 设置y轴
pen.left(90)
pen.forward(200)
pen.right(180)
pen.forward(400)
pen.right(180)
# 绘制sin(x)曲线
pen.pencolor("red")
pen.pensize(2)
for x in range(-300, 301):
y = math.sin(x / 50) * 100
pen.goto(x, y)
# 关闭画笔
pen.hideturtle()
turtle.done()
```
这段代码首先设置了画笔的起始位置,并绘制了一个坐标系。然后使用 `math.sin()` 函数计算每个 `x` 坐标对应的 `y` 坐标,最后使用 `pen.goto()` 函数移动画笔并绘制曲线。最后隐藏画笔并显示绘制结果。
执行该代码,可以得到如下图形:
![sinx](https://img-blog.csdnimg.cn/20210716092924798.png)
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)