gpt4 book ai didi

python - 关于格式化 DataFrame 以与 matplotlib 一起使用的问题

转载 作者:太空宇宙 更新时间:2023-11-03 20:54:14 25 4
gpt4 key购买 nike

我有一个偏微分方程的解,我想绘制它。我在文档中看到了两种方法,其中一种适合我,另一种则不适合我。不会产生错误。一种方法只是生成正确的绘图(正弦波),另一种方法生成一条斜率为 1 的线。即使我现在有可以运行的代码,第二种方法在将来可能会有用。提前致谢。

工作解决方案:

plt.plot(arange(0, 16*pi, Dt), u[:, index])
plt.show()

这太棒了,而且 super 简单!下面的方法也在 matplotlib 文档中找到,但它产生了错误的绘图。我想知道我的错误:

无效解决方案:

df = pd.DataFrame({'t':arange(0, 16*pi, Dt), 'u':u[:,index]})
plt.plot('t', 'u', data=df)
plt.show()
<小时/>

上下文的完整代码

from math import sin, cos, pi, fabs, log10, ceil, floor
from numpy import arange, zeros
import pandas as pd
from matplotlib import pyplot as plt


#function applies periodic boundary condition where h is the period
def apply_pbc(f, i, Dx, M, h):
f[i][0] = f[i][int(h/Dx)]
f[i][int((M + Dx)/Dx)] = f[i][int((M + Dx)/Dx - 1)]

return f

# function for finding an index associated with
# a particular data point of interest for plotting
# or other analysis
def find_index(start, stop, step, x):
counter = len(arange(start, stop, step))
for i in arange(counter):
x_i = start + i*step
if abs(x - x_i) < pow(10, -15):
index = i
print("x = ", x_i, "@index j = ", i)
break

return index

#main body
if __name__ == "__main__":

#constants
a = 0.25
b = 0.25
c = 1

#period of boundary conditions
h = 4*pi

#space and time endpoints
M = 4*pi
N = 16*pi

#mesh
Dx = 0.005*4*pi
Dt = (0.25*Dx)/c

#simplification of numeric method
r = (Dt*pow(c,2))/pow(Dx,2)

#get size of data set
rows = len(arange(0, N, Dt))
cols = len(arange(-Dx, M, Dx))

#initiate solution arrays
u = zeros((rows, cols))
v = zeros((rows, cols))

#apply initial conditions
for j in range(cols):
x = -Dx + j*Dx
u[0][j] = cos(x)
v[0][j] = 0


#solve
for i in range(1, rows):
for j in range(1, cols - 1):
u[i][j] = u[i-1][j] + v[i-1][j]*Dt \
+ (a/2)*(u[i-1][j+1] - 2*u[i-1][j] + u[i-1][j-1])

v[i][j] = v[i-1][j] \
+ r*(u[i-1][j+1] - 2*u[i-1][j] + u[i-1][j-1]) \
+ (b/2)*(v[i-1][j+1] - 2*v[i-1][j] + v[i-1][j-1])
apply_pbc(u, i, Dx, M, h)
apply_pbc(v, i, Dx, M, h)

print("done")

#we want to plot the solution u(t,x), where x = pi
index = find_index(-Dx, M + Dx, Dx, pi)

df = pd.DataFrame({'t':arange(0,16*pi, Dt), 'u':u[:,index]})
plt.plot('t', 'x', data=df)
# plt.plot(arange(0, 16*pi, Dt), u[:, index])
plt.show()

最佳答案

来自documentation of plt.plot() :

Plotting labelled data

There's a convenient way for plotting objects with labelled data (i.e. data that can be accessed by index obj['y']). Instead of giving the data in x and y, you can provide the object in the data parameter and just give the labels for x and y:

plot('xlabel', 'ylabel', data=obj)

我认为您的代码中有一个拼写错误。在您提供的完整代码中,这是绘制绘图的行:

plt.plot('t', 'x', data=df)

这确实给出了

enter image description here

更改为

plt.plot('t', 'u', data=df)

按预期工作: enter image description here

代码的最后一点:

df = pd.DataFrame({'t':arange(0,16*pi, Dt), 'u':u[:,index]})
plt.plot('t', 'x', data=df) # <-- 'x' instead of 'u'
# plt.plot(arange(0, 16*pi, Dt), u[:, index])
plt.show()

关于python - 关于格式化 DataFrame 以与 matplotlib 一起使用的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56117761/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com