创作

新增鲜出炉的 Python 和 Turtle 支持

203   |   最后修改于  2019-09-12 06:49:49

Turtle



  1. Turtle 使用单独的 mEditor 组件实现。与jupyter不同:同一页面内的多个Turtle组件之间,**没有**任何上下文关联性。
  2. 不支持 turtle 中的 screen 相关语句(应该并不影响教学)。
  3. 支持在 turtle 中使用 input 和 print,但是在绘图中输出会影响显示速度。
  4. 只支持 Python 标准包,不支持在 turtle 中使用 Python 扩展包。
  5. 已知bug: abs(turtle.pos) 不支持,可以用为x和y的平方和替换。

import math
import turtle as tl

radius = 120
tl.color('red', 'yellow')

# 让图画位于中心
tl.up()
tl.backward(radius)
tl.down()

tl.begin_fill()
while True:
    tl.forward(radius * 2)
    tl.left(170)

    # 回到起点
    if math.sqrt((tl.pos()[0] + radius) ** 2 + tl.pos()[1] ** 2) < 1:
        break
tl.end_fill()
tl.done()

Matplotlib

%matplotlib inline

import matplotlib.pyplot as plt
import numpy as np

# plt.rcParams['font.sans-serif'] = ['SimHei']  # 替換sans-serif字型
plt.rcParams['axes.unicode_minus'] = False
plt.style.use('fivethirtyeight')

x = np.linspace(0, 10, 1000)
y = np.sin(x)
z = np.cos(x ** 2)
plt.figure(figsize=(8, 4))
plt.plot(x, y, label="$sin(x)$", color="red", linewidth=2)
plt.plot(x, z, "b--", label="$cos(x^2)$")
plt.xlabel("x轴")
plt.ylabel("y轴")
plt.title("标题")
plt.ylim(-1.2, 1.2)
plt.legend()
plt.show()

Pandas 的数据文件

课程所需要的 pandas 调用的 csv 数据,会预先放在 `~/common_data` 目录下,可以使用类似下面的例子读入。因为文件已经做了 utf-8 的编码,所以不需要指定数据文件编码为 gb2312。
目前放了三个文件。
  • 历史水资源情况.csv
  • 2018年各省级行政区水资源量.csv
  • 2018年各省级行政区供水量和用水量.csv
import pandas as pd

dataframe = pd.read_csv('~/common_data/历史水资源情况.csv')
print(dataframe)

点赞2
收藏0
加入专辑

附件资料

没有附件

所需硬件

暂无数据

Connie

👍

回复0

大牛,别默默的看了,快登录帮我点评一下吧!

立即注册

问天鼓

美科科技问天鼓

作品信息

暂无更多信息
500