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

tamasgal
  • 24,826
  • 18
  • 96
  • 135

2 Answers2

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

Lee
  • 29,398
  • 28
  • 117
  • 170
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?

Paul H
  • 65,268
  • 20
  • 159
  • 136
  • 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). – tamasgal May 14 '14 at 19:48
  • It is both, you need `extent` + `interoplation='none'` – tacaswell May 15 '14 at 00:11