2

this question is different from this post, which is discussing other ways to set grid rather than using pyplot.setp().

matplotlib.pyplot.setp() could be used to set some properties for all subplots.

this piece of code

xlim = (-2,2)
ylim = (-2,2)
f, axs = plt.subplots(2, 2)
a = plt.setp(axs, xlim=xlim, ylim=ylim)
plt.show()

sets limits as (-2,2) for all subplots.

AxesSubplot.grid could be used to set grid lines

f, axs = plt.subplots(2, 2)
axs[0,0].grid(True)
plt.show()

I am trying to set grid lines by using pyplot.setp().

f, axs = plt.subplots(2, 2)
plt.setp(axs, grid=True)
plt.show()

get this error

--------------------------------------------------------------------------
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

I am asking for an explanation why this code causes error rather than a solution to set grid for all subplots.

  • Are you asking for an explanation why your code causes error or a solution to set grid for all subplots? – Jay Jul 04 '19 at 22:47

2 Answers2

0

It is possible using rcParams. You can use the following

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()

enter image description here

Sheldore
  • 37,862
  • 7
  • 57
  • 71
0

No, you cannot use plt.setp to turn the grid on.

The reason is that the axes does not have an attribute grid and no set_grid method, but only a method .grid(..).

You would need to call that method,

for ax in axs.flat:
    ax.grid(True)

Other options to turn the grid on are shown in How do I draw a grid onto a plot in Python?

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712