python - 循环行以在 Python 中的坐标对之间绘制线(matplotlib)
问题描述
我对 Python 有点陌生,我正在努力使用 matplotlib 制作以下情节:
使用以下数据:
我有很多行,所以我想做类似的事情:
For each row, plot a line from (x_start, y_start) to (x_end, y_end)
有什么建议吗?
谢谢!
解决方案
You're on the right track. Import the data via something like pandas.read_csv()
. Here, I just put everything into an array to make this simple.
In [1]: import numpy as np
In [2]: import matplotlib.pyplot as plt
In [3]: x = np.array([[1, 1, 3, 1], [2, 2, 4, 2], [3, 3, 5, 3], [4, 4, 6, 4], [5, 5, 7, 5]])
In [4]: plt.figure()
...: for row in x:
...: plt.plot([row[0], row[2]], [row[1], row[3]], marker='.')
...:
In [5]: plt.show()
I leave the axis labels and the colors to you. Hint: look into matplotlib's colormaps.