[英]How to plot line segments or vectors with matplotlib
我有一个包含 4 列的文件,这意味着坐标 (x1, y1) 为起点,坐标 (x2, y2) 为终点。 我需要 plot 很多线连接这两点。 它有不同的 y 值,x 值是旁边的 xtics 的数量。 你能推荐我一个循环来在同一个 plot 中绘制这些线吗?
2 11.6414 3 9.2395
3 9.23494 4 8.43797
3 9.2395 4 8.43797
1 6.46786 2 1.69241
1 8.76289 2 1.69241
1 8.76289 2 7.04954
2 11.6414 3 9.2395
3 9.2395 4 8.43797
4 10.3475 5 9.69117
4 10.7528 5 9.69117
4 10.7528 5 10.3576
4 11.0156 5 9.69117
5 11.199 6 11.021
1 6.46786 2 1.69241
1 8.76289 2 1.69241
4 11.3245 5 11.199
5 11.199 6 11.021
6 11.021 5 9.69117
6 11.021 5 10.3576
建议后我有
dfr = pd.read_csv('souradnice.csv')
dfr.columns = ['x1', 'y1', 'x2', 'y2']
dfr['dx'] = dfr.x2 - dfr.x1 # rozdíl x-ovových hodnot
dfr['dy'] = dfr.y2 - dfr.y1 # rozdíl y-ových hodnot
q = ax.quiver(dfr.x1, dfr.y1, dfr.dx, dfr.dy, units='xy', scale=1)
ax.set_aspect('equal')
plt.xlim(0, 6)
plt.ylim(0, 12)
matplotlib.pyplot.quiver
:dx
和dy
import pandas as pd
df = pd.read_csv('souradnice.csv',
header=None,
names=['x1', 'y1', 'x2', 'y2'],
dtype='float') # add sep=' ' if values are space separated
df['dx'] = df.x2 - df.x1
df['dy'] = df.y2 - df.y1
x1 y1 x2 y2 dx dy
2 11.64140 3 9.23950 1 -2.40190
3 9.23494 4 8.43797 1 -0.79697
3 9.23950 4 8.43797 1 -0.80153
1 6.46786 2 1.69241 1 -4.77545
1 8.76289 2 1.69241 1 -7.07048
1 8.76289 2 7.04954 1 -1.71335
2 11.64140 3 9.23950 1 -2.40190
3 9.23950 4 8.43797 1 -0.80153
4 10.34750 5 9.69117 1 -0.65633
4 10.75280 5 9.69117 1 -1.06163
4 10.75280 5 10.35760 1 -0.39520
4 11.01560 5 9.69117 1 -1.32443
5 11.19900 6 11.02100 1 -0.17800
1 6.46786 2 1.69241 1 -4.77545
1 8.76289 2 1.69241 1 -7.07048
4 11.32450 5 11.19900 1 -0.12550
5 11.19900 6 11.02100 1 -0.17800
6 11.02100 5 9.69117 -1 -1.32983
6 11.02100 5 10.35760 -1 -0.66340
quiver
文档,因为有许多参数可用于更改线条和箭头的外观。import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(10, 10))
q = ax.quiver(df.x1, df.y1, df.dx, df.dy, units='xy', scale=1)
plt.grid()
ax.set_aspect('equal')
plt.xlim(0, 6)
plt.ylim(0, 12)
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.