Scatterplot with different size, marker, and color from pandas dataframe

前端 未结 2 476
离开以前
离开以前 2020-12-17 00:09

I am trying to do a scatter plot with speed over meters for each point where marker indicate different types, size indicate different weights and color indicate how old a po

2条回答
  •  無奈伤痛
    2020-12-17 00:36

    scatter can only do one kind of marker at a time, so you have to plot the different types separately. Fortunately pandas makes this easy:

    import matplotlib.pyplot as plt
    import pandas as pd
    x = {'speed': [10, 15, 20, 18, 19],
         'meters' : [122, 150, 190, 230, 300],
         'type': ['phone', 'phone', 'gps', 'gps', 'car'],
         'weight': [0.2, 0.3, 0.1, 0.85, 0.0],
         'old': [1, 2, 4, 5, 8]}
    
    m = pd.DataFrame(x)
    mkr_dict = {'gps': 'x', 'phone': '+', 'car': 'o'}
    for kind in mkr_dict:
        d = m[m.type==kind]
        plt.scatter(d.meters, d.speed, 
                    s = 100* d.weight, 
                    c = d.old, 
                    marker = mkr_dict[kind])
    plt.show()
    

    enter image description here

    .... Where's the car? Well, the weight is 0.0 in the original test data, and we're using weight for marker-size, so: can't see it.

提交回复
热议问题