matplotlib 中的散点图 [英] scatter plot in matplotlib
问题描述
这是我的第一个 matplotlib 程序,很抱歉我的无知.
This is my first matplotlib program, so sorry for my ignorance.
我有两个字符串数组.比如说,A = ['test1','test2']
和 B = ['test3','test4']
.如果 A
和 B
元素之间存在任何关联,则其corr值将设置为 1
.
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
现在,我想绘制一个散点图,其中我的 X 轴将是 A
的元素,Y 轴将是 B
的元素,如果相关值为 >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?
推荐答案
也许是这样的:
import matplotlib.pyplot
import pylab
x = [1,2,3,4]
y = [3,4,8,6]
matplotlib.pyplot.scatter(x,y)
matplotlib.pyplot.show()
让我看看我现在是否正确理解你:
Let me see if I understand you correctly now:
您有:
test1 | test2 | test3
test3 | 1 | 0 | 1
test4 | 0 | 1 | 0
test5 | 1 | 1 | 0
现在您要在散点图中表示上述值,以1表示一个点.
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()
请注意,我确实需要导入 pylab
,并且您可能会使用轴标签.此外,这感觉像是一种解决方法,并且可能(可能是)一种直接的方法来做到这一点.
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.
这篇关于matplotlib 中的散点图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!