簡體   English   中英

Matplotlib - 為表面指定特定顏色

[英]Matplotlib - Assign specific colors to a surface

這段代碼將生成以下兩張圖片,代表一個復雜的函數。 是否可以將第一張圖像的顏色應用於表面? 如果是這樣,如何?

域着色

3d 量級

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import hsv_to_rgb

def saw_func(x, dx, a, b):
    x = x / dx - np.floor(x / dx)
    return a + (b - a) * x

def domain_coloring(mag, arg, phaseres=20):
    arg[arg < 0] += 2 * np.pi
    arg /= (2 * np.pi)
    blackp = saw_func(arg, 1 / phaseres, 0.75, 1)
    blackm = saw_func(np.log(mag), 2 * np.pi / phaseres, 0.75, 1)
    black = blackp * blackm
    H = arg
    S, V = np.ones_like(H), black
    return (hsv_to_rgb(np.dstack([H, S, V])) * 255).astype(np.uint8)

x = y = np.linspace(-2, 2, 500)
x, y = np.meshgrid(x, y)
z = x + 1j * y
f = (z - 1) / (z**2 + z + 1)
mag, arg = np.absolute(f), np.angle(f)
img = domain_coloring(mag, arg)

fig1, ax1 = plt.subplots()
ax1.imshow(
    img,
    extent = [np.amin(x), np.amax(x), np.amin(y), np.amax(y)], 
    interpolation = "nearest",
    origin = "lower",
)
plt.show()

fig2 = plt.figure()
ax2 = fig2.add_subplot(1, 1, 1, projection="3d")
ax2.plot_surface(x, y, mag)
ax2.set_zlim([0, 10])
plt.show()

本教程示例使用參數facecolors= 顏色需要是 0 到 1 之間的 rgb 值。示例代碼使用 200x200 網格,因為 500x500 相當慢(並且在漸近線處也有更多的偽影問題)。 rstridecstride設置為1作為默認plot_surface跳過點。

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import hsv_to_rgb

def saw_func(x, dx, a, b):
    x = x / dx - np.floor(x / dx)
    return a + (b - a) * x

def domain_coloring(mag, arg, phaseres=20):
    arg[arg < 0] += 2 * np.pi
    arg /= (2 * np.pi)
    blackp = saw_func(arg, 1 / phaseres, 0.75, 1)
    blackm = saw_func(np.log(mag), 2 * np.pi / phaseres, 0.75, 1)
    black = blackp * blackm
    H = arg
    S, V = np.ones_like(H), black
    return hsv_to_rgb(np.dstack([H, S, V]))

x = y = np.linspace(-2, 2, 200)
x, y = np.meshgrid(x, y)
z = x + 1j * y
f = (z - 1) / (z**2 + z + 1)
mag, arg = np.absolute(f), np.angle(f)
img = domain_coloring(mag, arg)

fig2 = plt.figure()
ax2 = fig2.add_subplot(1, 1, 1, projection="3d")
ax2.plot_surface(x, y, mag, facecolors=img)
ax2.set_zlim([0, 10])
plt.show()

帶有 facecolors 的 plot_surface

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM