Scatter plots in Pandas/Pyplot: How to plot by category

前端 未结 8 1004
孤城傲影
孤城傲影 2020-11-22 10:53

I am trying to make a simple scatter plot in pyplot using a Pandas DataFrame object, but want an efficient way of plotting two variables but have the symbols dictated by a t

8条回答
  •  孤城傲影
    2020-11-22 11:00

    With plt.scatter, I can only think of one: to use a proxy artist:

    df = pd.DataFrame(np.random.normal(10,1,30).reshape(10,3), index = pd.date_range('2010-01-01', freq = 'M', periods = 10), columns = ('one', 'two', 'three'))
    df['key1'] = (4,4,4,6,6,6,8,8,8,8)
    fig1 = plt.figure(1)
    ax1 = fig1.add_subplot(111)
    x=ax1.scatter(df['one'], df['two'], marker = 'o', c = df['key1'], alpha = 0.8)
    
    ccm=x.get_cmap()
    circles=[Line2D(range(1), range(1), color='w', marker='o', markersize=10, markerfacecolor=item) for item in ccm((array([4,6,8])-4.0)/4)]
    leg = plt.legend(circles, ['4','6','8'], loc = "center left", bbox_to_anchor = (1, 0.5), numpoints = 1)
    

    And the result is:

    enter image description here

提交回复
热议问题