亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 python 繪制二次函數虛數輸入的 4 維圖

如何使用 python 繪制二次函數虛數輸入的 4 維圖

三國紛爭 2023-12-29 14:37:04
我是一名高中生,沒有編碼經驗,但我嘗試編寫一個 matplotlib 3d 圖形,并以顏色漸變作為第四維來繪制函數圖:f(x) = x^2 + 1對于x的所有實數和復數值 (a + b i ) ,這需要我有四個維度:x的 (x + bi) 的實部 x [現在稱為 X] , ( x + bi) [現在稱為W],(y + bi) 的實部y [現在稱為Y],以及(y + bi) 的虛部bi [現在稱為Z]?;旧?,輸入將為x,格式為(X + W),輸出將為f(x),格式為(Y + Z)。我希望圖表具有 X、Y 和 Z 軸,W 表示為圖表表面上的顏色漸變。Z 和 W 是虛數 b*cmath.sqrt(-1)。我想要所有參數的范圍為 (-20, 20)。我的筆記本電腦上有 python 和 anaconda,請幫忙。<3
查看完整描述

1 回答

?
守著星空守著你

TA貢獻1799條經驗 獲得超8個贊

這是我的解決方案:


# Import packages

import numpy as np

from mpl_toolkits.mplot3d import Axes3D   # Needed to create 3D plots

import matplotlib

import matplotlib.pyplot as plt

#%matplotlib notebook                     # Uncomment this if using Jupyter


# Define function

def fun(x):

    # Note: x is a complex() object, and * and + are defined for complex numbers

    return x*x + 1


# Create 1x1 figure with 3 axes

fig = plt.figure()

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


# Define grid of x and w using a step of 0.05

X = W = np.arange(-20.0, 20.0, 0.05)

X, W = np.meshgrid(X,W)

complexVals = X + 1j*W      # Convert X/W grid into complex numbers


# Call the function on the complex numbers and extract the real/imag parts

Y_Z = fun(complexVals)

Y = Y_Z.real

Z = Y_Z.imag


# Create colormap for W

color_dimension = W

minn, maxx = color_dimension.min(), color_dimension.max()

norm = matplotlib.colors.Normalize(minn, maxx)

m = plt.cm.ScalarMappable(norm=norm, cmap='jet')

m.set_array([])

fcolors = m.to_rgba(color_dimension)


# Create surface

ax.plot_surface(X, Y, Z, facecolors=fcolors, vmin=minn, vmax=maxx, shade=False,

                linewidth=0, antialiased=False)


# Set axis labels

ax.set_xlabel('X (Re)')

ax.set_ylabel('Y (Re)')

ax.set_zlabel('Z = f(x) (Im)')


# Create colorbar and set title

cbr = plt.colorbar(m)

cbr.ax.set_title('W')


# Show plot with colorbar

plt.show()

這是輸出:

https://img1.sycdn.imooc.com/658e69a10001511906100476.jpg

但是,如果您使用 Anaconda,我強烈建議將此代碼添加到 Jupyter Notebook 中。這就是你可以獲得交互式情節的原因。
它已經隨 Anaconda 一起安裝,您可以通過jupyter notebook在 Anaconda 提示符中輸入來啟動它。

查看完整回答
反對 回復 2023-12-29
  • 1 回答
  • 0 關注
  • 183 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號