2

I found an odd behavior for pyplot plots. When the number of data points exceeds a certain amount, the line does not draw to the end -- there's white space on the far right that I cannot get rid of. I'd like to know how I can force pyplot to plot the all the way.

I am using Python 2.7.5 and matplotlib 1.2.1.

from pylab import *
from random import random
bins = arange(0, 180, 5)
data = array([random() for i in xrange(len(bins))])
plot(bins[:10], data[:10], drawstyle="steps-mid") #draws till the end
title("some data")
figure()
plot(bins, data, drawstyle="steps-mid") #white space at the right
title("all data")
show()

This also happens with other active drawstyle options. I could use a xlim to shrink the xaxis after plotting so that the part cut off is cut off the axis as well, but I'd rather not to as I'd like to keep my axis the way it is. I thought about adding an additional element to bins and a zero to data and using xlimafterwards, but this strikes me as a really quirky fix.

The plots can be seen for all data and some data.

1
  • The default Locator used by matplotlib looks at your data range and tries to pick limits that let you have 'nice' tick spacing. It will pad out the limits as needed. Commented Aug 21, 2013 at 17:39

1 Answer 1

1

Use your Axes' set_xlim() methods to avoid the autorange behavior you're seeing.

data = array([random() for i in xrange(len(bins))])
plot(bins[:10], data[:10], drawstyle="steps-mid") #draws till the end
title("some data")

fig = figure()
ax = fig.add_subplot(111)
plot(bins, data)
ax.set_xlim(min(bins), max(bins)
ax.set_title("all data")
show()
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! I still have to learn how to use axes. But your answer itself resulted in an AttributeError: 'list' object has no attribute 'set_xlim'. Instead, I had to use fig = figure(); ax = fig.add_subplot(111); plot(bins, data); ax.set_xlim(min(bins), max(bins)); ax.set_ttile("all data"); show(). But this does indeed work as intended. Thank you very much!
Yes, indeed. Corrected. Feel free to upvote and/or accept the answer if you're satisfied

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.