Plotting coordinates as matplotlib matrices python
I have a set of coordinates, say [(2,3),(45,4),(3,65)]
I need to plot them as a matrix, anyway, I can do it in matplotlib, so I want it to look like this http://imgur.com/Q6LLhmk
source to share
Edit: my original answer was used ax.scatter
. There is a problem with this: if two points are side-by-side, ax.scatter
can draw them with a small gap between them, depending on the scale:
For example, for
data = np.array([(2,3),(3,3)])
Below is a detailed description:
So, here's an alternative solution that fixes this problem:
import matplotlib.pyplot as plt
import numpy as np
data = np.array([(2,3),(3,3),(45,4),(3,65)])
N = data.max() + 5
# color the background white (1 is white)
arr = np.ones((N,N), dtype = 'bool')
# color the dots black (0)
arr[data[:,1], data[:,0]] = 0
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.imshow(arr, interpolation='nearest', cmap = 'gray')
ax.invert_yaxis()
# ax.axis('off')
plt.show()
No matter how much you zoom in, the adjacent squares at (2,3) and (3,3) will stay side by side.
Unfortunately, in contrast ax.scatter
, use ax.imshow
requires creating an array N x N
, so it can be more memory intensive than using ax.scatter
. This shouldn't be a problem as long as it data
contains very large numbers.
source to share