代码之家  ›  专栏  ›  技术社区  ›  Austin Richardson

MatPlotLib:同一散点图上有多个数据集

  •  56
  • Austin Richardson  · 技术社区  · 14 年前

    我想在同一散点图上绘制多个数据集:

    cases = scatter(x[:4], y[:4], s=10, c='b', marker="s")
    controls = scatter(x[4:], y[4:], s=10, c='r', marker="o")
    
    show()
    

    以上仅显示最近的 scatter()

    我也试过:

    plt = subplot(111)
    plt.scatter(x[:4], y[:4], s=10, c='b', marker="s")
    plt.scatter(x[4:], y[4:], s=10, c='r', marker="o")
    show()
    
    4 回复  |  直到 14 年前
        1
  •  99
  •   Renaud    11 年前

    你需要一份 Axes 对象在同一子块上继续绘制。

    import matplotlib.pyplot as plt
    
    x = range(100)
    y = range(100,200)
    fig = plt.figure()
    ax1 = fig.add_subplot(111)
    
    ax1.scatter(x[:4], y[:4], s=10, c='b', marker="s", label='first')
    ax1.scatter(x[40:],y[40:], s=10, c='r', marker="o", label='second')
    plt.legend(loc='upper left');
    plt.show()
    

    enter image description here

        2
  •  17
  •   Sohaib Farooqi    7 年前

    我遇到这个问题是因为我有同样的问题。虽然接受的答案很好,但是使用matplotlib版本 2.1.0 ,在一个图中有两个散点图而不使用 Axes

    import matplotlib.pyplot as plt
    
    plt.scatter(x,y, c='b', marker='x', label='1')
    plt.scatter(x, y, c='r', marker='s', label='-1')
    plt.legend(loc='upper left')
    plt.show()
    
        3
  •  6
  •   Steve Tjoa    14 年前

    我不知道,这对我很好。精确命令:

    import scipy, pylab
    ax = pylab.subplot(111)
    ax.scatter(scipy.randn(100), scipy.randn(100), c='b')
    ax.scatter(scipy.randn(100), scipy.randn(100), c='r')
    ax.figure.show()
    
        4
  •  1
  •   MaVe    8 年前

    如果您的数据是在数据帧中表示的,那么您也可以在Pandas中轻松地执行此操作,如下所述:

    http://pandas.pydata.org/pandas-docs/version/0.15.0/visualization.html#scatter-plot