Bohrium
robot
新建

空间站广场

论文
Notebooks
比赛
课程
Apps
我的主页
我的Notebooks
我的论文库
我的足迹

我的工作空间

任务
节点
文件
数据集
镜像
项目
数据库
公开
Bk2_Ch33_贝塞尔曲线_05鸢尾花曲线
《可视之美》
python
鸾尾花书
数据可视化
《可视之美》python鸾尾花书数据可视化
刀刀
发布于 2024-05-29
推荐镜像 :Basic Image:bohrium-notebook:2023-04-07
推荐机型 :c2_m4_cpu
2
鸢尾花曲线
可视化

©️ Copyright 2024 @ Authors
共享协议:本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。
作者信息:Notebook原地址为 https://github.com/Visualize-ML

代码
文本

Chapter 33

鸢尾花曲线

Book_2《可视之美》 | 鸢尾花书:从加减乘除到机器学习

代码
文本
[1]
import numpy as np
import matplotlib.pyplot as plt
import scipy
from math import factorial

import os

# 如果文件夹不存在,创建文件夹
if not os.path.isdir("Figures"):
os.makedirs("Figures")
/opt/conda/lib/python3.8/site-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.16.5 and <1.23.0 is required for this version of SciPy (detected version 1.24.4
  warnings.warn(f"A NumPy version >={np_minversion} and <{np_maxversion}"
代码
文本
[2]
def comb(n, k):
return factorial(n) // (factorial(k) * factorial(n-k))

def get_bezier_curve(points):
n = len(points) - 1
return lambda t: sum(comb(n, i)*t**i * (1-t)**(n-i)*points[i] for i in range(n+1))

def evaluate_bezier(points, total):
bezier = get_bezier_curve(points)
new_points = np.array([bezier(t) for t in np.linspace(0, 1, total)])
return new_points[:, 0], new_points[:, 1]
代码
文本
[3]
# 定义散点数量
randPntNum = 20
tPntNum = 500
tList = np.arange(1, tPntNum + 1)

# 产生连续变化的角度
randList1 = (np.random.rand(randPntNum) - 0.5) * 2 * np.pi
randList2 = (np.random.rand(randPntNum) - 0.5) * 2 * np.pi
randList3 = (np.random.rand(randPntNum) - 0.5) * 2 * np.pi
thetaList1 = np.interp(tList, np.linspace(1, randPntNum, randPntNum), randList1, period=2*np.pi)
thetaList2 = np.interp(tList, np.linspace(1, randPntNum, randPntNum), randList2, period=2*np.pi)
thetaList3 = np.interp(tList, np.linspace(1, randPntNum, randPntNum), randList3, period=2*np.pi)

# 设定色谱颜色

CList = np.array([[4, 99, 128],
[22, 25, 59],
[53, 71, 140],
[78, 122, 199],
[127, 178, 240],
[173, 213, 247]]) / 255
ti = np.linspace(1, tPntNum, CList.shape[0])
CList = np.column_stack([np.interp(tList, ti, CList[:, i], period=2*np.pi) for i in range(3)])
代码
文本

可视化

代码
文本
[4]
fig, ax = plt.subplots()
ax.set_xlim(-1, 1)
ax.set_ylim(-1, 1)
ax.set_aspect('equal')
ax.axis('off')

# 随机生成两个椭圆参数
A1 = np.random.rand()
B1 = np.random.rand()
A2 = np.random.rand()
B2 = np.random.rand()

# 产生贝塞尔曲线
for i in range(tPntNum):
X = np.array([0, A1 * np.cos(thetaList1[i]), A2 * np.cos(thetaList2[i]), np.cos(thetaList3[i])])
Y = np.array([0, B1 * np.sin(thetaList1[i]), B2 * np.sin(thetaList2[i]), np.sin(thetaList3[i])])
bx, by = evaluate_bezier(np.column_stack([X, Y]), 200)
plt.plot(bx, by, color=np.append(CList[i, :], 0.2), linewidth=0.5)

fig.savefig('Figures/鸢尾花曲线,贝塞尔.svg', format='svg')
代码
文本

Oliver Brotherhood鸢尾花开源设计:
https://openprocessing.org/sketch/418113

代码参考:
https://zhuanlan.zhihu.com/p/597953851

代码
文本
[5]
# Repo: https://github.com/Visualize-ML
# Book 2 Beauty of Visualization | From Basic Arithmetic to Machine Learning
# Published and copyrighted by Tsinghua University Press
# Beijing, China, 2023
代码
文本
《可视之美》
python
鸾尾花书
数据可视化
《可视之美》python鸾尾花书数据可视化
点个赞吧
本文被以下合集收录
Book2《可视之美》 | 鸢尾花书:从加减乘除到机器学习
刀刀
更新于 2024-06-03
198 篇3 人关注
推荐阅读
公开
Bk2_Ch33_贝塞尔曲线_03二阶、三阶、四阶贝塞尔曲线
《可视之美》python鸾尾花书数据可视化
《可视之美》python鸾尾花书数据可视化
刀刀
发布于 2024-05-29
公开
Bk2_Ch10_平面等高线_02等高线原理
《可视之美》python鸾尾花书数据可视化
《可视之美》python鸾尾花书数据可视化
刀刀
发布于 2024-05-27