如何使用matplotlib最好地可视化此函数?

2024-10-08 18:29:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用matplotlib将这个答案(https://electronics.stackexchange.com/a/293108)中指出的函数可视化

这就是我想到的,看起来很糟糕……我对什么可以是一个列表或一个numpy值数组的理解相当薄弱,我不知道该怎么做。有人能举一个例子,说明你在引用的答案中是如何将这个片段形象化的吗

import math
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator
import numpy as np

def steering(x, y):
    # convert to polar
    r = np.hypot(x, y)
    t = np.arctan(y, x)

    # rotate by 45 degrees
    t += np.pi / 4

    # back to cartesian
    left = r * np.cos(t)
    right = r * np.sin(t)

    # rescale the new coords
    left = left * np.sqrt(2)
    right = right * np.sqrt(2)

    # clamp to -1/+1
    left = np.clip(left, -1, 1)
    right = np.clip(right, -1, 1)

    return left, right


fig, ax = plt.subplots(subplot_kw={"projection": "3d"})

# Make data.
X = np.arange(-1, 1, 0.1)
Y = np.arange(-1, 1, 0.1)
X, Y = np.meshgrid(X, Y)
left, right = steering(X, Y)
print(left)

# Plot the surface.
surf = ax.plot_surface(X, Y, left, cmap=cm.coolwarm,
                       linewidth=0, antialiased=False)

# Customize the z axis.
ax.set_zlim(-1.01, 1.01)
ax.zaxis.set_major_locator(LinearLocator(10))
# A StrMethodFormatter is used automatically
ax.zaxis.set_major_formatter('{x:.02f}')

# Add a color bar which maps values to colors.
fig.colorbar(surf, shrink=0.5, aspect=5)

plt.show()

---编辑 我已经把多元函数的两个输出分成了两个独立的图形…它们仍然没有像我所期望的那样表现

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider

def steering(x, y):
    # convert to polar
    r = np.hypot(x, y)
    t = np.arctan2(y, x)

    # rotate by 45 degrees
    t += np.pi / 4

    # back to cartesian
    left = r * np.cos(t)
    right = r * np.sin(t)

    # rescale the new coords
    left = left * np.sqrt(2)
    right = right * np.sqrt(2)

    # clamp to -1/+1
    #left = np.clip(left, -1, 1)
    #right = np.clip(right, -1, 1)

    return left, right


# Initial x and y arrays
x = np.linspace(-1, 1, 4096)
left, right = steering(x, 0)

# Plotting
fig = plt.figure()
plt.subplots_adjust(bottom=0.25)

(ax, ax2) = fig.subplots(nrows=1, ncols=2)
p, = ax.plot(left, x, 'g', color="blue", label="Left Thrust")
q, = ax.plot(left, x, 'g', color="green", label="Right Thrust")
ax2.quiver(0, 0, 0, 0)
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0.)
plt.title("Power Splitting Algorithm depending on motor thrust axis")
ax.legend()

# Defining the Slider button
# xposition, yposition, width and height
ax_slideY = plt.axes([0.05, 0.1, 0.03, 0.65])
# Defining the Slider button
# xposition, yposition, width and height
ax_slideX = plt.axes([0.25, 0.1, 0.65, 0.03])

# Properties of the slider
s_factorY = Slider(ax_slideY, 'Y Component',
                  -1, 1, orientation="vertical", valinit=0, valstep=1/4096)
s_factorX = Slider(ax_slideX, 'X Component',
                  -1, 1, orientation="horizontal", valinit=0, valstep=1/4096)

# Updating the plot


def update(val):
    current_y = s_factorY.val
    current_x = s_factorX.val

    newLeft, newRight = steering(x, current_y)
    p.set_ydata(newLeft)
    q.set_ydata(newRight)
    #print(f's_factorX.val*4096 {s_factorX.val * 4096}')
    print(f's_factorY.val*4096 {s_factorY.val / 4096}')
    ax2.clear()
    ax2.set_xlim([-2,2])
    ax2.set_ylim([-2,2])
    #ax2.arrow(0, 0, newLeft[int(s_factorX.val * 4095)], newRight[int(s_factorY.val * 4095)])
    #ax2.arrow(-1, 0, -1, newLeft[int(s_factorX.val * 4095)])
    ax2.arrow(-1, 0, 0, newLeft[int(s_factorY.val * 4095)])
    ax2.arrow(1, 0, 0, newRight[int(s_factorY.val * 4095)])
    #ax2.arrow(0, 0, np.cos(current_x), np.sin(current_x))
    # redrawing the figure



# Calling the function "update" when the value of the slider is changed
s_factorY.on_changed(update)
s_factorX.on_changed(update)
plt.show()

Tags: thetoimportrightmatplotlibfactorynpplt

热门问题