matplotlib:从文件中读取标记方向 [英] matplotlib: read marker direction from a file

查看:52
本文介绍了matplotlib:从文件中读取标记方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 matplotlib.pyplot 从数据文件中绘制一个图,我希望每个标记(三角形)都有一个在输入文件中给出的方向.

I want to make a plot from a data file with matplotlib.pyplot and I want every marker (triangle) to have an orientation which has been given in the input file.

输入文件:

    x   y   angle
    1   1   10  
    1.2 1.2 20  
    1.3 1.3 30

这是我的代码:

import numpy as np
import matplotlib.pyplot as plt




infile = open ('traj_marker.txt')

#for s in xrange(8):
x, y = [], []
m = []
for i in xrange(3):
        data = infile.readline()
        raw = data.split()
        x.append(float(raw[0]))
        y.append(float(raw[1]))
        m.append(float(raw[2]))

xnp = np.array(x)
ynp = np.array(y)
mnp = np.array(m)

fig, ax = plt.subplots()
ax.scatter(xnp, ynp, 100, marker = (3,0,mnp))
plt.xticks (range(1,3))
plt.yticks (range(1,3))
plt.grid()
fig.savefig ('trj.png')
infile.close()

但是标记中数组 mnp 的存在会产生错误.我该如何解决?

But the presence of array mnp in marker produces error. How can I solve this?

推荐答案

Matplotlib不喜欢作为列表传递的marker参数,因此请按以下方式运行它...

Matplotlib doesn't like the marker argument passed as a list, so run it in the following manner ...

N = 20
xnp = np.random.rand(N)
ynp = np.random.rand(N)
mnp = np.random.randint(0, 180, N)

fig, ax = plt.subplots()
for x, y, m in zip(xnp, ynp, mnp):
    ax.scatter(x, y, 100, marker = (3,0,m))
plt.show()

这篇关于matplotlib:从文件中读取标记方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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