python - 如何在 matplotlib 中设置轴限制?

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

我做错了什么,我可以做什么来设置轴的边界?

最佳答案

在您的第一次调用中,这是一个错误的关键字参数:要设置 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版本。

关于python - 如何在 matplotlib 中设置轴限制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13485015/

相关文章:

python - 列表换行符

python - 从列表列表中返回一个列表,其中包含按索引选取的元素

python - 完全隐藏x轴

uibutton - Swift iOS-如何将按钮添加到 CollectionView 单元格外部

python - 在前 X 个最重要的特征上运行 XGBoost 与使用变换方法之间的区别

python - 绘制和保存大量图形时如何加速 matplotlib?

python - 如何使用类似于 plt.hist 的 matplotlib 绘制 np.histogram 的结果

python - 索引越界 (Python)

ios - 如何在其父 View 范围之外启用 View 触摸?

python - 使用一个 C 程序运行 python 脚本(不同的 python 版本)