python绘制网格命令_Python&Matplotlib:如何创建网格网格来绘制surf?

但是我不知道如何将z值转换成grid以便我可以绘制它

下面是示例代码和我的修改。在import numpy as np

import matplotlib.pyplot as plt

from sklearn import mixture

import matplotlib as mpl

from mpl_toolkits.mplot3d import Axes3D

from matplotlib import cm

%matplotlib inline

n_samples = 1000

# generate random sample, two components

np.random.seed(0)

shifted_gaussian = np.random.randn(n_samples, 2) + np.array([20, 5])

sample = shifted_gaussian

# fit a Gaussian Mixture Model with two components

clf = mixture.GMM(n_components=3, covariance_type='full')

clf.fit(sample)

# Plot it

fig = plt.figure()

ax = fig.gca(projection='3d')

X = np.arange(-5, 5, .25)

Y = np.arange(-5, 5, .25)

X, Y = np.meshgrid(X, Y)

## In example Code, the z is generate by grid

# R = np.sqrt(X**2 + Y**2)

# Z = np.sin(R)

# In my case,

# for each point [x,y], the probability value is

# z = clf.score([x,y])

# but How can I generate a grid Z?

Gx, Gy = np.gradient(Z) # gradients with respect to x and y

G = (Gx**2+Gy**2)**.5 # gradient magnitude

N = G/G.max() # normalize 0..1

surf = ax.plot_surface(

X, Y, Z, rstride=1, cstride=1,

facecolors=cm.jet(N),

linewidth=0, antialiased=False, shade=False)

plt.show()

最初绘制z的方法是通过网格生成。但是在我的例子中,拟合的模型不能返回grid-like样式的结果,所以问题是如何生成grid-style z值,并绘制它?在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值