散点图的轴限制未保存在matplotlib中 [英] axis limits for scatter plot not holding in matplotlib

查看:54
本文介绍了散点图的轴限制未保存在matplotlib中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 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?

推荐答案

我使用以下示例来尝试重现您的问题.如果保留默认值,x 和 y 的范围是 -3 到 3.我输入了 xlim 和 ylim,因此两者的范围都是 -2 到 2.它起作用了.

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()

这篇关于散点图的轴限制未保存在matplotlib中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆