【TUI】轻量画图
🗓 2024年08月03日 📁 文章归类: 0x70_可视化
版权声明:本文作者是郭飞。转载随意,标明原文链接即可。
原文链接:https://www.guofei.site/2024/08/03/tui.html
介绍
使用 TUI 画图,可以在服务器、terminal 等场景画出一些简易的图(char的形式),在很多场景下非常有用。
安装
pip install plotext
画图
折线图
import plotext as plt
import numpy as np
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
# 画图
plt.clear_figure()
plt.title("Sin and Cos")
plt.xlabel("x")
plt.ylabel("y")
plt.plot(x, y1, marker="dot")
plt.plot(x, y2)
plt.canvas_color("default")
plt.axes_color("default")
plt.ticks_color("white")
plt.show()
bar
import plotext as plt
data = {
"Apple": 120,
"Banana": 90,
"Orange": 150,
"Grape": 70,
"Peach": 110
}
# 按销量排序
sorted_items = sorted(data.items(), key=lambda x: x[1], reverse=True)
labels = [x[0] for x in sorted_items]
values = [x[1] for x in sorted_items]
plt.clear_figure()
plt.title("Product Sales Ranking")
plt.xlabel("Product")
plt.ylabel("Sales")
plt.bar(labels, values)
plt.show()
动图
import plotext as plt
import random
import time
x = []
y = [100]
for i in range(1, 51):
x.append(i)
# 生成一点随机波动数据
y.append(y[-1] + random.randint(-5, 5))
plt.clear_figure()
plt.title("Real-time Line Chart")
plt.xlabel("Time")
plt.ylabel("Value")
plt.plot(x, y)
plt.show()
time.sleep(0.1)
您的支持将鼓励我继续创作!