【问题标题】:Multiple scatter plot on same axis同一轴上的多个散点图
【发布时间】:2019-11-26 13:21:18
【问题描述】:

我有 3 个散点图,想知道如何将这 3 个组合成 1 个大散点图。

我似乎无法仅使用 Matplotlib 找到解决此特定问题的方法。

x1 = np.random.randint(40, 100, 50)
y1 = np.random.randint(40, 100, 50)

x2 = np.random.randint(0, 60, 50)
y2 = np.random.randint(0, 60, 50)

x3 = np.random.randint(40, 100, 50)
y3 = np.random.randint(0, 60, 50)

fig, (plot1, plot2, plot3, plot4) = plt.subplots(1, 4)

plot1.scatter(x1,y1, color='green', s = 10)
plot1.set(xlim=(0, 100), ylim=(0, 100))

plot2.scatter(x2,y2, color='red', s = 10)
plot2.set(xlim=(0, 100), ylim=(0, 100))

plot3.scatter(x3,y3, color='blue', s = 10)
plot3.set(xlim=(0, 100), ylim=(0, 100))

plot4.scatter(plot1, plot2, plot3)

所以我希望 plot4 是 plot1、plot2 和 plot3 的组合。

【问题讨论】:

    标签: python matplotlib data-visualization scatter-plot


    【解决方案1】:

    如果plot4的颜色可能与原来的颜色不同,你可以这样做:

    fig, (plot1, plot2, plot3, plot4) = plt.subplots(1, 4)
    
    plot1.scatter(x1,y1, color='green', s = 10)
    plot1.set(xlim=(0, 100), ylim=(0, 100))
    
    plot2.scatter(x2,y2, color='red', s = 10)
    plot2.set(xlim=(0, 100), ylim=(0, 100))
    
    plot3.scatter(x3,y3, color='blue', s = 10)
    plot3.set(xlim=(0, 100), ylim=(0, 100))
    
    plot4.scatter([x1, x2, x3], [y1, y2, y3], color=('violet'), s = 10)
    

    但如果您希望所有地块的颜色与三个子地块中的颜色相同,您可以按照 Brendan 的建议进行操作

    【讨论】:

      【解决方案2】:

      plot4 上简单地绘制每个原始图:

      plot4.scatter(x1,y1, color='green', s = 10)
      plot4.scatter(x2,y2, color='red', s = 10)
      plot4.scatter(x3,y3, color='blue', s = 10)
      

      或者,您可以使用np.concatenate() 组合每个xy 数组以仅调用一次绘图命令,但这会失去为每个子组独立着色的能力。

      plot4.scatter(np.concatenate((x1,x2,x3)), 
                    np.concatenate((y1,y2,y3)), 
                    color='black', s=10)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-05-15
        • 1970-01-01
        • 1970-01-01
        • 2013-12-31
        • 2017-02-24
        • 2013-02-01
        • 2018-06-19
        相关资源
        最近更新 更多