【问题标题】:Passing an empty string to matplotlib's yticks将一个空字符串传递给 matplotlib 的 yticks
【发布时间】:2015-09-27 22:05:31
【问题描述】:

This webpage 让我相信我可以将空字符串传递给 matplotlib.yticks 以关闭图形的 y 刻度。

但是,当我尝试这样做时,我收到以下错误:

In [1]: from pylab import *

In [2]: figure()
Out[2]: <matplotlib.figure.Figure at 0xb69d43cc>

In [3]: yticks('')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-7ead106262fa> in <module>()
----> 1 yticks('')

/usr/local/lib/python2.7/dist-packages/matplotlib/pyplot.pyc in yticks(*args, **kwargs)
   1631     elif len(args)==1:
   1632         locs = ax.set_yticks(args[0])
-> 1633         labels = ax.get_yticklabels()
   1634     elif len(args)==2:
   1635         locs = ax.set_yticks(args[0])

/usr/local/lib/python2.7/dist-packages/matplotlib/axes/_base.pyc in get_yticklabels(self, minor, which)
   2912         return cbook.silent_list('Text yticklabel',
   2913                                   self.yaxis.get_ticklabels(minor=minor,
-> 2914                                                             which=which))
   2915 
   2916     @docstring.dedent_interpd

/usr/local/lib/python2.7/dist-packages/matplotlib/axis.pyc in get_ticklabels(self, minor, which)
   1212         if minor:
   1213             return self.get_minorticklabels()
-> 1214         return self.get_majorticklabels()
   1215 
   1216     def get_majorticklines(self):

/usr/local/lib/python2.7/dist-packages/matplotlib/axis.pyc in get_majorticklabels(self)
   1166     def get_majorticklabels(self):
   1167         'Return a list of Text instances for the major ticklabels'
-> 1168         ticks = self.get_major_ticks()
   1169         labels1 = [tick.label1 for tick in ticks if tick.label1On]
   1170         labels2 = [tick.label2 for tick in ticks if tick.label2On]

/usr/local/lib/python2.7/dist-packages/matplotlib/axis.pyc in get_major_ticks(self, numticks)
   1295         'get the tick instances; grow as necessary'
   1296         if numticks is None:
-> 1297             numticks = len(self.get_major_locator()())
   1298         if len(self.majorTicks) < numticks:
   1299             # update the new tick label properties from the old

TypeError: len() of unsized object

有什么关系?如果这种关闭 y-ticks 的方法不正确,正确的做法是什么?

【问题讨论】:

    标签: python matplotlib plot figure


    【解决方案1】:

    matplotlib 中的 yticks 函数需要一个列表作为参数而不是字符串。因此,要关闭 y 刻度,

    In [1]: import matplotlib.pyplot as plt
    In [2]: plt.figure()
    Out[2]: <matplotlib.figure.Figure at 0x10ef23710>
    
    In [3]: plt.yticks([])
    Out[3]: ([], <a list of 0 Text yticklabel objects>)
    

    要在 matplotlib 中更好地控制刻度,请考虑使用 tick_params,正如 this answer 中指出的那样。

    【讨论】:

      【解决方案2】:

      这个问题的公认解决方案应该实现您想要的,并让您更好地控制显示的内容。

      Remove xticks in a matplot lib plot?

      例如,关闭 y 轴上的所有刻度:

      >>> from pylab import *
      >>> figure()
      >>> plot(arange(10))
      >>> tick_params(axis='y', which='both', left='off', right='off', labelleft='off')
      >>> show()
      

      【讨论】:

      • 酷,谢谢。是否传递了一个曾经受支持但不再支持的空字符串?
      猜你喜欢
      • 2012-11-13
      • 1970-01-01
      • 1970-01-01
      • 2018-12-13
      • 2012-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多