Python 散点图的轴限制未保留在matplotlib中
我尝试使用matplotlib将散点图覆盖到等高线图上,matplotlib包含Python 散点图的轴限制未保留在matplotlib中,python,matplotlib,Python,Matplotlib,我尝试使用matplotlib将散点图覆盖到等高线图上,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['
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$')
最终的结果是结果图扩展到包括所有散点,这可能超过等高线图的限制。有什么办法可以解决这个问题吗?我使用了下面的示例来尝试复制您的问题。如果保留默认值,则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()
你能给出一个我们可以尝试的完整示例吗?你能打印
xLimHist
和yLimHist
的值,并显示它们的计算方式/位置吗?当我绘制自己的等高线图,然后叠加散射点时,您的代码工作正常。如果我将xLimHist
和yLimHist
定义为应该排除一些散点,那么绘图确实排除了这些散点,并且它不适合包含所有散点。所以我猜您对xLimHist
和yLimHist
的定义不正确。