【深耕 Python】Quantum Computing 量子计算机(1)图像绘制基础

一、绘制静止图像

使用matplotlib库绘制函数图像y = sin(pi * x):

import math
import matplotlib.pyplot as plt

x_min = -2.0
x_max = 2.0

N = 1000

x1 = []
y1 = []

for i in range(N + 1):
    x = x_min + (x_max - x_min) * i / N
    y = math.sin(math.pi * x)
    x1.append(x)
    y1.append(y)

plt.xlim([-2, 2])
plt.ylim([-1.2, 1.2])

plt.plot(x1, y1)
plt.title("Y = sin(pi*x)")
plt.xlabel("X")
plt.xlabel("Y")

plt.grid()
plt.show()

程序输出:

在这里插入图片描述

二、绘制图形动画

import math
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig = plt.figure(figsize=(10, 6))

x_min = -2.0
x_max = 2.0

ims = []

N = 100
AN = 30

for a in range(AN):
    phi = 2.0 * math.pi * a / AN

    xl = []
    yl = []

    for i in range(N + 1):
        x = x_min + (x_max - x_min) * i / N
        y = math.sin(math.pi * x + phi)
        xl.append(x)
        yl.append(y)

    img = plt.plot(xl, yl, color="blue", linewidth=3.0, linestyle="solid")
    ims.append(img)

plt.title("Animated Sine Function")
plt.xlabel("x-axis")
plt.ylabel("y-axis")

plt.xlim([-2.0, 2.0])
plt.ylim([-1.2, 1.2])

ani = animation.ArtistAnimation(fig, ims, interval=50)
ani.save("output.html", writer=animation.HTMLWriter())

plt.grid()
plt.show()

使用浏览器打开生成的html文件:

在这里插入图片描述

参考文献 Reference

《14天自造量子计算机:使用薛定谔方程》,【日】远藤理平 著,陈欢 译,北京,中国水利水电出版社,2023年9月。

相关推荐

  1. 【Qt+opencv】基础图像绘制

    2024-05-04 22:38:02       22 阅读
  2. 计算机基础1

    2024-05-04 22:38:02       47 阅读
  3. 计算机基础1-汇编基础

    2024-05-04 22:38:02       40 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-04 22:38:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-04 22:38:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-05-04 22:38:02       82 阅读
  4. Python语言-面向对象

    2024-05-04 22:38:02       91 阅读

热门阅读

  1. Rust 动态数组Vector

    2024-05-04 22:38:02       30 阅读
  2. Ruby递归目录文件的又一种方法

    2024-05-04 22:38:02       28 阅读
  3. 【leetcode】滑动窗口题目总结

    2024-05-04 22:38:02       36 阅读
  4. 初始MySQL

    2024-05-04 22:38:02       30 阅读
  5. Django框架之模板层

    2024-05-04 22:38:02       23 阅读
  6. leetcode39-Combination Sum

    2024-05-04 22:38:02       33 阅读
  7. 深入浅出 iptables - Linux下的强大防火墙工具

    2024-05-04 22:38:02       37 阅读
  8. 大模型+低代码平台

    2024-05-04 22:38:02       33 阅读