Reputation: 5045
I have a 2D numpy array and I want to create a discrete density plot using it. Discrete in the sense that at each point (i,j)
on the plot a dot should be placed whose color should correspond to the value of the (i,j)
th element of the 2D array. I do not want to use imshow
because I do not want any interpolation and I also want to control the size the dots to be placed.
Upvotes: 1
Views: 2020
Reputation: 12234
You can always do this explicitly for each point, but this will be slow. I think it is best to do it line by line (keep say y fixed) and use the scatter
function.
import matplotlib.pylab as plt
from mpl_toolkits.mplot3d import Axes3D
n = 100
x = plt.linspace(0,5, n)
y = plt.linspace(0,5, n)
ax = plt.subplot(111)
for i in range(n):
y_fixed = y[i] * plt.ones(n)
z = [(abs(plt.cos(x[i])), 0.0, 0.5) for i in range(n)]
ax.scatter(x, y_fixed, c=z)
plt.show()
The size is also adjustable in this manor using the s
argument.
Without any data on how you want the color specified I used an RGB
value. You may need to normalise however c=
will take anything pretty much and turn it into a colour, but that may not be very relevant to you.
For more with scatter see the demo here
Upvotes: 0
Reputation: 879471
Have you tried imshow
with interpolation='nearest'
? Is this close to what you want?
import matplotlib.pyplot as plt
import numpy as np
data = np.arange(100).reshape(10, 10)
fig, ax = plt.subplots()
ax.imshow(data, interpolation='nearest')
plt.show()
Upvotes: 4