【问题标题】:Matplotlib - how to prevent graph from backtracking [duplicate]Matplotlib - 如何防止图形回溯[重复]
【发布时间】:2020-04-28 22:42:48
【问题描述】:

我是 Matplotlib 的新手。如何防止我的图表回溯?注意我图表右上角的钩子。 X 轴是字符串,Y 轴是浮点数。

这是我的数据:

['192.168.0.1', 2.568]
['96.120.96.153', 14.139]
['96.110.232.169', 10.505]
['162.151.49.133', 11.446]
['68.86.90.225', 24.335]
['68.86.84.226', 23.631]
['68.86.83.94', 29.011]
['173.167.58.162', 35.688]
['209.58.57.17', 162.768]
['64.86.79.2', 187.42]
['64.86.21.104', 162.461]
['63.243.205.1', 166.525]
['120.29.217.66', 156.898]
['209.58.86.143', 156.785]
['120.29.217.66', 181.599]

以及对应的代码:

import matplotlib.pyplot as plt

# x axis values
a = []
# corresponding y axis values
b = []

for k in range(15):
    a.append(dataArray[k][0])
    b.append(dataArray[k][1])

# plotting the points
plt.plot(a, b)

# naming the x axis
plt.xlabel('Hop Addresses')

# naming the y axis
plt.ylabel('average time (ms)')

# giving a title to my graph
plt.title('Time vs. Hops')

# function to show the plot
plt.show()

【问题讨论】:

  • 你能发布代码和你的价值观吗?我怀疑您的 x 值不正确。
  • 没错,它们是不按顺序排列的字符串。我正在尝试绘制跟踪路由、时间与 IP 地址的关系图,所以我不希望它们按顺序排列。
  • 那么,IP地址是按访问顺序排列的,但时间与其他时间无关?
  • 好吧,如果它们不按顺序排列,它们会在某个时间点回到过去。有时候,在生活中你必须做出选择。

标签: python matplotlib


【解决方案1】:

您遇到的问题是plt.plot() 将绘制从一个数据点到下一个数据点的线。

从数据中可以明显看出,每个时间步都相互独立,但 IP 地址的顺序很重要。 plt.plot() 往往表示趋势。由于数据不是趋势,而是一系列独立事件,因此不同类型的图表可能更合适。在这种情况下,我建议使用条形图。而且由于 x 轴标签是捆绑在一起的,所以水平条形图会更好。

您会在下图中注意到120.29.217.66 出现了两次。每跳一次。为此,请对照列表中的索引而不是 IP 地址进行绘图,然后替换 y 轴标签。

import matplotlib.pyplot as plt

data = [
    ['192.168.0.1', 2.568],
    ['96.120.96.153', 14.139],
    ['96.110.232.169', 10.505],
    ['162.151.49.133', 11.446],
    ['68.86.90.225', 24.335],
    ['68.86.84.226', 23.631],
    ['68.86.83.94', 29.011],
    ['173.167.58.162', 35.688],
    ['209.58.57.17', 162.768],
    ['64.86.79.2', 187.42],
    ['64.86.21.104', 162.461],
    ['63.243.205.1', 166.525],
    ['120.29.217.66', 156.898],
    ['209.58.86.143', 156.785],
    ['120.29.217.66', 181.599],
]

idxs = range(len(data))
ips = [i[0] for i in data]
times = [i[1] for i in data]

plt.barh(idxs, times)  # plot times vs the index of the array

plt.ylabel('Hop Addresses')
plt.xlabel('average time (ms)')
plt.title('Time vs. Hops')

plt.yticks(idxs, ips)  # Replace tick labels with IP Addresses

plt.tight_layout()
plt.show()

由于我们倾向于从上到下阅读,您可以随时flip the y-axis

plt.gca().invert_yaxis()

【讨论】:

  • 天哪,是的,我应该注意到我有两个相同的地址。现在这一切都说得通了。感谢您的关注,并感谢您提供格式建议!
【解决方案2】:

一条线按顺序连接点。您需要数据的顺序正确,才能使折线图有意义。我们也可以只使用索引作为“x”值。例如,对于您的数据如下

data = [['192.168.0.1', 2.568],
        ['96.120.96.153', 14.139],
        ['96.110.232.169', 10.505],
        ['162.151.49.133', 11.446],
        ['68.86.90.225', 24.335],
        ['68.86.84.226', 23.631],
        ['68.86.83.94', 29.011],
        ['173.167.58.162', 35.688],
        ['209.58.57.17', 162.768],
        ['64.86.79.2', 187.42],
        ['64.86.21.104', 162.461],
        ['63.243.205.1', 166.525],
        ['120.29.217.66', 156.898],
        ['209.58.86.143', 156.785],
        ['120.29.217.66', 181.599]]

我们只能得到traceroute提供的相同顺序的延迟。那么图表就会是正确的。

from matplotlib import pyplot as plt
plt.plot([i[1] for i in data]) 
plt.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-23
    • 1970-01-01
    • 2016-08-02
    相关资源
    最近更新 更多