scatter plot in matplotlib

后端 未结 1 1798
深忆病人
深忆病人 2021-02-03 23:18

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\

相关标签:
1条回答
  • 2021-02-03 23:58

    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 讨论(0)
提交回复
热议问题