简体   繁体   中英

axis limits for scatter plot not holding in matplotlib

I am trying to overlay a scatter plot onto a contour plot using matplotlib, which contains

    plt.contourf(X, Y, XYprof.T, self.nLevels, extent=extentYPY, \
                 origin = 'lower')
    if self.doScatter == True and len(xyScatter['y']) != 0:
        plt.scatter(xyScatter['x'], xyScatter['y'], \
                    s=dSize, c=myColor, marker='.', edgecolor='none')
    plt.xlim(-xLimHist,  xLimHist)
    plt.ylim(-yLimHist, yLimHist)
    plt.xlabel(r'$x$')
    plt.ylabel(r'$y$')

What ends up happening is the resulting plots extend to include all of the scatter points, which can exceed the limits for the contour plot. Is there any way to get around this?

I used the following example to try and replicate your problem. If left to default, the range for x and y was -3 to 3. I input the xlim and ylim so the range for both was -2 to 2. It worked.

   import numpy as np
   import matplotlib.pyplot as plt
   from pylab import *

   # the random data
   x = np.random.randn(1000)
   y = np.random.randn(1000)

   fig = plt.figure(1, figsize=(5.5,5.5))

   X, Y = meshgrid(x, y)
   Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
   Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
   Z = 10 * (Z1 - Z2)

   origin = 'lower'
   CS = contourf(x, y, Z, 10, # [-1, -0.1, 0, 0.1],
                 cmap=cm.bone,
                 origin=origin)

   title('Nonsense')
   xlabel('x-stuff')
   ylabel('y-stuff')

   # the scatter plot:
   axScatter = plt.subplot(111)
   axScatter.scatter(x, y)

   # set axes range
   plt.xlim(-2, 2)
   plt.ylim(-2, 2)

   show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM