【问题标题】:MatPlotLib: Multiple datasets on the same scatter plotMatPlotLib:同一个散点图上的多个数据集
【发布时间】:2011-05-15 07:02:23
【问题描述】:

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

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()

【问题讨论】:

  • 叠印在同一行。

标签: python scipy matplotlib


【解决方案1】:

我遇到了这个问题,因为我遇到了完全相同的问题。虽然接受的答案效果很好,但使用 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()

【讨论】:

  • 我可以对第三个散点图使用相同的方法吗?比如,plt.scatter(x, y, c='g', marker='o', label='-2')
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    您需要引用 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()
    

    【讨论】:

    • 111 中的fig.add_subplot(111) 是什么意思?
    • 是这个图中子图的排列方式。第一个数字是子图的行数;第二个数字是子图的列数;第三个数字是您现在正在谈论的子图。在这种情况下,有一行一列的子图(即一个子图),并且轴正在谈论它们中的第一个。类似 fig.add_subplot(3,2,5) 的东西将是三行两列网格中左下角的子图。
    • 如果我需要绘制三个散点图而不是此处显示的两个?
    • @NixMan 没有任何变化,它使用相同的设置
    【解决方案4】:

    我不知道,它对我来说很好用。确切的命令:

    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()
    

    【讨论】:

    • 我的数据集重叠了 :)
    猜你喜欢
    • 2019-01-14
    • 2019-11-26
    • 1970-01-01
    • 1970-01-01
    • 2013-01-04
    • 1970-01-01
    • 2019-08-20
    • 1970-01-01
    相关资源
    最近更新 更多