0

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)
4
  • 2
    Are you asking about the width of the plot in pixels (which is currently a bit less than 1200 pixels) or the data limits of the axes? You seem to mean the data limits, but then why are you referring to a width in pixels? (They have absolutely nothing to do with each other...) I'm confused... Are you just asking how to turn autoscaling off? Commented May 30, 2012 at 6:06
  • My bad, I meant points, not pixels Commented May 30, 2012 at 11:31
  • @aitchnyu Your question is still not particularly clear, which is why you have not gotten many responses. Can you clarify, do you want your subplot to be 640 pixels wide or do you want the x axis to range from 0 to 640? Just saying 640 points is not very helpful. Commented May 31, 2012 at 12:56
  • @Chris, I do want "x axis to range from 0 to 640"and modified my question. Commented May 31, 2012 at 13:56

2 Answers 2

3

Assuming I understand the question correctly, you can use the Axes.set_xlim() function. You almost had it right in your question, but your call to set_xlim is not quite right and must be after you plot the data:

import matplotlib.pyplot as plt

hf = plt.figure()
hf.set_size_inches( (15,3) )
hf.figure.set_dpi(80)

ha = hf.add_subplot(211)

pic = ...

ha.imshow(pic)

ha.set_xlim((0,640)) # This sets the x axis to range from 0 to 640.

If you want to just adjust the upper or lower limit of your x-axis you can use the left and right keyword arguments of Axes.set_xlim. For example, to set the upper limit of your x-axis to 640 but to leave the lower limit as what ever it was you could use

ha.set_xlim(right=640)

Two points are important to note here:

  • These keyword arguments are different to Axes.set_ylim, which accepts the keyword arguments bottom and top.

  • This is also different to matplotlib.pyplot.xlim, which accepts the keyword arguments xmin and xmax (this is the function @tsyu80 was in fact talking about), and matplotlib.pyplot.ylim, which accepts ymin and ymax.

Sign up to request clarification or add additional context in comments.

Comments

2
+50

First some setup code to follow along:

import numpy as np
import matplotlib.pyplot as plt

figure, (picture, intensity) = plt.subplots(nrows=2, figsize=(15, 3))
figure.set_dpi(80)
picture.imshow(np.random.uniform(size=(5, 50)))
intensity.plot(np.random.random(size=641))

Note the call to plt.subplots (the s at end is important; there's a different function without the s) 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 figsize parameter, dpi setting, and picture axes have nothing to do with the original issue, but I added them to match the original example.

As Chris mentions, you can call

intensity.set_xlim((0, 640))

You can instead pass in a keyword argument to only tweak the desired parameter:

intensity.set_xlim(right=640)

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.

intensity.autoscale(tight=True)

Or if you only want to scale the x-axis:

intensity.autoscale(axis='x', tight=True)

Note autoscale is 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 margins method:

intensity.margins(0)

This sets both x and y axis limits to the data limits and adds the specified padding---in this case, 0. If you actually want some spacing in the y-direction then you can write:

intensity.margins(0, 0.1)

which adds spacing equal to 10% of the y data-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_xlim based on Chris's suggestion.

3 Comments

intensity.set_xlim(xmax=640) is incorrect. The keyword arguments to Axes.set_xlim are left and right. The function matplotlib.pyplot.xlim accepts xmin and xmax as keyword arguments, whereas matplotlib.axes.Axes.set_xlim does not. According to the documentation, the keyword arguments used to be xmin and xmax, although they make no reference to when this was changed.
@Chris Thanks for the correction. I've fixed this in the response.
Thanks for the comprehensive answer!

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.