简体   繁体   English

matplotlib中的散点图

[英]scatter plot in matplotlib

This is my first matplotlib program, so sorry for my ignorance. 这是我的第一个matplotlib程序,非常抱歉。

I've two arrays of string. 我有两个字符串数组。 say, A = ['test1','test2'] and B = ['test3','test4'] . 例如, A = ['test1','test2']B = ['test3','test4'] If any correlation exists between A and B element, their corr value will be set to 1 . 如果AB元素之间存在任何相关性,则其corr值将设置为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. 现在,我想绘制一个散点图,其中我的X轴将是A元素,Y轴将是B元素,并且如果相关值为1 ,它将在散点图中标记。 how to do that? 怎么做?

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. 现在,您要在散点图中表示上述值,以1表示一个点。

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. 请注意,我确实需要导入pylab ,您将可以使用轴标签。 Also this feels like a work around, and there might be (probably is) a direct method to do this. 同样,这感觉像是在变通,并且可能有(可能是)直接方法来执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM