【问题标题】:Python MATPLOT scatter plotPython MATPLOTLIB 散点图
【发布时间】:2019-08-28 19:26:26
【问题描述】:

我有以下 x 和 y numpy 数组。我希望将它们绘制在一个图中,例如 XY 网格上的点或点。在 MATLAB 中,在每次调用绘图函数之后,我都会坚持下去,然后再向我展示整个绘图。

PYthon 的替代方案是什么?以下代码绘制了每个点,并且必须关闭图形,以便它可以在另一个图形上绘制下一个点。但是,我希望将所有点都放在同一张图上。

寻找有关如何使用 Python 实现这一目标的建议

import numpy as np
import matplotlib.pyplot as plt
a = 12; # Max length of spray wall; a is x direction in FEET
b = 12;  # Max width of spray wall; b is y direction in FEET
w = 1; # Width of spray pattern in INCHES
l = 12 ; # Length of spray pattern in INCHES
l = l/12; # IN FEET

ex = np.linspace(w/2, a - w/2, a*50); # THe resolution here is user preference or the speed
ey = np.linspace (l/2, b - l/2, int(2*b/l) -1); # The resolution here is based on 50 % overlap

for y in ey:
    for x in ex:
        plt.plot(x, y, '*')

        plt.show()

我不能使用 scatter,因为 ex 和 ey 的长度必须相同

【问题讨论】:

  • plt.show() 放在代码中要显示绘图的位置的所有循环之外。

标签: python matplotlib


【解决方案1】:

你可以附加两个列表,这样你就可以得到相同形状的 (x,y) 对:

point_x =[]
point_y =[]
for y in ey:
    for x in ex:
        point_x.append(x)
        point_y.append(y)

然后

plt.scatter (point_x, point_y)

或者说要将 plt.show 移出嵌套循环 但绘图需要时间

【讨论】: