1

I have this code:

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

I want to set a lowerbound of args.avg_window on the x-axis of ax. I have tried using xlim, set_xlim, set_xbound, and now adding xrange=(args.avg_window, args.posts_limit) to that function call, but nothing seems to work:

With

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

I get this error:

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'

With

ax.set_xlim(left = args.avg_window)

I get this error:

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

What am I doing wrong, and what can I do to set the axes' bounds?

bmu
  • 35,119
  • 13
  • 91
  • 108
wrongusername
  • 18,564
  • 40
  • 130
  • 214
  • 1
    Can you print `args.avg_window` and tell us what it is? Is it an int, float, something else? Based on your last error it looks like you're passing something that matplotlib doesn't expect. This may be due to your keyword arg `left`, which I think makes matplotlib expect a scalar. Try `set_xlim(args.avg_window, right=1)` – John Lyon Nov 21 '12 at 01:43
  • May be [this question](http://stackoverflow.com/questions/7030807/setting-an-axis-in-matplotlib) will be helpful. By the way, there is awesome method subplot.margins(x_margin, y_margin) that solved axis problem for me. – Melevir Nov 21 '12 at 04:03
  • @jozzas ohhh, now that you mention it... `args.avg_window` is some integer that the user enters in the commandline (and parsed using argparse). It prints like an integer in the Terminal, but doesn't do comparisons correctly with integers, although it can be cast to an int and compared correctly that way. At least that was what I had to do to fix another bug. I will try that out -- thanks! :) – wrongusername Nov 21 '12 at 05:26
  • If it's direct from user input, it'll be stored as a string! If you want to make sure it's an integer, you could do the `int()` cast on the user's input and store that as avg_window. – John Lyon Nov 21 '12 at 05:40
  • @jozzas yes, that was the problem! It finally worked after I cast it to an int -- thanks, I'll accept it as the answer if you submit it – wrongusername Nov 21 '12 at 19:45

2 Answers2

2

In your first call it is a wrong keyword argument: to set the x-axis limits you should pass xlim to add_axes:

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

Setting the axis limits afterwards with

ax.set_xlim(left=args.avg_window)

works for me. Maybe something went wrong in your previous code or it is really not implemented in your version of matplotlib. I use version 1.2.

bmu
  • 35,119
  • 13
  • 91
  • 108
  • aha, yes, thank you, this worked after I cast args.avg_window to an int (silly me, I was thinking it was an int, but it was really a string from user input) – wrongusername Nov 21 '12 at 19:46
1

Your args.avg_window was stored as a string as returned by the user's input. You need to cast it to an int() before passing it as a matplotib axes limit argument, whichever way you want to do that.

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

Will set the minimum x-axis limit while leaving the maximum unchanged.

John Lyon
  • 11,180
  • 4
  • 36
  • 44