【问题标题】:'SyntaxError: keyword can't be an expression'语法错误:关键字不能是表达式
【发布时间】:2020-02-10 19:00:05
【问题描述】:

我想绘制这个数值解

import math as ma
import numpy as np  
import matplotlib.pyplot as plt

m=np.array([0, 1, 2, 3])    
x=np.linspace(0,1,1000)
plt.figure()
for i in range (4):
        plt.plot(x, ma.tan(ma.pi*5*x-m[i]*ma.pi/2)=- (0.954)**2*ma.sqrt(0.3**2/x**2-1))
plt.show()

但得到以下错误:

语法错误:关键字不能是表达式

【问题讨论】:

  • 错误是由于使用= 的表达式/赋值。您能否澄清您是否希望绘制函数(即ma.tan(ma.pi*5*x-m[i]*ma.pi/2)- (0.954)**2*ma.sqrt(0.3**2/x**2-1)x 或仅显示tansqrt 表达式在沿x 的点处相等(A==B)的位置?
  • 我明白你的意思!我想绘制左侧和右侧,所以我可以看到两个图表并查看它们的交集。我尝试重写代码,但情节与我预期的相差甚远,此外,rhs 和 lhs 是完全相同的曲线...` x=np.linspace(0.01,0.3,1000) rhs=lhs= np.zeros(1000) for i in range (1000): rhs[i]=ma.tan(ma.pi*5*x[i]-2*ma.pi/2) lhs[i]=-(0.954 )**2*ma.sqrt(0.3**2/x[i]**2-1) plt.figure() plt.plot(x,rhs,label='rhs') plt.plot(x,lhs ,label='lhs') plt.legend() plt.show()`

标签: matplotlib plot python-3.7


【解决方案1】:

我想也许你想要:

for i in range (4):
    plt.plot(x, np.tan(np.pi*5*x - m[i]*np.pi/2))
    plt.plot(x, -0.954**2*np.sqrt(0.3**2/x**2-1))

不请自来的建议:

  • 我建议在使用数组时坚持使用 NumPy;你也不需要math
  • 直接遍历m,而不是对其进行索引。换句话说,先使用for mi in m:,然后使用mi 而不是m[i]

【讨论】:

  • 是的!你说对了!非常感谢!
  • @user12212883 如果对您有帮助,请不要忘记点赞和/或接受!
猜你喜欢
  • 2012-07-22
  • 2016-09-26
  • 2013-06-04
  • 2019-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-26
  • 1970-01-01
相关资源
最近更新 更多