The axes for the scatter plot are not held in matplotlib

I am trying to overlay a scatter plot on 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$') 

As a result, it turns out that the obtained graphs apply to all scatter points that may exceed the limits for the contour graph. Is there any way around this?

+3
source share
1 answer

I used the following example to try to replicate your problem. If you leave the default value, the range for x and y was from -3 to 3. I entered xlim and ylim, so the range for both was from -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() 
+9
source

Source: https://habr.com/ru/post/972911/


All Articles