1

I'm struggling to get the axis right:

I've got the x and y values, and want to plot them in a 2d histogram (to examine correlation). Why do I get a histogram with limits from 0-9 on each axis? How do I get it to show the actual value ranges?

This is a minimal example and I would expect to see the red "star" at (3, 3):

import numpy as np
import matplotlib.pyplot as plt

x = (1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 3)
y = (1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 3)
xedges = range(5)
yedges = range(5)
H, xedges, yedges = np.histogram2d(y, x)
im = plt.imshow(H, origin='low')
plt.show()

histogram2d

2 Answers 2

4

I think the problem is twofold:

Firstly you should have 5 bins in your histogram (it's set to 10 as default):

H, xedges, yedges = np.histogram2d(y, x,bins=5)

Secondly, to set the axis values, you can use the extent parameter, as per the histogram2d man pages:

im = plt.imshow(H, interpolation=None, origin='low',
                extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]])

enter image description here

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

Comments

1

If I understand correctly, you just need to set interpolation='none'

import numpy as np
import matplotlib.pyplot as plt

x = (1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 3)
y = (1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 3)
xedges = range(5)
yedges = range(5)
H, xedges, yedges = np.histogram2d(y, x)
im = plt.imshow(H, origin='low', interpolation='none')

enter image description here

Does that look right?

2 Comments

Not really, the red square is still at (5,5) and want the scales so that they're reflecting the actual data values. The red square should be at (3,3).
It is both, you need extent + interoplation='none'

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.