【问题标题】:Plotting boolean dataframes with a condition绘制带有条件的布尔数据框
【发布时间】:2016-03-08 14:56:40
【问题描述】:

我有两个数据框,如下所示:

test1 = pd.DataFrame({'A':[False, False, False, True], 'B':[False, False, True, False], 'C':[True, False, True, False]})

产生:

      A       B      C
0   False   False   True
1   False   False   False
2   False   True    True
3   True    False   False

test2 = pd.DataFrame({'A':[False, False, False, False], 'B':[True, False, True, False], 'C':[False, False, False, False]})

产生:

      A      B        C
0   False   True    False
1   False   False   False
2   False   True    False
3   False   False   False

我想以 y 轴是列名,x 轴是索引的方式将这些数据框绘制在一个图中。我想在对应的值为True 的地方有一个黄色 点。如果两个数据帧中的对应值为True,那么我想要一个red点。

非常感谢任何帮助。

【问题讨论】:

    标签: python pandas matplotlib


    【解决方案1】:

    我做一个图表:x轴表示列索引(ABC),y轴表示行索引。这是你想做的吗?

    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    
    test1 = pd.DataFrame({'A':[False, False, False, True], 'B':[False, False, True, False], 'C':[True, False, True, False]})
    test2 = pd.DataFrame({'A':[False, False, False, False], 'B':[True, False, True, False], 'C':[False, False, False, False]})
    
    
    # Make a matrix whose elements are 0 if both are True, 1 if neither True, else 2.
    mat = np.vectorize( lambda a,b: 0 if a and b else 1 if a or b else 2)(test1,test2) 
    
    y,x = np.where(mat==0)
    plt.plot(x, y, 'ro')
    y,x = np.where(mat==1)
    plt.plot(x, y, 'yo')
    
    xlabels = test1.columns
    
    plt.ylim(-1,len(test1.index))
    plt.xlim(-1,len(xlabels))
    
    plt.xticks(range(len(xlabels)), xlabels)
    
    plt.show()
    

    【讨论】:

    • @takoika 如果绘制的子弹大小(直径)根据你有多少正确匹配而改变,那就太好了。例如,当您获得更多“真实”和“真实”补丁时,图表中的这个点会更大。因此,您不仅可以查看对应的值是真还是假,还可以查看您有多少匹配项......您也可以添加吗?
    猜你喜欢
    • 1970-01-01
    • 2016-09-19
    • 1970-01-01
    • 2022-07-07
    • 2017-01-01
    • 1970-01-01
    • 2018-09-01
    • 2016-02-26
    • 2023-03-26
    相关资源
    最近更新 更多