scatter plot in matplotlib

2020-05-23 03:36发布

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?

1条回答
可以哭但决不认输i
2楼-- · 2020-05-23 04:02

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.

查看更多
登录 后发表回答