这个问题不同于post,后者正在讨论其他设置网格的方法,而不是使用pyplot.setp()。
matplotlib.pyplot.setp()可用于为所有子图设置一些属性。
这段代码
xlim = (-2,2)
ylim = (-2,2)
f, axs = plt.subplots(2, 2)
a = plt.setp(axs, xlim=xlim, ylim=ylim)
plt.show()
将所有子图的限制设置为(-2,2)。
AxesSubplot.grid可用于设置网格线
f, axs = plt.subplots(2, 2)
axs[0,0].grid(True)
plt.show()
我正在尝试使用pyplot.setp()设置网格线。
f, axs = plt.subplots(2, 2)
plt.setp(axs, grid=True)
plt.show()
遇到此错误
--------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-10-5014b2a61ed3> in <module>()
1 f, axs = plt.subplots(2, 2)
----> 2 plt.setp(axs, grid=True)
3 plt.show()
~/anaconda3/envs/tf11/lib/python3.6/site-packages/matplotlib/pyplot.py in setp(obj, *args, **kwargs)
340 @docstring.copy(_setp)
341 def setp(obj, *args, **kwargs):
--> 342 return _setp(obj, *args, **kwargs)
343
344
~/anaconda3/envs/tf11/lib/python3.6/site-packages/matplotlib/artist.py in setp(obj, *args, **kwargs)
1505 # put args into ordereddict to maintain order
1506 funcvals = OrderedDict((k, v) for k, v in zip(args[::2], args[1::2]))
-> 1507 ret = [o.update(funcvals) for o in objs] + [o.set(**kwargs) for o in objs]
1508 return list(cbook.flatten(ret))
1509
~/anaconda3/envs/tf11/lib/python3.6/site-packages/matplotlib/artist.py in <listcomp>(.0)
1505 # put args into ordereddict to maintain order
1506 funcvals = OrderedDict((k, v) for k, v in zip(args[::2], args[1::2]))
-> 1507 ret = [o.update(funcvals) for o in objs] + [o.set(**kwargs) for o in objs]
1508 return list(cbook.flatten(ret))
1509
~/anaconda3/envs/tf11/lib/python3.6/site-packages/matplotlib/artist.py in set(self, **kwargs)
1013 key=lambda x: (self._prop_order.get(x[0], 0), x[0])))
1014
-> 1015 return self.update(props)
1016
1017 def findobj(self, match=None, include_self=True):
~/anaconda3/envs/tf11/lib/python3.6/site-packages/matplotlib/artist.py in update(self, props)
914
915 with cbook._setattr_cm(self, eventson=False):
--> 916 ret = [_update_property(self, k, v) for k, v in props.items()]
917
918 if len(ret):
~/anaconda3/envs/tf11/lib/python3.6/site-packages/matplotlib/artist.py in <listcomp>(.0)
914
915 with cbook._setattr_cm(self, eventson=False):
--> 916 ret = [_update_property(self, k, v) for k, v in props.items()]
917
918 if len(ret):
~/anaconda3/envs/tf11/lib/python3.6/site-packages/matplotlib/artist.py in _update_property(self, k, v)
910 func = getattr(self, 'set_' + k, None)
911 if not callable(func):
--> 912 raise AttributeError('Unknown property %s' % k)
913 return func(v)
914
AttributeError: Unknown property grid
我要求解释为什么此代码会导致错误,而不是为所有子图设置网格的解决方案。
答案 0 :(得分:1)
可以使用rcParams
。您可以使用以下
import matplotlib.pyplot as plt
rc = {"axes.grid" : True}
plt.rcParams.update(rc)
xlim = (-2,2)
ylim = (-2,2)
f, axs = plt.subplots(2, 2)
a = plt.setp(axs, xlim=xlim, ylim=ylim)
plt.show()
答案 1 :(得分:1)
否,您不能使用plt.setp
来打开网格。
原因是轴没有属性grid
并且没有set_grid
方法,而只有方法.grid(..)
。
您需要调用该方法,
for ax in axs.flat:
ax.grid(True)
其他打开网格的选项显示在How do I draw a grid onto a plot in Python?
中