在 Python 中绘制数组 - 怎么做?
Plotting an array in Python - How to do it?
我正在编写一些代码,但在绘制 a 中的最终值时遇到问题,请参阅下面的代码:
import scipy.stats
import matplotlib.pyplot as plt
import numpy as np
a = np.zeros((1, 1000))
for i in range(1000):
samples = scipy.stats.norm.rvs(mu, sigma, i+1)
fcn = (abs(samples))**3
a[0, i] = (sum(fcn > 13) / len(fcn))
plt.plot(a) # Would like to plot the values in a, row-wise, against their index values. How to do that?
我很确定这很容易,但只需键入 plt.plot(a) 就会生成一个空图,其中包含以下形式的消息:
<matplotlib.lines.Line2D at 0x1fb259b44c0>,
<matplotlib.lines.Line2D at 0x1fb259b4580>,
<matplotlib.lines.Line2D at 0x1fb259b4640>,
<matplotlib.lines.Line2D at 0x1fb259b4700>,
<matplotlib.lines.Line2D at 0x1fb259b47c0>,
<matplotlib.lines.Line2D at 0x1fb259b4880>,
<matplotlib.lines.Line2D at 0x1fb259b4940>,
在您的代码中,a
是包含 1 个项目的二维数组。
a=a.flatten()
plt.plot(a)
我正在编写一些代码,但在绘制 a 中的最终值时遇到问题,请参阅下面的代码:
import scipy.stats
import matplotlib.pyplot as plt
import numpy as np
a = np.zeros((1, 1000))
for i in range(1000):
samples = scipy.stats.norm.rvs(mu, sigma, i+1)
fcn = (abs(samples))**3
a[0, i] = (sum(fcn > 13) / len(fcn))
plt.plot(a) # Would like to plot the values in a, row-wise, against their index values. How to do that?
我很确定这很容易,但只需键入 plt.plot(a) 就会生成一个空图,其中包含以下形式的消息:
<matplotlib.lines.Line2D at 0x1fb259b44c0>,
<matplotlib.lines.Line2D at 0x1fb259b4580>,
<matplotlib.lines.Line2D at 0x1fb259b4640>,
<matplotlib.lines.Line2D at 0x1fb259b4700>,
<matplotlib.lines.Line2D at 0x1fb259b47c0>,
<matplotlib.lines.Line2D at 0x1fb259b4880>,
<matplotlib.lines.Line2D at 0x1fb259b4940>,
在您的代码中,a
是包含 1 个项目的二维数组。
a=a.flatten()
plt.plot(a)