簡體   English   中英

如何更新mplot3d對象中的facecolors?

[英]How to update the facecolors in a mplot3d object?

我正在嘗試創建一個3D表面圖,其facecolors值可以通過滑塊交互式更新。 值得注意的是,在我的情況下,facecolors值與曲面的坐標位置無關。 此處的表面僅代表特定的幾何圖形,而facecolors是映射到該幾何圖形的值。

作為一個基本示例,我嘗試創建一個平面,該平面的面色由到給定中心的歐幾里德距離的函數提供。 中心是我可以通過滑塊調整的參數。 這是我的代碼:

from numpy import pi, sin
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm, colors
from matplotlib.widgets import Slider, Button, RadioButtons

def signal(amp, freq):
    return amp * sin(2 * pi * freq * t)

axis_color = 'lightgoldenrodyellow'

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Adjust the subplots region to leave some space for the sliders and buttons
fig.subplots_adjust(bottom=0.25)

x = np.linspace(0.0, 1.0, num=50)
y = np.linspace(0.0, 1.0, num=50)
t = np.linspace(0.0, 1.0, num=50)
x, y = np.meshgrid(x, y)
z = 1.0 * x + 2.0 * y
def colormapping_values(x_center, y_center):
    return (x-x_center)**2 + (y-y_center)**2

x0 = 0.5
y0 = 0.5
norm = colors.Normalize()
somtethin = cm.jet(norm(colormapping_values(x0, y0)))
surf = ax.plot_surface(x, y, z, facecolors=somtethin)
# Draw the initial plot
# The 'line' variable is used for modifying the line later

# Add two sliders for tweaking the parameters

# Define an axes area and draw a slider in it
x_center_slider_pos  = fig.add_axes([0.15, 0.15, 0.65, 0.03], facecolor=axis_color)
x_center_slider = Slider(x_center_slider_pos, 'X center', 0.0, 1.0, valinit=x0)

# Draw another slider
y_center_slider_pos = fig.add_axes([0.15, 0.1, 0.65, 0.03], facecolor=axis_color)
y_center_slider = Slider(y_center_slider_pos, 'Y center', 0.0, 1.0, valinit=y0)
# Define an action for modifying the line when any slider's value changes
def sliders_on_changed(val):
    print(cm.jet(colormapping_values(x_center_slider.val, y_center_slider.val)).shape)
    surf.set_facecolors(cm.jet(colormapping_values(x_center_slider.val, y_center_slider.val)))
    fig.canvas.draw_idle()
x_center_slider.on_changed(sliders_on_changed)
y_center_slider.on_changed(sliders_on_changed)

# Add a button for resetting the parameters
reset_button_ax = fig.add_axes([0.8, 0.025, 0.1, 0.04])
reset_button = Button(reset_button_ax, 'Reset', color=axis_color, hovercolor='0.975')
def reset_button_on_clicked(mouse_event):
    y_center_slider.reset()
    x_center_slider.reset()
reset_button.on_clicked(reset_button_on_clicked)

plt.show()

初始圖是正確的,但是當我單擊滑塊時,在“ surf.set_facecolors(cm.jet(colormapping_values(x_center_slider.val,y_center_slider.val)))”行中得到“ ValueError:無效的RGBA參數”。 我想我在使用該功能時做錯了什么。 我已經完成了研究,但是還沒有得出我做錯了什么的結論,因為其用法與傳遞參數facecolors時所做的幾乎相同

您需要為.set_facecolors()提供一維數組或顏色列表,而不是當前提供的二維數組。

為此,將數組重塑為形狀為((len(x)-1 * len(y)-1), 4)的形狀。 (請注意, 4是4通道顏色值)。

c_len = (len(x)-1 * len(y)-1)

def sliders_on_changed(val):
    print(cm.jet(colormapping_values(x_center_slider.val, y_center_slider.val)).shape)
    surf.set_facecolors(
            cm.jet(colormapping_values(x_center_slider.val, y_center_slider.val)
                )[:-1, :-1].reshape(c_len, 4))
    surf.set_edgecolors(
            cm.jet(colormapping_values(x_center_slider.val, y_center_slider.val)
                )[:-1, :-1].reshape(c_len, 4))    
    fig.canvas.draw_idle()

請注意,您還需要以相同的方式設置edgecolors。

使用一些不同的滑塊值進行測試:

在此處輸入圖片說明 在此處輸入圖片說明 在此處輸入圖片說明

暫無
暫無

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

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