# Contour/Contourf Plot Heatmap - Grid - Multiple Items

Hi,

I have been using matplotlib for about 2 weeks now. I thought it would be
good to try plotting heatmaps to show some data.

The idea:

I have 100 values all ranging from 0.00 to 1.00, I would like to create a
graph with a 10 by 10 grid. Therefore each value has 1 section of the grid.

So, for example if the value was 0.10, the colour would be blue, or if 0.95,
the colour red.

Now the problem is I do not know how to access each individual grid
position, I guess I would have some form of counter system to count 0 -> 9
on the X axis and 0 -> 9 on the Y also.

Here is my pseudo code:

···

##########################################

Count1 = 0 ## For the X axis
Count2 = 0 ## For the Y axis
Max = 0 ## To count 0 - 99

ListValues = [0.11, 0.09, 0.34, 0.44, 0.29,.... all the way to 100 values]

while Max < 100:

if Count1 == 10:
Count2 = Count2 + 1 ## Increase the Y axis by one
Count1 = 0 ## If X axis counter = 10, reset it to 0
else:
GridPosition = []
GridPosition.append(Count1)
GridPosition.append(Count2)
GridValue = ListValues[Max]

# Now armed with the GridPosition and GridValue, im stuck on
how to plot the square...

##########################################

So as you can see from above, I have the data ready to fill the relevant
grid position, just unsure on how to actually do it!

Just to clarify, this is a heatmap, so the GridValue should correspond to a
colour and fill that particular square with the colour from the colorbar
i.e.

levels = arange(0.0, 1.0, 0.025)

Thank you in advance for any help.

View this message in context: http://www.nabble.com/Contour-Contourf-Plot-Heatmap---Grid---Multiple-Items-tp18872991p18872991.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

Someone more knowlegeable than me may corrct me here, but the code for getting your data into a grid could be greatly simplified to:

X = array([0,1,2,3,4,5,6,7,8,9,])
Y = array([0,1,2,3,4,5,6,7,8,9,])

Z = ListValues.reshape(10,10)

than you could use something like imshow() to show the data without any interpolation.

Pete.

2008/8/7 stuartornum <stuart@…2110…>

···

Hi,

I have been using matplotlib for about 2 weeks now. I thought it would be

good to try plotting heatmaps to show some data.

The idea:

I have 100 values all ranging from 0.00 to 1.00, I would like to create a

graph with a 10 by 10 grid. Therefore each value has 1 section of the grid.

So, for example if the value was 0.10, the colour would be blue, or if 0.95,

the colour red.

Now the problem is I do not know how to access each individual grid

position, I guess I would have some form of counter system to count 0 -> 9

on the X axis and 0 -> 9 on the Y also.

Here is my pseudo code:

##########################################

Count1 = 0 ## For the X axis

Count2 = 0 ## For the Y axis

Max = 0 ## To count 0 - 99

ListValues = [0.11, 0.09, 0.34, 0.44, 0.29,… all the way to 100 values]

while Max < 100:

``````    if Count1 == 10:

Count2 = Count2 + 1     ## Increase the Y axis by one

Count1 = 0                  ## If X axis counter = 10, reset it to 0

else:

GridPosition = []

GridPosition.append(Count1)

GridPosition.append(Count2)

GridValue = ListValues[Max]

# Now armed with the GridPosition and GridValue, im stuck on
``````

how to plot the square…

##########################################

So as you can see from above, I have the data ready to fill the relevant

grid position, just unsure on how to actually do it!

Just to clarify, this is a heatmap, so the GridValue should correspond to a

colour and fill that particular square with the colour from the colorbar

i.e.

levels = arange(0.0, 1.0, 0.025)

Thank you in advance for any help.

View this message in context: http://www.nabble.com/Contour-Contourf-Plot-Heatmap—Grid—Multiple-Items-tp18872991p18872991.html

Sent from the matplotlib - users mailing list archive at Nabble.com.

This SF.Net email is sponsored by the Moblin Your Move Developer’s challenge

Build the coolest Linux based applications with Moblin SDK & win great prizes

Grand prize is a trip for two to an Open Source event anywhere in the world

http://moblin-contest.org/redirect.php?banner_id=100&url=/

Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Hi Pete,

Thanks for the quick response.

Will imshow() actually plot the graph?

Do I not need to do something like:

contourf(X, Y, Z, levels)

Thanks

···

View this message in context: http://www.nabble.com/Contour-Contourf-Plot-Heatmap---Grid---Multiple-Items-tp18872991p18874222.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

here's an example of pcolormesh on a random 10x10 array

http://www.nabble.com/file/p18874393/spam2.png

stuartornum wrote:

···

Hi Pete,

Thanks for the quick response.

Will imshow() actually plot the graph?

Do I not need to do something like:

contourf(X, Y, Z, levels)

Thanks

View this message in context: http://www.nabble.com/Contour-Contourf-Plot-Heatmap---Grid---Multiple-Items-tp18872991p18874393.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

That looks perfect, how did you do it?

Thanks

kippertoffee wrote:

···

here's an example of pcolormesh on a random 10x10 array

http://www.nabble.com/file/p18874393/spam2.png

stuartornum wrote:

Hi Pete,

Thanks for the quick response.

Will imshow() actually plot the graph?

Do I not need to do something like:

contourf(X, Y, Z, levels)

Thanks

View this message in context: http://www.nabble.com/Contour-Contourf-Plot-Heatmap---Grid---Multiple-Items-tp18872991p18874605.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

Hi Pete / All,

Thank you for the help so far, really appreciate it.

I have managed to plot the graph above using the code you gave me:

···

###################

z=rand(3,3)
pcolormesh(z)
colorbar()
savefig('colour.png')

###################

However, I am trying to find out what rand() actually does..? Apparently I
shouldn't use it unless a specific C library is required...?

Also what does rand(3,3) actually produce? is it a list, dict, im not really
sure.

if I print rand(3,3) I get this:

[[ 0.66877509 0.58785363 0.32387598]
[ 0.30220588 0.39593143 0.82576463]
[ 0.41322716 0.49939853 0.99874519]]

Which looks like a list but without commas, also how would I address a grid
position.. say.. (2,3) and plot a value to that?

Thank you

View this message in context: http://www.nabble.com/Contour-Contourf-Plot-Heatmap---Grid---Multiple-Items-tp18872991p18887443.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

rand() produces a numpy array, so long as numpy is imported into the namespace.

I was simply using the random numbers as example data.

In your case you would take your 1x100 vector of heat data and re-shape it to a 10x10 array. data=data.reshape(10,10)

Pete

2008/8/8 stuartornum <stuart@…2110…>

···

Hi Pete / All,

Thank you for the help so far, really appreciate it.

I have managed to plot the graph above using the code you gave me:

###################

z=rand(3,3)

pcolormesh(z)

colorbar()

savefig(‘colour.png’)

###################

However, I am trying to find out what rand() actually does…? Apparently I

shouldn’t use it unless a specific C library is required…?

Also what does rand(3,3) actually produce? is it a list, dict, im not really

sure.

if I print rand(3,3) I get this:

[[ 0.66877509 0.58785363 0.32387598]

[ 0.30220588 0.39593143 0.82576463]

[ 0.41322716 0.49939853 0.99874519]]

Which looks like a list but without commas, also how would I address a grid

position… say… (2,3) and plot a value to that?

Thank you

View this message in context: http://www.nabble.com/Contour-Contourf-Plot-Heatmap—Grid—Multiple-Items-tp18872991p18887443.html

Sent from the matplotlib - users mailing list archive at Nabble.com.

This SF.Net email is sponsored by the Moblin Your Move Developer’s challenge

Build the coolest Linux based applications with Moblin SDK & win great prizes

Grand prize is a trip for two to an Open Source event anywhere in the world

http://moblin-contest.org/redirect.php?banner_id=100&url=/

Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Thanks again Pete for your help.

I have numpy imported, and here is what my code looks like:

···

############################
import matplotlib
import numpy
from numpy import *
from pylab import *

list = [0.66877509, 0.58785363, 0.32387598, 0.16877509, 0.48785363,
0.22387598, 0.96877509, 0.18785363, 0.52387598]
newlist = list.reshape(3,3)

pcolormesh(newlist)
colorbar()
savefig('colour.png')

############################

However I get the error:

Traceback (most recent call last):
File "colour.py", line 24, in <module>
newlist = list.reshape(3,3)
AttributeError: 'list' object has no attribute 'reshape'
############################

Just to note I get no error when numpy in imported.

Thank you.

View this message in context: http://www.nabble.com/Contour-Contourf-Plot-Heatmap---Grid---Multiple-Items-tp18872991p18888817.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

try this:

list = numpy.array([0.66877509, 0.58785363, 0.32387598, 0.16877509, 0.48785363,

0.22387598, 0.96877509, 0.18785363, 0.52387598])

Pete

2008/8/8 stuartornum <stuart@…2110…>

···

Thanks again Pete for your help.

I have numpy imported, and here is what my code looks like:

############################

import matplotlib

import numpy

from numpy import *

from pylab import *

list = [0.66877509, 0.58785363, 0.32387598, 0.16877509, 0.48785363,

0.22387598, 0.96877509, 0.18785363, 0.52387598]

newlist = list.reshape(3,3)

pcolormesh(newlist)

colorbar()

savefig(‘colour.png’)

############################

However I get the error:

Traceback (most recent call last):

File “colour.py”, line 24, in

``````newlist = list.reshape(3,3)
``````

AttributeError: ‘list’ object has no attribute ‘reshape’

############################

Just to note I get no error when numpy in imported.

Thank you.

AH HA!

Pete, you are a life saver! Thank you so much for all your help !

···

View this message in context: http://www.nabble.com/Contour-Contourf-Plot-Heatmap---Grid---Multiple-Items-tp18872991p18888931.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

stuartornum wrote:

Also what does rand(3,3) actually produce? is it a list, dict, im not really sure.

Since you are using Python, you can just ask it:

>>> import numpy as np
>>> help(np.random.rand)
Help on built-in function rand:

rand(...)
Return an array of the given dimensions which is initialized to
random numbers from a uniform distribution in the range [0,1).

rand(d0, d1, ..., dn) -> random values

Note: This is a convenience function. If you want an
interface that takes a tuple as the first argument
use numpy.random.random_sample(shape_tuple).
>>> np.random.rand(2,3)
array([[ 0.309125 , 0.68037706, 0.59229101],
[ 0.20640643, 0.33197814, 0.20436687]])

hth,
Alan Isaac