1

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.

The input file :

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

and this is my code:

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

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

user252935
  • 317
  • 3
  • 15
  • Actually, every point is x and y coordinate of a particle and I want to show every particle's velocity with a triangle pointing toward the direction of velocity, and by the "angle" I mean from horizontal axis. – user252935 Dec 10 '16 at 17:52

2 Answers2

2

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

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
p-robot
  • 4,652
  • 2
  • 29
  • 38
  • 2
    Exactly, this can be seen [here](http://stackoverflow.com/questions/26490817/matplotlib-scatter-plot-with-different-markers-and-colors), [here](http://stackoverflow.com/questions/28706115/how-to-use-different-marker-for-different-point-in-scatter-plot-pylab) or [here](http://stackoverflow.com/questions/16774586/python-scatter-plot-conditions-for-marker-styles) – ImportanceOfBeingErnest Dec 10 '16 at 18:03
1

Just in case you are not aware, you can use quiver to plot 2D fields:

x = [1, 1.2, 1.3]
y = [1, 1.2, 1.3]
angle = [10, 20, 30]

plt.quiver(x, y, np.cos(np.radians(angle)), np.sin(np.radians(angle)))

enter image description here

Stop harming Monica
  • 12,141
  • 1
  • 36
  • 56