引言
隨著數據可視化的需求不斷增加,曲線繪製成為一種非常流行的可視化方式。 Python 作為一種功能強大的編程語言,具有豐富的繪圖功能,可繪製包括動態曲線、數據曲線、科赫曲線、笛卡爾心形曲線等多種曲線類型。
使用Python繪製動態曲線
動態曲線可以非常生動地呈現數據的變化趨勢,通過 pyecharts 包可以輕鬆完成。下面是 Pyecharts 實現動態曲線展示的例子。
import datetime
import random
from pyecharts import charts
# 實時獲取溫度數據
def get_temperature():
temperature = random.randint(10, 20)
timestamp = datetime.datetime.now()
return [timestamp, temperature]
# 更新動態數據
def update_temperature():
line_chart.add_xaxis([get_temperature()[0].strftime("%Y-%m-%d %H:%M:%S")])
line_chart.add_yaxis("溫度", [get_temperature()[1]])
line_chart = charts.Line()
line_chart.set_global_opts(title_opts=charts.TitleOpts(title="動態曲線圖"))
line_chart.add_xaxis([])
line_chart.add_yaxis("溫度", [])
line_chart.render_notebook()
for i in range(10):
update_temperature()
time.sleep(1)
line_chart.render_notebook()
使用Python繪製數據曲線圖
數據曲線是分析和展示數據變化趨勢非常常用的方式,Python 中提供了豐富的數據處理和繪圖工具包,例如 matplotlib、seaborn 等。下面是使用 matplotlib 繪製數據曲線的的例子。
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
y = np.sin(x)
fig = plt.figure()
plt.plot(x, y)
plt.title("數據曲線圖")
plt.show()
使用遞歸法繪製科赫曲線
科赫曲線是一種非常經典的分形曲線,可以通過遞歸演算法來實現。下面是 Python 中使用遞歸法繪製科赫曲線的代碼,其中繪製函數 koch() 是使用遞歸演算法實現的。
import turtle
def koch(tortoise, order, size):
if order == 0:
tortoise.forward(size)
else:
for angle in [60, -120, 60, 0]:
koch(tortoise, order-1, size/3)
tortoise.left(angle)
def main():
tortoise = turtle.Turtle()
window = turtle.Screen()
tortoise.penup()
tortoise.goto(-200,100)
tortoise.pendown()
for i in range(3):
koch(tortoise, 3, 300)
tortoise.right(120)
main()
使用遞歸法繪製科赫曲線 Python
科赫曲線的計算也可以通過遞歸演算法來實現,下面是 Python 中使用遞歸法繪製科赫曲線的代碼,其中繪製函數 koch_curve() 是使用遞歸演算法實現的。
from turtle import *
def koch_curve(turtle, iterations, length, shortening_factor, angle):
if iterations == 0:
turtle.forward(length)
else:
iterations -= 1
length *= shortening_factor
koch_curve(turtle, iterations, length, shortening_factor, angle)
turtle.left(angle)
koch_curve(turtle, iterations, length, shortening_factor, angle)
turtle.right(angle * 2)
koch_curve(turtle, iterations, length, shortening_factor, angle)
turtle.left(angle)
koch_curve(turtle, iterations, length, shortening_factor, angle)
t = Pen()
t.speed(0)
t.penup()
t.goto(-200, 100)
t.pendown()
iterations = 3
length = 300
shortening_factor = 1/3
angle = 60
koch_curve(t, iterations, length, shortening_factor, angle)
exitonclick()
使用Python處理CSV文件繪製曲線
使用 Python 處理 CSV 文件非常方便,通過 pandas 庫的 read_csv() 方法可將 CSV 文件讀取為 DataFrame, 然後可以使用 matplotlib 繪製出曲線圖。下面是 Python 中使用CSV文件數據繪製曲線的例子。
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("data.csv")
plt.plot(df["x"], df["y"])
plt.title("CSV文件繪製曲線")
plt.show()
使用Python繪製函數曲線
Python 中通過使用 sympy 庫可以快速的繪出函數的曲線圖,其中符號計算帶來的優勢在於可對函數求導或者積分,並進行簡化。下面是 Python 中使用 sympy 繪製函數曲線的例子。
import sympy as sym
from sympy.plotting import plot
x = sym.Symbol("x")
y = sym.sin(x)
plot(y, (x, -5, 5))
使用Python繪製loss曲線
loss 曲線常用於機器學習訓練過程可視化,通過 Python 中的 matplotlib 繪製可以快速的進行實現。下面是 Python 中使用 matplotlib 繪製 loss 曲線的例子。
import matplotlib.pyplot as plt
epoch = range(100)
train_loss = [0.9**i + i**0.5 for i in epoch]
plt.plot(epoch, train_loss)
plt.title("Loss曲線")
plt.show()
使用Python繪製概率密度曲線
概率密度曲線對於了解數據分布情況非常有幫助,通過 Python 中的 scipy 包可以快速的對數據進行分析並且繪製出概率密度曲線。下面是 Python 中使用 scipy 繪製概率密度曲線的例子。
import numpy as np
from scipy.stats import norm
import matplotlib.pyplot as plt
x = np.linspace(-10, 10, 100)
plt.plot(x, norm.pdf(x, loc=0, scale=1))
plt.title("概率密度曲線")
plt.show()
使用Python繪製笛卡爾心形曲線
笛卡爾心形曲線是一種經典的二維曲線,它的數學表達式為 (x^2+y^2-1)^3 – x^2y^3 = 0。Python 中多種繪圖庫都可以完成對笛卡爾心形曲線的繪製。下面是 Python 中使用 matplotlib 繪製笛卡爾心形曲線的例子。
import matplotlib.pyplot as plt
import numpy as np
t = np.linspace(0, 2*np.pi, 1000)
x = np.sin(t)*(np.cos(t)**3)
y = np.cos(t)*(np.sin(t)**3)
plt.plot(x, y)
plt.title("笛卡爾心形曲線")
plt.show()
使用Python繪製螺旋曲線
螺旋曲線是一種非常有趣的曲線類型,Python 中使用 turtle 庫可以快捷的完成對螺旋曲線的繪製。下面是 Python 中使用 turtle 繪製螺旋曲線的例子。
import turtle
def spiral(n):
for i in range(n):
turtle.forward(i * 10)
turtle.right(144)
turtle.speed(0)
turtle.pensize(2)
turtle.pencolor("red")
spiral(25)
turtle.done()
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/293022.html