如何在matplotlib中设置轴的限制?

时间:2012-11-21 01:26:49

标签: python matplotlib bounds axes

我有这段代码:

ax = fig.add_axes([0.1, 0.1, 0.55, 0.8])

我想在ax的x轴上设置args.avg_window的下限。我尝试过使用xlim,set_xlim,set_xbound,现在将xrange=(args.avg_window, args.posts_limit)添加到该函数调用中,但似乎没有任何效果:

使用

ax = fig.add_axes([0.1, 0.1, 0.55, 0.8], xrange=(args.avg_window, args.posts_limit))

我收到此错误:

Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 162, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/Users/d3admin6/python_phpBB_scraper/analysis/liwc/plot_forum_emos.py", line 334, in <module>
    .format(args.xattr, args.yattr, window_str))
  File "/Users/d3admin6/python_phpBB_scraper/analysis/liwc/plot_forum_emos.py", line 247, in plot_emos_line
    ax = fig.add_axes([0.1, 0.1, 0.55, 0.8], xrange=(args.avg_window, args.posts_limit))
  File "/Library/Python/2.7/site-packages/matplotlib/figure.py", line 806, in add_axes
    a = projection_class(self, rect, **kwargs)
  File "/Library/Python/2.7/site-packages/matplotlib/axes.py", line 499, in __init__
    martist.setp(self, **kwargs)
  File "/Library/Python/2.7/site-packages/matplotlib/artist.py", line 1229, in setp
    func = getattr(o, funcName)
AttributeError: 'Axes' object has no attribute 'set_xrange'

使用

ax.set_xlim(left = args.avg_window)

我收到此错误:

Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 162, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/Users/d3admin6/python_phpBB_scraper/analysis/liwc/plot_forum_emos.py", line 335, in <module>
    .format(args.xattr, args.yattr, window_str))
  File "/Users/d3admin6/python_phpBB_scraper/analysis/liwc/plot_forum_emos.py", line 256, in plot_emos_line
    ax.set_xbound(lower=args.avg_window)
  File "/Library/Python/2.7/site-packages/matplotlib/axes.py", line 2477, in set_xbound
    self.set_xlim(upper, lower, auto=None)
  File "/Library/Python/2.7/site-packages/matplotlib/axes.py", line 2553, in set_xlim
    left, right = mtransforms.nonsingular(left, right, increasing=False)
  File "/Library/Python/2.7/site-packages/matplotlib/transforms.py", line 2567, in nonsingular
    if (not np.isfinite(vmin)) or (not np.isfinite(vmax)):
NotImplementedError: Not implemented for this type

我做错了什么,我该怎么做才能设置轴的界限?

2 个答案:

答案 0 :(得分:2)

在您的第一次调用中,这是一个错误的关键字参数:要设置x轴限制,您应该将xlim传递给add_axes

ax = fig.add_axes([0.1, 0.1, 0.55, 0.8], xlim=(args.avg_window, args.posts_limit))

然后使用

设置轴限制
ax.set_xlim(left=args.avg_window)

适合我。可能在您之前的代码中出现了问题,或者您的matplotlib版本中没有实现。我使用1.2版本。

答案 1 :(得分:1)

您的args.avg_window存储为用户输入返回的字符串。在将它作为matplotib轴限制参数传递之前,您需要将其转换为int(),无论您希望采用哪种方式。

set_xlim(int(args.avg_window), right=1)

将设置最小x轴限制,同时保持最大值不变。