Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Draw graph (Python)
#1
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
Reply
#2
#!/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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Photo How to do a graph in Python? (and proper terminology) jpy 2 2,060 Dec-23-2020, 01:07 PM
Last Post: codeto
  Using a list as a Y value in a python graph Afterdarkreader 5 3,777 Dec-14-2017, 10:54 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020