首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >python 3空图

python 3空图
EN

Stack Overflow用户
提问于 2017-11-11 14:18:25
回答 1查看 2.1K关注 0票数 1

我在图表中显示数据有问题。出现了图形框架,但没有看到任何图形。你能帮忙吗?

我确保了x轴的尺寸和数据是一样的.我根本找不出为什么我没有得到一个图形作为回报。

先谢谢你。

代码语言:javascript
复制
 import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm


n = 1000
theta = 0.8

d = np.sqrt(1-theta**2)

def p(x,y):
    "Stochastic kernel for the TAR model"
    return norm().pdf((y-theta*np.abs(x))/d)/d


Z = norm().rvs(n)
X = np.empty(n)

for t in range(n-1):
    X[t+1] = theta*np.abs(X[t])+d*Z[t+1]


n = len(X)
X = X.reshape((n, 1))

ys = np.linspace(-3,3,200)
k = len(ys)
ys = ys.reshape((1,k))

v = p(X,ys)
kernel = np.mean(v, axis=0)    
h = len(kernel)
kernel = kernel.reshape((1,h))


fig, ax = plt.subplots(figsize=(10,7))
ax.plot(ys,kernel, 'b-', lw=2,alpha=0.6, label='look ahead estimate')
plt.show()
EN

回答 1

Stack Overflow用户

发布于 2017-11-11 16:27:47

问题是,通过将两个一维数组( yskernel )分别重塑为1xk或1xh数组,就可以得到二维数组,其中一维数组是1,plot函数显然只遍历第一维,这就是为什么图中没有显示任何内容。我可以想出两个简单的方法来解决这个问题:

  1. 不要重塑变量kernelys..。继续你的代码..。ys = np.linspace(-3,3,200) k= len(ys) #ys =ys.reshape(1,k)v= p(X,ys)核= np.mean(v,axis=0) h=len(核)#核=kernel.reshape(1,h)无花果,ax = plt.subplots(figsize=(10,7)) ax.plot(ys,内核,'b-',lw=2,alpha=0.6,label=‘前瞻性估计’) plt.show()
  2. 按如下方式调用您的情节函数: ax.plot(ys,内核,'b-',lw=2,alpha=0.6,标签=‘展望未来估计’)

我希望这能解决你的问题。

来理解为什么你仍然要重塑X:

让我们首先从维度的角度理解您的函数p(x,y)

代码语言:javascript
复制
def p(x,y):
    "Stochastic kernel for the TAR model"
    """If x is not reshaped, you substract two one-dimensional arrays from each other,
    which have not the same dimensions (dim(x) == 1000, dim(y) == 200 in your case).
    This throws an error.
    If you reshape X before passing to this function, the y array is substracted
    element-wise by each of the values of X, which gives you a matrix with dimension
    dim(x) x dim(y).
    """
    return norm().pdf((y-theta*np.abs(x))/d)/d

为了说明这里发生了什么,在维度上:

代码语言:javascript
复制
>>> X = np.array([[1], [2], [3], [4]])
>>> Y = np.array([1, 2, 3])
>>> Y-X
array([[ 0,  1,  2],
       [-1,  0,  1],
       [-2, -1,  0],
       [-3, -2, -1]])

现在我们来看看p(x,y)返回的矩阵会发生什么

kernelnp.mean(v, axis=0)的计算(其中v是从p(X,ys)返回的矩阵)的工作原理是,np.mean迭代矩阵v的直线并计算矩阵中每个“线向量”的平均值。这为您提供了一个一维数组(ys维度),您可以在ys上绘制该数组。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47238870

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档