如何索引List/numpy数组以便用matplotlib绘制数据

2024-07-02 03:48:44 发布

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

我有一个函数f(x,t) = cos(t)*t + x,我想显示结果在离散时间步t_i和离散宽度步x_j时在宽度{}和时间{}上的变化。在

现在我在SX上呆了一段时间,我真的很尴尬,只能发布这么小的代码,或者说什么都没有(因为什么都没起作用,我做过…): 不过,如果有人有时间帮忙,我会很感激的。在

from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as pyplot
from astropy.io.ascii.latex import AASTex

def func(xi, ti):
    res = np.cos(ti)*ti + xi
    return res

timeSpacing = 100
timeStart = 0
timeEnd = 1
time = np.linspace(timeStart, timeEnd, timeSpacing)

widthSpacing = 300
widthStart = 0
widthEnd = 3
width = np.linspace(widthStart, widthEnd, widthSpacing)

resultList = [None]*timeSpacing
resultListInner = [None]*widthSpacing

for i, ithTime in enumerate(time):
    for j, jthWidth in enumerate(width):
        aas = np.zeros_like(width)
        aas.fill(ithTime)
        resultListInner[j] = ithTime, jthWidth, func(jthWidth, aas)
    resultList[i] = resultListInner

那么如何正确地索引列表和数组,并使用matplotlib绘制数据呢?在


我的情节应该是这样的:

plot

在我的例子中,孔径应该是宽度x,天轮是我的时间t,RMS是我的func(x,t)。在


Tags: fromimport宽度np时间ticoswidth
1条回答
网友
1楼 · 发布于 2024-07-02 03:48:44

有几点:

  • Numpy为数组元素的差异提供了一个非常好的函数:^{}

  • Matplotlib使用^{}创建您想要的绘图(也使用Numpy的^{}

现在,把它们组合成你可能想要的,看起来像这样。在

from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt

def func(xi, ti):
    res = np.cos(ti)*np.sin(xi)
    return res

timeSpacing = 20
timeStart = 0
timeEnd = 1
time = np.linspace(timeStart, timeEnd, timeSpacing)

widthSpacing = 50
widthStart = 0
widthEnd = 3
width = np.linspace(widthStart, widthEnd, widthSpacing)


X,T = np.meshgrid(width,time)
F = func(X,T)

DF = np.diff(np.diff(F,axis=0),axis=1)

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

ax.plot_wireframe(X[:-1,:-1],T[:-1,:-1],DF)

plt.show()

注意,diff应用了两次:在每个维度axis=中应用一次。我也改变了你提供的玩具功能,在这种情况下看起来不错。在

对于更一般的用途,您似乎只想将所有的F数据收集到一个2D数组中,然后从DF =行继续。在

相关问题 更多 >

    热门问题