Python画图练习案例分享

目录

话不多说,直接上源码:

# 多边形的绘制案例
import turtle
def main():
turtle.color("green")
# steps代表多边形的绘制
turtle.circle(50,steps=6)
turtle.exitonclick()
if __name__ == "__main__":
main()

# 太阳花案例*******************************************************************
import turtle
import time
turtle.color("red","yellow")
turtle.begin_fill()
for _ in range(50):
turtle.speed(0)
turtle.forward(200)
turtle.left(170)
turtle.end_fill()
turtle.mainloop()

# 颜色五角星案例******************************************************************
import turtle
import time
turtle.pensize(5)
turtle.pencolor("yellow")
turtle.fillcolor("red")
turtle.begin_fill()
for _ in range(5):
turtle.forward(200)
turtle.right(144)
turtle.end_fill()
time.sleep(2)
turtle.penup()
turtle.goto(-150,-120)
turtle.color("violet")
turtle.write("Done",font=("Arial"))
turtle.mainloop()

# 艺术图片*************************************************************************
import turtle
turtle.speed(0)
turtle.delay(0)
turtle.pensize(2)
turtle.bgcolor("black")
colors=["red","blue","yellow","purple"]
for x in range(300):
turtle.color(colors[x%4])
turtle.forward(2*x)
turtle.left(91)
turtle.done()

# #黑六边形*****************************************************************************
import turtle
def bye(x,y):
turtle.bye()
s = turtle.Screen()
s.bgcolor("black")
s.screensize(800,800)
s.title("Class Using")
s.onscreenclick(bye)
p=turtle.Turtle()
p.speed(0)
p.hideturtle()
p.pencolor("red")
p.pensize(3)
p.circle(50,360,6)
turtle.done()

前方高能

#绘制时钟************************************************************************************************
import turtle as tt
from datetime import *
# 当前日期属于一周的第几天
def Week(t):
week = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"]
return week[t.weekday()]
# 获取当前时间
def Date(t):
y = t.year
m = t.month
d = t.day
cur_hour = t.hour;
cur_min = t.minute;
cur_sec = t.second;
return "%s-%d-%d %d:%02d:%02d" % (y, m, d, cur_hour, cur_min, cur_sec)
# 移动画笔,距离为distance
def movePen(distance):
tt.penup()
tt.pensize(5)
tt.pencolor("blue")
tt.fd(distance)
tt.pendown()
# 绘制表针
def makeHands(name, length):
# 清空窗口,重置turtule状态为初始状态
tt.reset()
movePen(-length * 0.1)
# 开始记录多边形的顶点
tt.begin_poly()
tt.fd(length * 1.1)
# 停止记录多边形的顶点
tt.end_poly()
# 返回记录的多边形
handForm = tt.get_poly()
tt.register_shape(name, handForm)
# 初始化
def initial():
global secHand, minHand, hurHand, printer
# 重置方向向北(上),正角度为顺时针
tt.mode("logo")
# 建立并初始化表针
makeHands("secHand", 180)
makeHands("minHand", 150)
makeHands("hurHand", 110)
secHand = tt.Turtle()
secHand.shape("secHand")
minHand = tt.Turtle()
minHand.shape("minHand")
hurHand = tt.Turtle()
hurHand.shape("hurHand")
for hand in secHand, minHand, hurHand:
hand.shapesize(1, 1, 4)
hand.speed(0)
# 输出文字
printer = tt.Turtle()
# 隐藏画笔
printer.hideturtle()
printer.penup()
# 绘制表盘外框
def drawClock(R):
# 清空窗口,重置turtule状态为初始状态
tt.reset()
# 画笔尺寸
tt.pensize(5)
for i in range(60):
movePen(R)
if i % 5 == 0:
tt.fd(20)
movePen(-R - 20)
movePen(R + 20)
if i == 0:
# 写文本
tt.write(int(12), align="center", font=("Consolas", 14, "bold"))
elif i == 30:
movePen(25)
tt.write(int(i / 5), align="center", font=("Consolas", 14, "bold"))
movePen(-25)
elif (i == 25 or i == 35):
movePen(20)
tt.write(int(i / 5), align="center", font=("Consolas", 14, "bold"))
movePen(-20)
else:
tt.write(int(i / 5), align="center", font=("Consolas", 14, "bold"))
movePen(-R - 20)
else:
# 绘制指定半径和颜色的点
tt.dot(5, "red")
movePen(-R)
tt.right(6)
# 表针的动态显示
def handsMove():
t = datetime.today()
second = t.second + t.microsecond * 0.000001
minute = t.minute + second / 60.0
hour = t.hour + minute / 60.0
secHand.seth(6 * second)
minHand.seth(6 * minute)
hurHand.seth(30 * hour)
tt.tracer(False)
printer.fd(65)
tt.pencolor("green")
printer.write(Week(t), align="center", font = ("黑体", 14))
printer.back(130)
printer.write(Date(t), align="center", font = ("Consolas", 14))
# 设置当前画笔位置为原点,方向朝东
printer.home()
tt.tracer(True)
# 经过100ms后继续调用handsMove函数
tt.ontimer(handsMove, 100)
# 调用定义的函数,打开和关闭动画,为更新图纸设置延迟;
tt.tracer(False)
initial()
drawClock(200)
tt.tracer(True)
handsMove()
tt.mainloop()

# 绘制分形树******************************************************************************
import turtle
def draw_branch(branch_length):
'''
绘制分形树
'''
if branch_length > 5:
# 绘制右侧树枝
turtle.forward(branch_length)
print("向前:", branch_length)
turtle.right(20)
print("右转:20度")
draw_branch(branch_length - 15)

# 绘制左侧树枝
turtle.left(40)
print("左转:40度")
draw_branch(branch_length - 15)
# 返回之前的树枝
turtle.right(20)
print("右转:20度")
turtle.backward(branch_length)
print("向后:", branch_length)
def main():
'''
主函数
'''
turtle.speed(0.5)
turtle.pensize(3)
turtle.left(90)
turtle.color('green')
turtle.penup()
turtle.backward(150)
turtle.pendown()
turtle.bgcolor("black")
draw_branch(100)
turtle.exitonclick()
if __name__ == "__main__":
main()

# 彩虹线绘制案例***************************************************************************
import turtle as t
from random import randint as rint
t.shape("turtle")
t.pensize(5)
t.colormode(255)
t.bgcolor("black")
t.tracer(False)
for x in range(700):
t.color(rint(0,255),rint(0,255),rint(0,255))
t.circle(2*(1+x/4),5)
t.speed(0)
t.tracer(True)
t.exitonclick()

到此这篇关于Python画图练习案例分享的文章就介绍到这了,更多相关Python画图内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • Python在画图时使用特殊符号的方法总结

    目录 一.问题背景 二.注意事项 三.常见特殊符号及对应代码 四.引入特殊符号的万能方法 本文总结了python画图中使用各种特殊符号方式 一.问题背景 在论文中,如何使用特殊符号进行表示?这里给出效果图和代码 完整代码: from matplotlib import pyplot import matplotlib.pyplot as plt from matplotlib.font_manager import FontProperties from matplotlib.ticker im

  • python matplotlib各种画图

    目录 1.引入matpltlib库 2.pyplot基础图标函数总结 3.plot函数画图语法规则 4.折线图 4.散点图 5.直方图 6.条形图 纵向 横向 多条 7.饼图 1.引入matpltlib库 matplotlib是一种优秀的python数据可视化第三方库 使用matpltlib库画图时,先将它引入,加载里面的pyplot,并命名为plt,然后使用plot函数画图 import matplotlib.pyplot as plt #plt是引入模块的别名 2.pyplot基础图标函数总

  • Python画图时如何调用本地字体

    matplotlib中的字体文件被封装在font_manager这个子模块中,fontManager.ttflist这个列表涵盖了所有Matplotlib支持的字体. >>> import matplotlib.pyplot as plt >>> from matplotlib.font_manager import fontManager >>> print(fontManager.ttflist[0]) #此为字体文件 <Font 'cmmi

  • python必备库Matplotlib画图神器

    目录 1.安装方法 2.用好官网的例子 最简单的应用-折线图 添加注释的方法 柱状图-BarLabel 折线图之CSD 前言: Matplotlib 通常与 NumPy.Pandas 一起使用,是数据分析中不可或缺的重要工具之一. Matplotlib 是 Python 中类似 MATLAB 的绘图工具,如果您熟悉 MATLAB,那么可以很快的熟悉它.Matplotlib 提供了一套面向对象绘图的 API,它可以轻松地配合 Python GUI 工具包(比如 PyQt,WxPython.Tkin

  • python画图时给图中的点加标签和plt.text的使用

    背景: 今天在用matplotlib模块画各城市2019-nCoV疫情确诊人数和节前流入人口数的图的时候遇到了要给图中的点加上标签示意,原本图长这个样子 现在要给各散点标注是哪个哪个城市,即下面这种图: matplotlib模块加标签主要有matplotlib.pyplot.text()和matplotlib.pyplot.annotate()两个关键函数,后者适用范围更广,今天主要谈一下前者matplotlib.pyplot.text(),简写成plt.text() . 准备知识: 在此重点讲

  • python 随时间序列变动画图的方法

    画这种图要考虑两点: 1.如何生成连续的时间轴 2.如何在图中适当的显示轴标签的样式和数量. import matplotlib.pyplot as plt import matplotlib as mpl import numpy as np % matplotlib inline import datetime#这个包很关键 #设定开始和结束时间 start=datetime.datetime(2013,1,1) stop=datetime.datetime(2013,12,31) delt

  • Python画图练习案例分享

    目录 话不多说,直接上源码: # 多边形的绘制案例 import turtle def main(): turtle.color("green") # steps代表多边形的绘制 turtle.circle(50,steps=6) turtle.exitonclick() if __name__ == "__main__": main() # 太阳花案例*********************************************************

  • Python画图小案例之多啦A梦叮当猫超详细注释

    一步步教你怎么用Python画多啦A梦叮当猫,进一步熟悉Python的基础画图操作. 分析:叮当猫由头.脸.眼.眼珠.鼻子.嘴.胡子.项带.铃当.身子.围嘴.手臂.手.脚组成. 其中:头.脸.眼.眼珠.鼻子.嘴.胡子组成一个部件:其余元件组成一个部件.废话不多说,上代码. 希望您给个关注给个赞,也算对我们的支持了. import math import sys from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWi

  • Python画图小案例之小雪人超详细源码注释

    一步步教你怎么用Python画雪人,进一步熟悉Python的基础画图操作,废话不多说,上代码. 希望您给个关注给个赞,也算对我们的支持了. class Shape: # 基类(雪人各部件(形状)共有的属性) def __init__(self, cvns, points, fill): # 构造方法 画布 位置坐标 颜色 self.cvns = cvns # 画布 self.points = points # 坐标(x1, y1, x2, y2) self.fill = fill self.pi

  • Python Ajax爬虫案例分享

    目录 1. 抓取街拍图片 2. 分析街拍图片结构 3. 按功能不同编写不同方法组织代码 3.1 获取网页json格式数据 3.2 从json格式数据提取街拍图片 3.3 将街拍图片以其md5码命名并保存图片 3.4 main()调用其他函数 4 抓取20page今日头条街拍图片数据 1. 抓取街拍图片 街拍图片网址 2. 分析街拍图片结构 keyword: 街拍 pd: atlas dvpf: pc aid: 4916 page_num: 1 search_json: {"from_search

  • python 绘制3D图案例分享

    目录 1.散点图 代码 输入的数据格式 2.三维表面 surface 代码 输入的数据格式 scatter + surface图形展示 3. 三维瀑布图waterfall 代码 输入的数据格式 4. 3d wireframe code 输入的数据格式 1.散点图 代码 # This import registers the 3D projection, but is otherwise unused. from mpl_toolkits.mplot3d import Axes3D # noqa:

  • python matplotlib画图实例代码分享

    python的matplotlib包支持我们画图,有点非常多,现学习如下. 首先要导入包,在以后的示例中默认已经导入这两个包 import matplotlib.pyplot as plt import numpy as np 然后画一个最基本的图 t = np.arange(0.0, 2.0, 0.01)#x轴上的点,0到2之间以0.01为间隔 s = np.sin(2*np.pi*t)#y轴为正弦 plt.plot(t, s)#画图 plt.xlabel('time (s)')#x轴标签 p

  • Python字体反爬实战案例分享

    目录 实战场景 实战编码 实战场景 本篇博客学习字体反爬,涉及的站点是实习 x,目标站点地址直接百度搜索即可. 可以看到右侧源码中出现了很多“乱码”,这其中就包含了关键信息. 接下来按照常规的套路,在开发者工具中检索字体相关信息,但是筛选之后,并没有得到反爬的字体,只有一个 file? 有些许的可能性. 这里就是一种新鲜的场景了,如果判断不准,那只能用字体样式和字体标签名进行判断了.在网页源码中检索 @font-face 和 myFont,得到下图内容,这里发现 file 字体又出现了,看来解决

  • Python制作七夕表白案例分享

    目录 一.记录一起走过的那些日子 二.创意代码表白 2.1.效果演示 2.2.制作步过程 2.2.1.清屏函数 2.2.2.重定位海龟的位置 2.2.3.显示文字 2.2.4.画出人物 2.2.5.画爱心 2.2.6.主函数 2.2.7.调用主函数 2.3.代码文件 一.记录一起走过的那些日子 讲述和亲爱的TA一起经历的那些故事 那些初见印象 那些浪漫的开始 那些铭记于心的大小事 那些经历的曲折 那些经历的幸福与快乐 那些珍贵的瞬间 那些对未来的期许/计划 二.创意代码表白 以程序员的方式撒狗粮

  • Python画图学习入门教程

    本文实例讲述了Python画图的基本方法.分享给大家供大家参考,具体如下: Python:使用matplotlib绘制图表 python绘制图表的方法,有个强大的类库matplotlib,可以制作出高质量的2D和3D图形,先记录一下,以后慢慢学习. matplotlib下载及API手册地址:http://sourceforge.net/projects/matplotlib/files/matplotlib/ 数学库numpy下载及API手册地址:http://www.scipy.org/Dow

  • python画图--输出指定像素点的颜色值方法

    如下所示: # -*- coding: utf-8 -*- #------------------------------------------------------------------------------- # Name: module1 # Purpose: # # Author: leniy_tsan # # Created: 10-04-2012 # Copyright: (c) leniy_tsan 2012 # Licence: GPL v2 #-------------

随机推荐