Draw scientific plots with matplotlib in moonbit
Dependencies
sudo apt-get update && sudo apt-get install python3.13 python3.13-devbrew install python@3.13pip install matplotlibconda install matplotlib# Check Python version
python3 --version
# Get Python development header file path
python3-config --prefixmoon update
moon add Kaida-Amethyst/matplotlib{
"import": [
{
"path" : "Kaida-Amethyst/matplotlib",
"alias" : "plt"
}
],
"link": {
"native": {
"cc": "$CC",
"cc-flags": "$CC_FLAGS",
"cc-link-flags": "$CC_LINK_FLAGS"
}
}
}#!/bin/bash
export PY_VERSION=$(python3 -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')")
export CC=clang # 🚀 We recommend using the clang compiler for better performance
export CC_FLAGS="-I$(python3-config --prefix)/include/python$PY_VERSION -O2 -DNDEBUG"
export CC_LINK_FLAGS="$(python3-config --ldflags) -lpython$PY_VERSION"
export C_INCLUDE_PATH="$(python3-config --prefix)/include/python$PY_VERSION:$C_INCLUDE_PATH"source env.sh$PY_PATH = (python -c "import sys; print(sys.prefix)") | Out-String
$env:PY_VERSION = python -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')"
$env:CC = "clang"
$env:CC_FLAGS = "-I$($PY_PATH.Trim())\include -O2 -DNDEBUG"
$env:CC_LINK_FLAGS = "-L$($PY_PATH.Trim())\libs -lpython$env:PY_VERSION". .\env.ps1let sin : (Double) -> Double = @math.sin
let cos : (Double) -> Double = @math.cos
const PI: Double = @math.PI
fn main {
let n = 5000 // Number of data points
let t : Array[Double] = Array::makei(n, fn (i) {2.0 * PI * i.to_double() / n.to_double()})
let xs = t.map(fn (t) {16.0 * sin(t) * sin(t) * sin(t)})
let ys = t.map(fn (t) {13.0 * cos(t) - 5.0 * cos(2.0 * t) - 2.0 * cos(3.0 * t) - cos(4.0 * t)})
let (_, axes) = @plt.subplots(1, 1)
let ax = axes[0][0]
ax.plot(xs, ys, color=@plt.Red, linewidth=5)
ax.set_title("Moonbit x Matplotlib")
@plt.show()
}moon run main.mbt --target native
import numpy as np
import matplotlib.pyplot as plt
n = 5000
t = np.linspace(0, 2 * np.pi, n)
x = 16 * np.sin(t)**3
y = 13 * np.cos(t) - 5 * np.cos(2*t) - 2 * np.cos(3*t) - np.cos(4*t)
fig, ax = plt.subplots()
ax.plot(x, y, color='red', linewidth = 5)
ax.set_title("Moonbit x Matplotlib")
plt.show()sudo apt-get update && sudo apt-get install python3.13 python3.13-devbrew install python@3.13pip install matplotlibconda install matplotlib# 检查 Python 版本
python3 --version
# 获取 Python 开发头文件路径
python3-config --prefixmoon update
moon add Kaida-Amethyst/matplotlib{
"import": [
{
"path" : "Kaida-Amethyst/matplotlib",
"alias" : "plt"
}
],
"link": {
"native": {
"cc": "$CC",
"cc-flags": "$CC_FLAGS",
"cc-link-flags": "$CC_LINK_FLAGS"
}
}
}#!/bin/bash
export PY_VERSION=$(python3 -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')")
export CC=clang # 🚀 推荐使用 clang 编译器以获得更佳的性能
export CC_FLAGS="-I$(python3-config --prefix)/include/python$PY_VERSION -O2 -DNDEBUG"
export CC_LINK_FLAGS="$(python3-config --ldflags) -lpython$PY_VERSION"
export C_INCLUDE_PATH="$(python3-config --prefix)/include/python$PY_VERSION:$C_INCLUDE_PATH"source env.sh$PY_PATH = (python -c "import sys; print(sys.prefix)") | Out-String
$env:PY_VERSION = python -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')"
$env:CC = "clang"
$env:CC_FLAGS = "-I$($PY_PATH.Trim())\include -O2 -DNDEBUG"
$env:CC_LINK_FLAGS = "-L$($PY_PATH.Trim())\libs -lpython$env:PY_VERSION". .\env.ps1let sin : (Double) -> Double = @math.sin
let cos : (Double) -> Double = @math.cos
const PI: Double = @math.PI
fn main {
let n = 5000 // Number of data points
let t : Array[Double] = Array::makei(n, fn (i) {2.0 * PI * i.to_double() / n.to_double()})
let xs = t.map(fn (t) {16.0 * sin(t) * sin(t) * sin(t)})
let ys = t.map(fn (t) {13.0 * cos(t) - 5.0 * cos(2.0 * t) - 2.0 * cos(3.0 * t) - cos(4.0 * t)})
let (_, axes) = @plt.subplots(1, 1)
let ax = axes[0][0]
ax.plot(xs, ys, color=@plt.Red, linewidth=5)
ax.set_title("Moonbit x Matplotlib")
@plt.show()
}moon run main.mbt --target native
import numpy as np
import matplotlib.pyplot as plt
n = 5000
t = np.linspace(0, 2 * np.pi, n)
x = 16 * np.sin(t)**3
y = 13 * np.cos(t) - 5 * np.cos(2*t) - 2 * np.cos(3*t) - np.cos(4*t)
fig, ax = plt.subplots()
ax.plot(x, y, color='red', linewidth = 5)
ax.set_title("Moonbit x Matplotlib")
plt.show()pub struct Axes {
// private fields
}test "Axes::bar" {
let (fig, axes) = @matplotlib.subplots(1, 1)
let x = [1.0, 2.0, 3.0]
let heights = [4.0, 5.0, 6.0]
axes[0][0].bar(x, heights, width=0.5, color=Red)
}///|
test "Axes::get_title" {
let (fig, axes) = @matplotlib.subplots(1, 1)
axes[0][0].set_title("My Plot")
inspect!(axes[0][0].get_title(), content="\"My Plot\"")
}test "Axes::get_xbound" {
let (fig, axes) = @matplotlib.subplots(1, 1)
axes[0][0].set_xbound(-1.0, 1.0)
let (lower, upper) = axes[0][0].get_xbound()
inspect!((lower, upper), content="(-1.0, 1.0)")
}///|
test "Axes::get_xlabel" {
let (fig, axes) = @matplotlib.subplots(1, 1)
axes[0][0].set_xlabel("X Label")
inspect!(axes[0][0].get_xlabel(), content="\"X Label\"")
}test "Axes::get_xlim" {
let (fig, axes) = @matplotlib.subplots(1, 1)
axes[0][0].set_xlim(-1.0, 1.0)
let (left, right) = axes[0][0].get_xlim()
inspect!((left, right), content="(-1.0, 1.0)")
}///|
test "Axes::get_ybound" {
let (fig, axes) = @matplotlib.subplots(1, 1)
axes[0][0].set_ybound(-1.0, 1.0)
let (lower, upper) = axes[0][0].get_ybound()
inspect!((lower, upper), content="(-1.0, 1.0)")
}///|
test "Axes::get_ylabel" {
let (fig, axes) = @matplotlib.subplots(1, 1)
axes[0][0].set_ylabel("Y Label")
inspect!(axes[0][0].get_ylabel(), content="\"Y Label\"")
}test "Axes::get_ylim" {
let (fig, axes) = @matplotlib.subplots(1, 1)
axes[0][0].set_ylim(-1.0, 1.0)
let (bottom, top) = axes[0][0].get_ylim()
inspect!((bottom, top), content="(-1.0, 1.0)")
}test "Axes::pie" {
let (fig, axes) = @matplotlib.subplots(1, 1)
let values = [35.0, 25.0, 40.0]
let labels = ["A", "B", "C"]
let colors = [
@matplotlib.Color::Red,
@matplotlib.Color::Green,
@matplotlib.Color::Blue,
]
axes[0][0].pie(values, labels~, colors~)
}test "Axes::scatter" {
let (fig, axes) = @matplotlib.subplots(1, 1)
let x = [1.0, 2.0, 3.0]
let y = [4.0, 5.0, 6.0]
axes[0][0].scatter(x, y, color=Red, marker=Star, alpha=0.5)
}///|
test "Axes::set_title" {
let (fig, axes) = @matplotlib.subplots(1, 1)
axes[0][0].set_title("My Plot")
inspect!(axes[0][0].get_title(), content="\"My Plot\"")
}test "Axes::set_xbound" {
let (fig, axes) = @matplotlib.subplots(1, 1)
axes[0][0].set_xbound(-1.0, 1.0)
let (lower, upper) = axes[0][0].get_xbound()
inspect!((lower, upper), content="(-1.0, 1.0)")
}///|
test "Axes::set_ybound" {
let (fig, axes) = @matplotlib.subplots(1, 1)
axes[0][0].set_ybound(-1.0, 1.0)
let (lower, upper) = axes[0][0].get_ybound()
inspect!((lower, upper), content="(-1.0, 1.0)")
}pub(all) enum Color {
Aliceblue
Antiquewhite
Aqua
Aquamarine
Azure
Beige
Bisque
Black
Blanchedalmond
Blue
Blueviolet
Brown
Burlywood
Cadetblue
Chartreuse
Chocolate
Coral
Cornflowerblue
Cornsilk
Crimson
Cyan
Darkblue
Darkcyan
Darkgoldenrod
Darkgray
Darkgreen
Darkgrey
Darkkhaki
Darkmagenta
Darkolivegreen
Darkorange
Darkorchid
Darkred
Darksalmon
Darkseagreen
Darkslateblue
Darkslategray
Darkslategrey
Darkturquoise
Darkviolet
Deeppink
Deepskyblue
Dimgray
Dimgrey
Dodgerblue
Firebrick
Floralwhite
Forestgreen
Fuchsia
Gainsboro
Ghostwhite
Gold
Goldenrod
Gray
Green
Greenyellow
Grey
Honeydew
Hotpink
Indianred
Indigo
Ivory
Khaki
Lavender
Lavenderblush
Lawngreen
Lemonchiffon
Lightblue
Lightcoral
Lightcyan
Lightgoldenrodyellow
Lightgray
Lightgreen
Lightgrey
Lightpink
Lightsalmon
Lightseagreen
Lightskyblue
Lightslategray
Lightslategrey
Lightsteelblue
Lightyellow
Lime
Limegreen
Linen
Magenta
Maroon
Mediumaquamarine
Mediumblue
Mediumorchid
Mediumpurple
Mediumseagreen
Mediumslateblue
Mediumspringgreen
Mediumturquoise
Mediumvioletred
Midnightblue
Mintcream
Mistyrose
Moccasin
Navajowhite
Navy
Oldlace
Olive
Olivedrab
Orange
Orangered
Orchid
Palegoldenrod
Palegreen
Paleturquoise
Palevioletred
Papayawhip
Peachpuff
Peru
Pink
Plum
Powderblue
Purple
Rebeccapurple
Red
Rosybrown
Royalblue
Saddlebrown
Salmon
Sandybrown
Seagreen
Seashell
Sienna
Silver
Skyblue
Slateblue
Slategray
Slategrey
Snow
Springgreen
Steelblue
Tan
Teal
Thistle
Tomato
Turquoise
Violet
Wheat
White
Whitesmoke
Yellow
Yellowgreen
Rgb(Int, Int, Int)
}pub struct Figure {
// private fields
}pub(all) enum LineStyle {
Solid
Dashed
Dotted
DashDot
}pub(all) enum Marker {
Point
Pixel
Circle
Octagon
Square
Pentagon
Plus
PlusFilled
Star
Hexagon1
Hexagon2
X
XFilled
Diamond
ThinDiamond
Vline
Hline
TriangleUp
TriangleDown
TriangleLeft
TriangleRight
TriDown
TriUp
TriLeft
TriRight
}pub enum MatplotlibError {
LoadPyPlotError
LoadPylabError
LoadFuncError(String)
}impl Show for MatplotlibErrorDraw scientific plots with matplotlib in moonbit
Dependencies