matplotlib: grid in 3D plots

非 Y 不嫁゛ 提交于 2020-12-01 12:14:26

问题


In matplotlib, how can I show a grid in 3D scatter plot?

In 2D plots I just do: plt.grid(True) and it works like a charm. Now with 3D plots the same call returns a warning:

File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 2792, in grid
   ret = gca().grid(b, which, axis, **kwargs)    
TypeError: grid() takes at most 2 arguments (4 given)

How do I do this?


回答1:


You can use the grid method of the axes to turn the grid on and off.

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

fig = plt.figure()
ax = fig.add_subplot(211, projection='3d')
ax.plot([1,2,3,4,5],[7,4,6,2,8],[4,6,8,9,2],'*')
ax.grid(True)
ax.set_title('grid on')

ax2 = fig.add_subplot(212, projection='3d')
ax2.plot([1,2,3,4,5],[7,4,6,2,8],[4,6,8,9,2],'*')
ax2.grid(False)
ax2.set_title('grid off')
plt.show()

3d plot with and without grid



来源:https://stackoverflow.com/questions/16279978/matplotlib-grid-in-3d-plots

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!