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

前端 未结 8 909
孤城傲影
孤城傲影 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 10:57

    seaborn has a wrapper function scatterplot that does it more efficiently.

    sns.scatterplot(data = df, x = 'one', y = 'two', data =  'key1'])
    
    0 讨论(0)
  • 2020-11-22 10:58

    You can use df.plot.scatter, and pass an array to c= argument defining the color of each point:

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    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)
    colors = np.where(df["key1"]==4,'r','-')
    colors[df["key1"]==6] = 'g'
    colors[df["key1"]==8] = 'b'
    print(colors)
    df.plot.scatter(x="one",y="two",c=colors)
    plt.show()
    

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-11-22 11:04

    It's rather hacky, but you could use one1 as a Float64Index to do everything in one go:

    df.set_index('one').sort_index().groupby('key1')['two'].plot(style='--o', legend=True)
    

    Note that as of 0.20.3, sorting the index is necessary, and the legend is a bit wonky.

    0 讨论(0)
  • 2020-11-22 11:10

    This is simple to do with Seaborn (pip install seaborn) as a oneliner

    sns.scatterplot(x_vars="one", y_vars="two", data=df, hue="key1") :

    import seaborn as sns
    import pandas as pd
    import numpy as np
    np.random.seed(1974)
    
    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)
    
    sns.scatterplot(x="one", y="two", data=df, hue="key1")
    

    Here is the dataframe for reference:

    Since you have three variable columns in your data, you may want to plot all pairwise dimensions with:

    sns.pairplot(vars=["one","two","three"], data=df, hue="key1")
    

    https://rasbt.github.io/mlxtend/user_guide/plotting/category_scatter/ is another option.

    0 讨论(0)
  • 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()
    

    0 讨论(0)
提交回复
热议问题