1
我使用sympy和matplotlib繪製圖形,但出現錯誤。 我使用sympy來解決方程,並使用matplotlib繪製圖形Sympy-matplotlib:x和y必須具有相同的第一維
我在做什麼錯?
下面是代碼:
import numpy as np
import sympy
import math
from mpmath import *
from sympy import *
import matplotlib.pyplot as plt
x= symbols('x')
f, g, h = symbols('f g h', cls=Function)
A, C1, C2 = symbols('A C1 C2')
input = C1 * sin(5*x) + C2 * cos(5*x) + x/25
ex1 = input.subs({x:0})
ex2 = input.subs({x:4})
kq = solve((ex1, ex2), C1, C2)
pttq = input.subs({C1:kq[C1], C2: kq[C2]})
print(pttq)
x = np.arange(0, 5, 1)
y = pttq
fig, ax = plt.subplots()
plt.xlabel('(m)')
plt.ylabel('(KN)')
plt.title('NEO')
plt.plot(x, y)
plt.show()
錯誤消息:
Traceback (most recent call last):
File "\Text", line 31, in <module>
File "C:\Blender\2.78\python\lib\site-packages\matplotlib\pyplot.py", line 3318, in plot
ret = ax.plot(*args, **kwargs)
File "C:\Blender\2.78\python\lib\site-packages\matplotlib\__init__.py", line 1892, in inner
return func(ax, *args, **kwargs)
File "C:\Blender\2.78\python\lib\site-packages\matplotlib\axes\_axes.py", line 1406, in plot
for line in self._get_lines(*args, **kwargs):
File "C:\Blender\2.78\python\lib\site-packages\matplotlib\axes\_base.py", line 407, in _grab_next_args
for seg in self._plot_args(remaining, kwargs):
File "C:\Blender\2.78\python\lib\site-packages\matplotlib\axes\_base.py", line 385, in _plot_args
x, y = self._xy_from_xy(x, y)
File "C:\Blender\2.78\python\lib\site-packages\matplotlib\axes\_base.py", line 244, in _xy_from_xy
"have shapes {} and {}".format(x.shape, y.shape))
ValueError: x and y must have same first dimension, but have shapes (5,) and (1,)
要使用matplotlib,x和y的需要繪製列表(或numpy.array)。這裏'y'是sympy等式,這就是爲什麼你得到一個錯誤 – Mel