1

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.

2 Answers 2

4

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()

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

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()

enter image description here

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

2 Comments

I cannot really understand how to implement this in my case as I have a 2D array. Could you show me how to do it with a 2D array?
You take a slice from your 2D array corresponding to a fixed y (or x). And use that as the color argument e.g c=array[i].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.