【问题标题】:Matplotlib pyplot putting two plots side by side [duplicate]Matplotlib pyplot并排放置两个图[重复]
【发布时间】:2019-02-11 05:33:02
【问题描述】:

您好,我正在尝试将两个地块与 plt 并排放置,并在此处尝试了建议:Trying to position subplots next to each other

但是我正在使用 pandas 的默认功能进行绘图

myDataFrame.plot(kind='scatter' x='xcol', y='ycol') 

所以我不能像myDataFrame.subplot(2,1,2)那样做plt.subplot(2, 1, 2)(显然)

做事

plt.subplot(1, 2, 1)
myDataFrame.plot(kind='scatter' x='xcol', y='ycol') 

plt.subplot(1, 2, 2)
myDataFrame.plot(kind='scatter' x='xcol', y='ycol')

只需在我想要的情节之前添加两个情节

任何想法我如何仍然可以使用 myDataframe.plot(kind='scatter') 并将其中两个并排放置

【问题讨论】:

    标签: python pandas dataframe matplotlib


    【解决方案1】:

    如果您仍在寻找它,这里有一个最小的工作答案(不包括进口)

    fig, ax = plt.subplots(ncols=2, figsize=(10,4))
    
    df = pd.DataFrame([[5.1, 3.5, 0], [4.9, 3.0, 0], [7.0, 3.2, 1],
                    [6.4, 3.2, 1], [5.9, 3.0, 2], [7.1, 4.5, 2]],
                    columns=['X', 'Y', 'value'])
    df.plot.scatter(x='X', y='Y', c='red', ax=ax[0])
    df.plot.scatter(x='X', y='Y', c='red', ax=ax[1])
    

    输出

    【讨论】:

    • 感谢您的帮助
    【解决方案2】:

    看起来DataFrame.plot 为 pyplot 轴 https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.plot.html 使用了 ax 参数。 试试:

    ax = plt.subplot(1, 2, 1)
    myDataFrame.plot(kind='scatter' x='xcol', y='ycol', ax=ax) 
    

    【讨论】:

    • 非常有帮助,谢谢
    猜你喜欢
    • 2020-11-16
    • 1970-01-01
    • 2018-10-25
    • 1970-01-01
    • 2017-12-12
    • 1970-01-01
    • 1970-01-01
    • 2011-07-27
    • 1970-01-01
    相关资源
    最近更新 更多