I have a dataset of 640 points, but plotting it in subplot scales the axis to 700. How do I set the x axis to range from 0 to 640 itself instead of scaling?
Generally, how do I set the axes to arbitary measures? Please explain the options to set the axes.
Here is the code I am using:
self.figure = Figure()
self.figure.set_size_inches( (15,3) )
self.figure.set_dpi(80)
self.picture = self.figure.add_subplot(211)
#self.picture.xlim(xmax=640)
self.intensity = self.figure.add_subplot(212)
self.picture.imshow(pic)
#... where projection happens to be 640 px wide
self.intensity.plot(projection)
First some setup code to follow along:
Note the call to
plt.subplots(thesat end is important; there’s a different function without thes) requires matplotlib 1.1 or greater. But your original example setup works as well. Also note that the axis is scaled to 700 instead of 640 because matplotlib would prefer to draw a tick at 700 and figures the extra white space isn’t such a big deal.Edit: I just wanted to point out the
figsizeparameter,dpisetting, andpictureaxes have nothing to do with the original issue, but I added them to match the original example.As Chris mentions, you can call
You can instead pass in a keyword argument to only tweak the desired parameter:
If you know that you want tight axis limits, but don’t want to set it manually, the axes object can figure it out based on the data that’s been plotted.
Or if you only want to scale the
x-axis:Note
autoscaleis special because it will readjust if your data limits change (e.g. if you plot another data set that has 680 points).Alternatively, you can use the
marginsmethod:This sets both
xandyaxis limits to the data limits and adds the specified padding—in this case, 0. If you actually want some spacing in they-direction then you can write:which adds spacing equal to 10% of the
ydata-interval. These functions all do what you want, but their different call signatures (and behaviors) are useful in different situations.Edit: fixed keyword argument to
set_xlimbased on Chris’s suggestion.