25

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?

4
  • 6
    what have you tried? Apr 26, 2012 at 15:38
  • 1
    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, 2012 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, 2012 at 16:01
  • @bray - So you're wanting to plot a boolean matrix of your correlation values? Apr 26, 2012 at 17:11

1 Answer 1

78

Maybe something like this:

import matplotlib.pyplot
import pylab

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

matplotlib.pyplot.scatter(x,y)

matplotlib.pyplot.show()

EDIT:

Let me see if I understand 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 into two variables so we are able to plot them.

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

import matplotlib
import pylab


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

x = []
y = []

for ind_1, sublist in enumerate(results):
    for ind_2, ele in enumerate(sublist):
        if ele == 1:
            x.append(ind_1)
            y.append(ind_2)       


matplotlib.pyplot.scatter(x,y)

matplotlib.pyplot.show()

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.