散点图在matplotlib中不保持的轴限制

2024-04-24 20:33:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用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$')

最终发生的是结果图扩展到包括所有散射点,这可能超过等高线图的限制。有什么办法绕过这个吗?


Tags: selfifmatplotlibpltoriginlowerextentnlevels
3条回答

我用下面的例子试图复制你的问题。如果保留为默认值,则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()

相关问题 更多 >