Pyplot:如何提高 plot_surface 的分辨率以及如何去除线条? [英] Pyplot: how to increase the resolution of plot_surface and how to remove the lines?

查看:29
本文介绍了Pyplot:如何提高 plot_surface 的分辨率以及如何去除线条?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一个很基础的问题,但是看了帮助功能,在网上搜了一下,还是没有找到解决办法.对不起,如果我在这里遗漏了一些明显的东西.

考虑遵循 MWE,该 MWE 旨在使用颜色图且不使用线条在极坐标中绘制 3D 图形:

将 numpy 导入为 np导入 matplotlib.pyplot 作为 plt从 matplotlib 导入 cmr=np.linspace(0,1,100)theta=np.linspace(0,2*np.pi,10000)R,Theta=np.meshgrid(r,theta)X,Y=R*np.cos(Theta),R*np.sin(Theta)Z=R*np.sin(Theta)*np.cos(Theta)fig=plt.figure(1)ax=fig.add_subplot(projection='3d')ax.plot_surface(X,Y,Z,cmap=cm.inferno,linewidth=0)plt.show()

正如您在生成的图中所看到的,尽管询问 linewidth=0 并夸大了 theta 向量的大小,但在表面上仍然可以看到线条,并且颜色分辨率不好:

如何去除白线,获得颜色不断变化的光滑表面?

解决方案

根据

This seems to be a very basic question, but after reading the help function and searching on the internet, I still cannot find a solution. Excuse me if I missed something obvious here.

Consider following MWE that is meant to plot a 3D figure in polar coordinates, using a colormap and without lines :

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm

r=np.linspace(0,1,100)
theta=np.linspace(0,2*np.pi,10000)
R,Theta=np.meshgrid(r,theta)
X,Y=R*np.cos(Theta),R*np.sin(Theta)
Z=R*np.sin(Theta)*np.cos(Theta)

fig=plt.figure(1)
ax=fig.add_subplot(projection='3d')
ax.plot_surface(X,Y,Z,cmap=cm.inferno,linewidth=0)
plt.show()

As you can see in the produced figure, despite asking linewidth=0 and exaggerating the size of the theta vector, lines are visible on the surface, and the color resolution is bad :

How can I get rid of the white lines, and obtain a smooth surface with a continuously changing color?

解决方案

According to this blog post by Adam Murphy, surface plots in matplotlib are generated by filling in a wireframe plot, so it is difficult to get rid of the line artifacts completely even if you set linewidth=0.

However, looking through matplotlib's documentation on surface plots, there are two parameters rstride and cstride that downsample the rows and columns of your X, Y, and Z arrays - the defaults for both of these parameters are 10 so only every 10th row and 10th column are being plotted. Therefore, if we lower rstride and cstride to 5, then your surface plot should have a higher resolution, although it will be slower to render.

To increase the rendering speed, the documentation suggests setting rstride and cstride to be multiples of the number of rows - 1 and the number of columns - 1 for your 2D arrays. Since X, Y, Z all have dimensions (10000, 100) in your original code, I changed the dimensions of these arrays to be (10001, 101) so that 5 evenly divides 101-1 and 10001-1

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

## to maximize rendering speed, we want the parameters rstride and cstride 
## to be multiples of the number of rows-1 and columns-1
r=np.linspace(0,1,101)
theta=np.linspace(0,2*np.pi,10001)
R,Theta=np.meshgrid(r,theta)
X,Y=R*np.cos(Theta),R*np.sin(Theta)
Z=R*np.sin(Theta)*np.cos(Theta)

fig=plt.figure(1)
ax=fig.add_subplot(projection='3d')
ax.plot_surface(X,Y,Z,rstride=5,cstride=5,cmap=cm.inferno,linewidth=0)
plt.show()

这篇关于Pyplot:如何提高 plot_surface 的分辨率以及如何去除线条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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