如何使用Matplotlib绘制线段或向量 [英] How to plot line segments or vectors with matplotlib

查看:159
本文介绍了如何使用Matplotlib绘制线段或向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含4列的文件,该列的意思是,起点的坐标(x1,y1),终点的坐标(x2,y2).我需要绘制很多连接这两个点的线.它有不同的 y 值,x 值是旁边的 xtics 的数量.你能给我推荐一个在同一个图中绘制这些线的循环吗?

2 11.6414 3 9.23953 9.23494 4 8.437973 9.2395 4 8.437971 6.46786 2 1.692411 8.76289 2 1.692411 8.76289 2 7.049542 11.6414 3 9.23953 9.2395 4 8.437974 10.3475 5 9.691174 10.7528 5 9.691174 10.7528 5 10.35764 11.0156 5 9.691175 11.199 6 11.0211 6.46786 2 1.692411 8.76289 2 1.692414 11.3245 5 11.1995 11.199 6 11.0216 11.021 5 9.691176 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 hodnotdfr ['dy'] = dfr.y2-dfr.y1#rozdíly-ovýchhodnotq = ax.quiver(dfr.x1,dfr.y1,dfr.dx,dfr.dy,单位='xy',比例= 1)ax.set_aspect('相等')plt.xlim(0,6)plt.ylim(0, 12)

解决方案

使用

  • 此图显示了超过 1600 个向量的整个数据集

I have a file with 4 columns that mean, coordinates (x1, y1) for start point, (x2, y2) for end point. I need to plot lots of lines that connect these two points. It has different y values, and x values are numbers of xtics next to. Could you recommend me a cycle for plotting these lines in the same 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

After advise I have

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)

解决方案

Use matplotlib.pyplot.quiver:

  • Using quiver requires calculating dx and 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

Plot:

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()

  • This plot shows the entire dataset of more 1600 vectors

这篇关于如何使用Matplotlib绘制线段或向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆