Python Forum
Draw graph (Python) - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Draw graph (Python) (/thread-5965.html)



Draw graph (Python) - Eberle - Oct-30-2017

I have a text file that consists of 3 columns.

0.column contain X coordinate
1.column contain Y coordinate
2.column contain 0 or 1

So far I draw all the coordinates:

import matplotlib.pyplot as plt
import numpy as np

x, y = np.loadtxt("coordinates.txt",delimiter=' ',skiprows=1, usecols=(0,1),unpack=True)

plt.plot(x,y)
plt.show()
I want to draw only those coordinates where the value of 2rd column is 1.

Can you help me ?
Thanks


RE: Draw graph (Python) - heiner55 - May-28-2019

#!/usr/bin/python3
import matplotlib.pyplot as plt
import numpy as np

x,y,z = np.loadtxt("coordinates.txt", delimiter=' ', skiprows=1, unpack=True)
keep = z > 0
x = x[keep]
y = y[keep]

print(x)
print(y)

plt.plot(x,y)
plt.show()
#done