Take the 2-minute tour ×

This is my first matplotlib program, so sorry for my ignorance.

I've two arrays of string. say, A = ['test1','test2'] and B = ['test3','test4']. If any correlation exists between A and B element, their corr value will be set to 1.

        test1 | test2
test3 |   1   |   0

test4 |   0   |   1

Now, I want to draw a scatter diagram where my X axis will be elements of A, Y axis will be elements of B and if correlation value is 1, it'll be marked in the scattered plot. how to do that?

share|improve this question
what have you tried? –  Niek de Klein Apr 26 '12 at 15:38
I've answered your question, but then read it again, and I not sure that understood it. Are are elements of A and B really strings? How test for correlation of strings? If they are numerical values, then you will just get one measure of correlation. From your example, it seems that 'test1' and 'test2' lists. But then it is not clear to me what exactly do you want scatter plot of. I'll still leave my answer up in case it is useful, but it would help if you clarified. –  Akavall Apr 26 '12 at 15:53
Hi,yes elements of A and B are strings, which actually represent two filenames. I generated the correlation values from another program based on some logic. –  bray Apr 26 '12 at 16:01
@bray - So you're wanting to plot a boolean matrix of your correlation values? –  Joe Kington Apr 26 '12 at 17:11
add comment

1 Answer

Maybe something like this:

import matplotlib
import pylab

x = [1,2,3,4]
y = [3,4,8,6]




Let me see if I under stand you correctly now:

You have:

       test1 | test2 | test3
test3 |   1   |   0  |  1

test4 |   0   |   1  |  0

test5 |   1   |   1  |  0

Now you want to represent the above values in in a scatter plot, such that value of 1 is represented by a dot.

Let's say you results are stored in a 2-D list:

results = [[1, 0, 1], [0, 1, 0], [1, 1, 0]]

We want to transform them int two variables so war are able to plot them.

And I believe this code will give you what you are looking for:

import matplotlib
import pylab


x = []
y = []

for ind_1, sublist in enumerate(results):
    for ind_2, ele in enumerate(sublist):
        if ele == 1:



Notice that I do need to import pylab, and you would have play around with the axis labels. Also this feels like a work around, and there might be (probably is) a direct method to do this.

share|improve this answer
This is not working since my elements are strings –  bray Apr 26 '12 at 16:02
Wow, this is exactly what I wanted. Thanks. –  bray Apr 27 '12 at 1:43
Please accept the answer. –  Geoff Apr 4 '13 at 14:50
add comment

Your Answer


