I am truly a novice in Python. Now, I am doing a project which involves creating a list of 2D coordinates. The coordinates should be uniformly placed, using a square grid (10*10), like(0,0)(0,1)(0,2)(0,3)...(0,10)(1,0)(1,2)(1,3)...(2,0)(2,1)(2,2)...(10,10).
Here is my code:
coordinate = []
x = 0
y = 0
while y < 10:
while x < 10:
coordinate.append((x,y))
x += 1
coordinate.append((x,y))
y += 1
print(coordinate)
But I can only get: [(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (5, 0), (6, 0), (7, 0), (8, 0), (9, 0), (10, 0), (10, 1), (10, 2), (10, 3), (10, 4), (10, 5), (10, 6), (10, 7), (10, 8), (10, 9)]
How can I rewrite my code to get all the points?
Here plot method is used for plotting and the show is for showing output to the user. Now if there are given two coordinates to draw a line, firstly we have to make two different arrays for the values of x and y respectively and use that x and y value array in the plot function of matplotlib to draw the line for the corresponding coordinates.
We will pass on a function inside the function to find the area using the coordinates of the vertices. Follow the code to define the function in python. Code Explanation: Just like how we did in the previous step, we are defining a function ‘find_triangle_area’ that takes the coordinates of vertices as parameters.
coordinate = [] for y in range(0, 11): for x in range(0, 11): coordinate.append((x,y)) print(coordinate) For more information on for loops in Python, check out the official wiki page.
The basic steps to create 2D pixel plots in python using Matplotlib are as follows: We are importing NumPy library for creating a dataset and a ‘pyplot’ module from a matplotlib library for plotting pixel plots For plotting, we need 2-dimensional data. Let’s create a 2d array using the random method in NumPy.
It's common to use a couple of for-loops to achieve this:
coordinates = []
for x in range(11):
for y in range(11):
coordinates.append((x, y))
It's also common to simplify this by flattening it into a list comprehension:
coordinates = [(x,y) for x in range(11) for y in range(11)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With