【问题标题】:Overlay a scatter plot to a line plot in matplotlib在 matplotlib 中将散点图叠加到折线图上
【发布时间】:2021-07-11 18:33:53
【问题描述】:

美好的一天!我在 jupyter 上有这段代码

import pandas as pd
import matplotlib.pyplot

data = pd.read_csv("data.txt")

data.plot(x="Northings", y="Eastings")
data.plot.scatter(x="Northings", y="Eastings")

有没有办法将下面显示的这两个图结合起来?因为这在技术上是一块土地,我必须显示每个坐标的点。还是有更好的方法来解决这个问题?

如果需要这里是“data.txt”中包含的坐标

Station Northings   Eastings
1   10001.00    10001.00
2   10070.09    10004.57
3   10105.80    10001.70
4   10110.55    9964.66
5   10117.83    9908.10
6   10062.37    9893.94
7   10007.37    9902.18
8   10003.68    9943.23

【问题讨论】:

  • 您可以在 ax = data.plot(x="Northings", y="Eastings") 后跟 data.plot.scatter(x="Northings", y="Eastings", ax=ax) 将两者绘制在同一个 ax 上。在 matplotlib 中,ax 是子图的表示。

标签: python pandas matplotlib scatter-plot


【解决方案1】:

您应该能够使用 matplotlib 创建一个 Axes 对象,然后将该 Axes 对象传递给您的每个绘图方法,如下所示:

import pandas as pd
import matplotlib.pyplot as plt

data = pd.read_csv("data.txt")

fig, ax = plt.subplots()
data.plot(x="Northings", y="Eastings", ax=ax)
data.plot.scatter(x="Northings", y="Eastings", ax=ax)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-30
    • 2012-06-26
    • 1970-01-01
    • 2021-12-10
    • 1970-01-01
    • 2020-10-10
    • 2018-03-17
    相关资源
    最近更新 更多