【问题标题】:matplotlib: why does setting grid with pyplot.setp() causes error?matplotlib:为什么用 pyplot.setp() 设置网格会导致错误?
【发布时间】:2019-11-15 14:19:42
【问题描述】:

这个问题与这个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

我要求解释为什么此代码会导致错误,而不是为所有子图设置网格的解决方案。

【问题讨论】:

  • 您是否要求解释为什么您的代码会导致错误或为所有子图设置网格的解决方案?

标签: python matplotlib


【解决方案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?

【讨论】:

    【解决方案2】:

    可以使用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()
    

    【讨论】:

      猜你喜欢
      • 2017-01-01
      • 2021-02-18
      • 1970-01-01
      • 1970-01-01
      • 2011-11-22
      • 2015-06-18
      • 1970-01-01
      • 1970-01-01
      • 2021-08-05
      相关资源
      最近更新 更多