使用Python可视化技术,通过控制台的菜单选择,使用matplotlib进行相应的显示。
菜单项1:显示学号
菜单项2:显示姓名
菜单项0:退出
要求:
程序要求实现动态效果,姓与名交替显示,学号逐个数字显示,最终生成Gif动图,并提交。
一、姓与名交替显示
思路:当显示姓时,名与背景画布统一颜色,暂停一秒后显示名,此时姓与背景画布统一颜色
看代码:
def draw_point_name(self, i): # 姓名
self.setup_axes()
x = [1, 3, 1.7, 1.7, 2.3, 2.3, 1.5, 1.25, 1.25, 1.25, 1.25, 1.7,
1.7, 1.7, 1.25, 1.7, 1.25, 1.7, 2.5, 2.7, 2,
2.9, 2.2, 2.4, 2.7, 2.5, 1.9, 3, 2, 2.9, 2.5,
2.5]
y = [7, 7, 7.3, 6.7, 7.3, 6.7, 6.7, 6.4, 6.4, 5,
6.4, 6.4, 6.4, 5, 5, 5, 5.7, 5.7, 6.7, 6.5, 6.4, 6.4, 6.3, 6,
6.3, 6, 6, 6, 5.7, 5.7, 6, 5]
x1 = [5, 5, 5, 5.3, 5.3, 5.3, 5, 5.3, 5, 5.3, 5.5,
6.6, 5.8, 6.5, 6.5, 6.7, 6.5, 5.5, 5.5, 6.7,
5.9, 5.3, 6.3, 6.3, 6.3, 6.7, 6.7, 6.7]
y1 = [7, 5.3, 7, 7, 7, 5.3, 6.2, 6.2, 5.3, 5.3, 7.1, 7.1, 7.5, 6.2,
6.2, 6.4, 6.6, 6.2, 5.8, 5.8, 5.8, 4.8, 5.8, 5,
5, 5, 5, 5.3]
x2 = [9, 9.4, 9, 9.4, 9, 9.4, 9.8, 10,
10.6, 10.4, 9.7, 10.7, 9.8, 10.6, 9.6, 10.9,
10.2, 10.2]
y2 = [7.3, 7, 6.5, 6.2, 5, 5.7, 7.3, 7,
7.3, 7, 6.8, 6.8, 6.2, 6.2, 5.5, 5.5, 6.8, 5]
self.ax.plot(x, y, "b-")
plt.pause(1)
self.ax.plot(x, y, "g-")
self.ax.plot(x1, y1, "b-")
self.ax.plot(x2, y2, "b-")
plt.pause(1)
self.ax.plot(x1, y1, "g-")
self.ax.plot(x2, y2, "g-")
二、学生学号逐个显示
思路:每暂停一秒再继续运行
看代码:
def draw_point(self, i): # 学号
self.setup_axes()
x3 = [1, 3, 3, 3, 3, 1, 1, 1, 1, 3]
y3 = [10.5, 10.5, 10.5, 9.5, 9.5, 9.5, 9.5, 8.5, 8.5, 8.5]
x4 = [4, 4, 4, 6, 6, 6, 4, 6]
y4 = [10.5, 8.5, 10.5, 10.5, 10.5, 8.5, 8.5, 8.5]
x5 = [7, 7, 7, 9, 9, 9, 7, 9]
y5 = [10.5, 8.5, 10.5, 10.5, 10.5, 8.5, 8.5, 8.5]
x6 = [10, 12, 12, 12, 12, 10, 10, 10, 10, 12]
y6 = [10.5, 10.5, 10.5, 9.5, 9.5, 9.5, 9.5, 8.5, 8.5, 8.5]
x7 = [1, 1, 1, 3, 3, 3, 1, 3]
y7 = [6.5, 4.5, 6.5, 6.5, 6.5, 4.5, 4.5, 4.5]
x8 = [4, 6, 6, 6]
y8 = [6.5, 6.5, 6.5, 4.5]
x9 = [7, 9, 9, 9, 9, 7, 7, 7, 7, 9]
y9 = [6.5, 6.5, 6.5, 5.5, 5.5, 5.5, 5.5, 4.5, 4.5, 4.5]
x10 = [10, 12, 12, 12, 10, 12]
y10 = [6.5, 6.5, 6.5, 4.5, 4.5, 4.5]
x11 = [10, 12]
y11 = [5.5, 5.5]
self.ax.plot(x3, y3, "w-")
plt.pause(1)
self.ax.plot(x4, y4, "w-")
plt.pause(1)
self.ax.plot(x5, y5, "w-")
plt.pause(1)
self.ax.plot(x6, y6, "w-")
plt.pause(1)
self.ax.plot(x7, y7, "w-")
plt.pause(1)
self.ax.plot(x8, y8, "w-")
plt.pause(1)
self.ax.plot(x9, y9, "w-")
plt.pause(1)
self.ax.plot(x10, y10, "w-")
self.ax.plot(x11, y11, "w-")
来看看总代码:
import sys
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import os
import time
import numpy as np
class Dyn_Img:
def __init__(self):
self.width = 12
self.height = 12
self.fig = plt.figure()
self.ax = plt.axes()
# self.frame_number = 80
self.per_time = 500
self.do_menu()
def do_menu(self):
self.input_choi = input("1.学号\n2.姓名\n3.退出\n请输入您的选择")
while self.input_choi != "1" and self.input_choi != "2" and self.input_choi != "3":
self.input_choi = input("1.学号\n2.姓名\n3.退出\n请输入您的选择")
def setup_axes(self):
# self.ax.clear()
self.ax.set_xlim(0, self.width)
self.ax.set_ylim(0, self.height)
self.ax.get_xaxis().set_visible(True)
self.ax.get_yaxis().set_visible(True)
self.ax.set_facecolor("blue")
def do_animation(self):
anim = FuncAnimation(self.fig, self.draw_point_name)
self.draw_point_name()
def draw_point_name(self, i): # 姓名
self.setup_axes()
x = [1, 3, 1.7, 1.7, 2.3, 2.3, 1.5, 1.25, 1.25, 1.25, 1.25, 1.7,
1.7, 1.7, 1.25, 1.7, 1.25, 1.7, 2.5, 2.7, 2,
2.9, 2.2, 2.4, 2.7, 2.5, 1.9, 3, 2, 2.9, 2.5,
2.5]
y = [7, 7, 7.3, 6.7, 7.3, 6.7, 6.7, 6.4, 6.4, 5,
6.4, 6.4, 6.4, 5, 5, 5, 5.7, 5.7, 6.7, 6.5, 6.4, 6.4, 6.3, 6,
6.3, 6, 6, 6, 5.7, 5.7, 6, 5]
x1 = [5, 5, 5, 5.3, 5.3, 5.3, 5, 5.3, 5, 5.3, 5.5,
6.6, 5.8, 6.5, 6.5, 6.7, 6.5, 5.5, 5.5, 6.7,
5.9, 5.3, 6.3, 6.3, 6.3, 6.7, 6.7, 6.7]
y1 = [7, 5.3, 7, 7, 7, 5.3, 6.2, 6.2, 5.3, 5.3, 7.1, 7.1, 7.5, 6.2,
6.2, 6.4, 6.6, 6.2, 5.8, 5.8, 5.8, 4.8, 5.8, 5,
5, 5, 5, 5.3]
x2 = [9, 9.4, 9, 9.4, 9, 9.4, 9.8, 10,
10.6, 10.4, 9.7, 10.7, 9.8, 10.6, 9.6, 10.9,
10.2, 10.2]
y2 = [7.3, 7, 6.5, 6.2, 5, 5.7, 7.3, 7,
7.3, 7, 6.8, 6.8, 6.2, 6.2, 5.5, 5.5, 6.8, 5]
self.ax.plot(x, y, "b-")
plt.pause(1)
self.ax.plot(x, y, "g-")
self.ax.plot(x1, y1, "b-")
self.ax.plot(x2, y2, "b-")
plt.pause(1)
self.ax.plot(x1, y1, "g-")
self.ax.plot(x2, y2, "g-")
def draw_point(self, i): # 学号
self.setup_axes()
x3 = [1, 3, 3, 3, 3, 1, 1, 1, 1, 3]
y3 = [10.5, 10.5, 10.5, 9.5, 9.5, 9.5, 9.5, 8.5, 8.5, 8.5]
x4 = [4, 4, 4, 6, 6, 6, 4, 6]
y4 = [10.5, 8.5, 10.5, 10.5, 10.5, 8.5, 8.5, 8.5]
x5 = [7, 7, 7, 9, 9, 9, 7, 9]
y5 = [10.5, 8.5, 10.5, 10.5, 10.5, 8.5, 8.5, 8.5]
x6 = [10, 12, 12, 12, 12, 10, 10, 10, 10, 12]
y6 = [10.5, 10.5, 10.5, 9.5, 9.5, 9.5, 9.5, 8.5, 8.5, 8.5]
x7 = [1, 1, 1, 3, 3, 3, 1, 3]
y7 = [6.5, 4.5, 6.5, 6.5, 6.5, 4.5, 4.5, 4.5]
x8 = [4, 6, 6, 6]
y8 = [6.5, 6.5, 6.5, 4.5]
x9 = [7, 9, 9, 9, 9, 7, 7, 7, 7, 9]
y9 = [6.5, 6.5, 6.5, 5.5, 5.5, 5.5, 5.5, 4.5, 4.5, 4.5]
x10 = [10, 12, 12, 12, 10, 12]
y10 = [6.5, 6.5, 6.5, 4.5, 4.5, 4.5]
x11 = [10, 12]
y11 = [5.5, 5.5]
self.ax.plot(x3, y3, "w-")
plt.pause(1)
self.ax.plot(x4, y4, "w-")
plt.pause(1)
self.ax.plot(x5, y5, "w-")
plt.pause(1)
self.ax.plot(x6, y6, "w-")
plt.pause(1)
self.ax.plot(x7, y7, "w-")
plt.pause(1)
self.ax.plot(x8, y8, "w-")
plt.pause(1)
self.ax.plot(x9, y9, "w-")
plt.pause(1)
self.ax.plot(x10, y10, "w-")
self.ax.plot(x11, y11, "w-")
def drwa_Dyn_Img(self):
if self.input_choi == "1":
# frames = np.arange(0, len(self.id)) frames=self.frame_numbers, interval=self.per_time
anmi = FuncAnimation(self.fig, self.draw_point, interval=500)
anmi.save("学号.gif", dpi=80, writer="pillow")
plt.show()
elif self.input_choi == "2":
anmi = FuncAnimation(self.fig, self.draw_point_name, interval=500)
anmi.save("姓名.gif", dpi=80, writer="pillow")
plt.show()
elif self.input_choi == "3":
exit()
if __name__ == "__main__":
dyn_Img = Dyn_Img()
dyn_Img.drwa_Dyn_Img()
初次写有点紧张,有建议欢迎提出,我慢慢修正,拜拜,晚安