【问题标题】:Plots different columns of different dataframe in one plot as scatter plot在一个图中绘制不同数据框的不同列作为散点图
【发布时间】:2020-01-16 18:25:36
【问题描述】:

我正在尝试在一个图中绘制来自不同数据帧的不同列(经度和纬度)。但它们分别以不同的数字绘制。

这是我正在使用的代码

fig,ax=plt.subplots()

cells_final.plot.scatter(x='lon',y='lat')
data_rupture.plot.scatter(x='Longitude',y='Latitude',color='red')
plt.show()

如何将其绘制在一个图中?

【问题讨论】:

标签: python pandas matplotlib scatter


【解决方案1】:

使用由

创建的axes实例(ax
fig, ax = plt.subplots()

并将其作为pandas.DataFrame.plotax参数传递,

fig,ax=plt.subplots()

cells_final.plot.scatter(x='lon',y='lat', ax=ax)
data_rupture.plot.scatter(x='Longitude',y='Latitude',color='red', ax=ax)
plt.show()

或者,如果您希望在同一图中的不同 subplots 上绘制图,您可以创建多个轴

fig, (ax1, ax2) = plt.subplots(1, 2)

cells_final.plot.scatter(x='lon',y='lat', ax=ax1)
data_rupture.plot.scatter(x='Longitude',y='Latitude',color='red', ax=ax2)
plt.show()

【讨论】:

    【解决方案2】:

    你需要指定轴:

    fig,ax=plt.subplots(1,2, figsize=(12, 8))
    
    cells_final.plot.scatter(x='lon',y='lat', ax=ax=[0])
    data_rupture.plot.scatter(x='Longitude',y='Latitude',color='red', ax=ax[1])
    plt.show()
    

    【讨论】:

    • 实际上,我想要两个散点图在一个绘图中的不同数据帧中具有不同颜色,而不是两个不同的子图
    【解决方案3】:

    感谢@William Miller.......!

    【讨论】:

      猜你喜欢
      • 2019-06-12
      • 1970-01-01
      • 2022-12-04
      • 1970-01-01
      • 1970-01-01
      • 2020-07-23
      • 2019-01-14
      • 1970-01-01
      • 2023-02-24
      相关资源
      最近更新 更多