简体   繁体   English

使用 numpy 绘制 Python 图形

[英]Python graphing with numpy

Okay so I'm trying to plot a function with an e to some expression but I keep getting an error at the lines that I have put (##) @ where the error message is好的,所以我试图用 e 绘制一个函数到某个表达式,但我一直在我把 (##) @ 放在错误消息所在的行上收到错误

TypeError: unsupported operand type(s) for *: 'numpy.ufunc' and 'float'类型错误:* 不支持的操作数类型:'numpy.ufunc' 和 'float'

#!C:\Users\msawe\Anaconda3 or C:\Anaconda3\python

import numpy as np
import matplotlib.pyplot as plt

plt.figure (figsize=(10,10), dpi=100)

ax = plt.subplot(111)

plt.title('Projectile Motion: Goround given by h(x) ', size=24)

ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data',0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data',0))

def h(x):
"This function will return the y-coordinate for a given x-coordinate launch."
##return 1 - np.exp(-1* x/1000) + 0.28 *( 1 - np.exp(-0.038*x**2))*(1 - np.cos*(20*x**0.2))

X = np.linspace(0, 10, 101, endpoint=True)  

##plt.plot(X, h(X), color="black", linewidth=3, linestyle="-", label="h(x)")

plt.xlim(0,10)
plt.xticks(np.linspace(0,10,11,endpoint=True))

plt.ylim(0,20.0)
plt.yticks(np.linspace(0,20,11,endpoint=True))

plt.legend(loc='upper left', frameon=False)
plt.savefig("Ch5_P4_lowRes.png",dpi=60)
plt.savefig("Ch5_P4_hiRes.png",dpi=200)
plt.savefig("Ch5_P4_plotting.pdf",dpi=72)

plt.show()

If I could just get a general idea on how to make it work that would be great.如果我能对如何使其工作有一个大致的了解,那就太好了。

Assuming that I'm interpreting your equation correctly, there's a bug in your implementation of np.cos() in your definition of h(x): you wrote np.cos*(...) rather than np.cos(...) .假设我正确地解释了你的方程,那么在你对 h(x) 的定义中np.cos()实现中存在一个错误:你写的是np.cos*(...)而不是np.cos(...) . After fixing that, the code is able to plot- hopefully it's giving the right result!修复之后,代码就可以绘制了——希望它给出了正确的结果!

This would turn your definition of h(x) from:这将使您对 h(x) 的定义从:

def h(x):
    return 1 - np.exp(-1* x/1000) + 0.28 *( 1 - np.exp(-0.038*x**2))*(1 - np.cos*(20*x**0.2))

Into:进入:

def h(x):
    return 1 - np.exp(-1* x/1000) + 0.28 *( 1 - np.exp(-0.038*x**2))*(1 - np.cos(20*x**0.2))

The difference is subtle but important!区别很微妙但很重要! You can easily check for bugs like this by running successively smaller segments of your code to see which operation is throwing the error- this let me quickly hone in on the np.cos*(...) typo.您可以通过连续运行较小的代码段来轻松检查此类错误,以查看哪个操作np.cos*(...)了错误 - 这让我可以快速了解np.cos*(...)错别字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM