2

I have two arrays of data, x and y. I would like to plot on a scatter plot y vs. x. The range of x is [0,3] and the range of y is [-3, 3]. I then want to grid up this region into an n by m grid and color the points in each region based on the values of a separate 2D numpy array (same shape as the grid, n by m). So, the top-leftmost grid cell of my plot should be colored based on the value of colorarr[0][0] and so on. Anyone have any suggestions on how to do this? The closest I"ve found so far is the following:

2D grid data visualization in Python

Unfortunately this simply displays the colorarr, and not the 2D region I would like to visualize.

Thanks!

Community
  • 1
  • 1
billbert
  • 413
  • 1
  • 5
  • 8

2 Answers2

1

I think what you want is a 2 dimensional histogram. Matplotlib.pyplot makes this really easy.

import numpy as np
import matplotlib.pyplot as plt


# Make some points
npoints = 500
x = np.random.uniform(low=0, high=3, size=npoints)
y = np.random.uniform(low=-3, high=3, size=npoints)

# Make the plot
plt.hist2d(x, y)
plt.colorbar()
plt.show()

enter image description here

veda905
  • 782
  • 2
  • 12
  • 32
1

You can do it from just the color array by setting extent and aspect keywords of imshow

import matplotlib as plt
import numpy as np

zval = np.random.rand(100, 100)
plt.imshow(zvals, extent=[0,3,-3,3], aspect="auto")
plt.show()

What you get is the zval array just "crunched in" the [0:3, -3:3] range. Plot just the zval array in imshow to convince yourself of this.

ljetibo
  • 3,048
  • 19
  • 25