如何使用matplotlib在python中绘制完美光滑的球体? [英] How to plot a perfectly smooth sphere in python using matplotlib?

查看:235
本文介绍了如何使用matplotlib在python中绘制完美光滑的球体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用matplotlib在python中绘制一个完美光滑的球体.我一直在使用以下代码:

I am trying to plot a perfectly smooth sphere in python using matplotlib. I have been using the following code:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

fig = plt.figure(1)    
ax = fig.add_subplot(111, projection='3d')

u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)
x = np.outer(np.cos(u), np.sin(v))
y = np.outer(np.sin(u), np.sin(v))
z = np.outer(np.ones(np.size(u)), np.cos(v))

ax.plot_surface(x, y, z, linewidth=0.0)

plt.show()

我获得的情节附在下面:

The plot I obtained is attached below:

生成的图形由其上的矩形表面的小块组成.是否可以理顺边界或使其难以区分并形成一个完美光滑的球体?

The figure generated consists of patches of rectangular surfaces on it. Is it possible to smoothen out the boundaries or make it indistinguishable and to make a perfectly smooth sphere?

推荐答案

Matplotlib通过将3d曲面分解为相同颜色的小子多边形来进行绘制,如

Matplotlib plots 3d surfaces by breaking them down into small sub-polygons of equal colour, as is explained in the documentation, hence your result is not really a surprise. In order to get a smoother surface, you have to provide more data points. There is, however, a small twist, which is that plot_surface() may not use all the data you provide. This is controlled with the cstride and rstride keywords. How the defaults are calculated is not quite clear to me, but below a little example that demonstrates the effect:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

fig,axes = plt.subplots(ncols=2,nrows=2,subplot_kw=dict(projection='3d'))

N=50
stride=2
ax = axes[0,0]
u = np.linspace(0, 2 * np.pi, N)
v = np.linspace(0, np.pi, N)
x = np.outer(np.cos(u), np.sin(v))
y = np.outer(np.sin(u), np.sin(v))
z = np.outer(np.ones(np.size(u)), np.cos(v))
ax.plot_surface(x, y, z, linewidth=0.0, cstride=stride, rstride=stride)
ax.set_title('{0}x{0} data points, stride={1}'.format(N,stride))

N=50
stride=1
ax = axes[0,1]
u = np.linspace(0, 2 * np.pi, N)
v = np.linspace(0, np.pi, N)
x = np.outer(np.cos(u), np.sin(v))
y = np.outer(np.sin(u), np.sin(v))
z = np.outer(np.ones(np.size(u)), np.cos(v))
ax.plot_surface(x, y, z, linewidth=0.0, cstride=stride, rstride=stride)
ax.set_title('{0}x{0} data points, stride={1}'.format(N,stride))

N=200
stride=2
ax = axes[1,0]
u = np.linspace(0, 2 * np.pi, N)
v = np.linspace(0, np.pi, N)
x = np.outer(np.cos(u), np.sin(v))
y = np.outer(np.sin(u), np.sin(v))
z = np.outer(np.ones(np.size(u)), np.cos(v))
ax.plot_surface(x, y, z, linewidth=0.0, cstride=stride, rstride=stride)
ax.set_title('{0}x{0} data points, stride={1}'.format(N,stride))

N=200
stride=1
ax = axes[1,1]
u = np.linspace(0, 2 * np.pi, N)
v = np.linspace(0, np.pi, N)
x = np.outer(np.cos(u), np.sin(v))
y = np.outer(np.sin(u), np.sin(v))
z = np.outer(np.ones(np.size(u)), np.cos(v))
ax.plot_surface(x, y, z, linewidth=0.0, cstride=stride, rstride=stride)
ax.set_title('{0}x{0} data points, stride={1}'.format(N,stride))

plt.show()

结果图形如下:

如您所见,图的结果对数据的密度和stride关键字均敏感.但是,请注意提供的数据量-plot_surface()可能会花费大量时间来提供结果.希望这会有所帮助.

As you can see, the outcome of the plot is sensitive to both the density of your data and the stride keywords. Be careful though with the amount of data you provide -- plot_surface() can take up a considerable amount of time to provide a result. Hope this helps.

这篇关于如何使用matplotlib在python中绘制完美光滑的球体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆