19

我正在尝试使用 matplotlib 将散点图叠加到等高线图上,其中包含

    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$')

最终发生的是结果图扩展到包括所有散点,这可能超出等高线图的限制。有没有办法解决这个问题?

4

1 回答 1

26

我使用以下示例来尝试复制您的问题。如果保留默认值,x 和 y 的范围是 -3 到 3。我输入了 xlim 和 ylim,所以两者的范围都是 -2 到 2。它有效。

   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()
于 2012-04-24T03:45:13.937 回答