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

前端 未结 8 928
孤城傲影
孤城傲影 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:18

    You can also try Altair or ggpot which are focused on declarative visualisations.

    import numpy as np
    import pandas as pd
    np.random.seed(1974)
    
    # Generate Data
    num = 20
    x, y = np.random.random((2, num))
    labels = np.random.choice(['a', 'b', 'c'], num)
    df = pd.DataFrame(dict(x=x, y=y, label=labels))
    

    Altair code

    from altair import Chart
    c = Chart(df)
    c.mark_circle().encode(x='x', y='y', color='label')
    

    ggplot code

    from ggplot import *
    ggplot(aes(x='x', y='y', color='label'), data=df) +\
    geom_point(size=50) +\
    theme_bw()
    

提交回复
热议问题