Grid plotting in Python with coordinates and a map image












1















I'm fairly new to using python and I'm seeking help on a small project I'm working on.



So firstly I want to make a 10x10 grid out of coordinates and then add a loop function that can randomly pick a cell and somehow store the cell coordinates it has picked without selecting the same cell twice.



So far I have come up with this.



x = 1
y = 1

scale = 10

nn =

for x in range(1,scale+1):
mm =
for y in range(1,scale+1):
xy = [x,y]
mm.append(xy)
#print(xy)
y=+1
nn.append(mm)
x=+1

Out:[[[1, 1],
[1, 2],
[1, 3],
[1, 4],
[1, 5],
[1, 6],
etc


The next part is where I'm struggling.



import random
r = random.randint(1,10)
x = 1
y = r

xy = [x,y]
print(xy)
while x < 10:

# direction North=1, East=2, South=3
if y == 1:
dir = [random.randint(0,1),random.randint(0,1)]
elif y == 10:
dir = [random.randint(0,1),random.randint(-1,0)]
else:
dir = [random.randint(0,1),random.randint(-1,1)]

xy = [(a + b) for (a, b) in zip(xy, dir)]

x = xy[0]
y = xy[1]

if xy == [(a + b) for (a, b) in zip(xy, dir)]:
pass
else:
print(xy)


Eventually I would like to plot the coordinates onto a grid and then put a map image over the grid.



This is my first question so please excuse mistakes and code quotes.
Thanks in advance










share|improve this question


















  • 1





    I'm not completely following how each code block relates to each other. What I think you're looking for is a set. docs.python.org/3/tutorial/datastructures.html#sets . On each loop, check the set if the new value exists. if not, add it, else try again.

    – J'e
    Nov 22 '18 at 17:19


















1















I'm fairly new to using python and I'm seeking help on a small project I'm working on.



So firstly I want to make a 10x10 grid out of coordinates and then add a loop function that can randomly pick a cell and somehow store the cell coordinates it has picked without selecting the same cell twice.



So far I have come up with this.



x = 1
y = 1

scale = 10

nn =

for x in range(1,scale+1):
mm =
for y in range(1,scale+1):
xy = [x,y]
mm.append(xy)
#print(xy)
y=+1
nn.append(mm)
x=+1

Out:[[[1, 1],
[1, 2],
[1, 3],
[1, 4],
[1, 5],
[1, 6],
etc


The next part is where I'm struggling.



import random
r = random.randint(1,10)
x = 1
y = r

xy = [x,y]
print(xy)
while x < 10:

# direction North=1, East=2, South=3
if y == 1:
dir = [random.randint(0,1),random.randint(0,1)]
elif y == 10:
dir = [random.randint(0,1),random.randint(-1,0)]
else:
dir = [random.randint(0,1),random.randint(-1,1)]

xy = [(a + b) for (a, b) in zip(xy, dir)]

x = xy[0]
y = xy[1]

if xy == [(a + b) for (a, b) in zip(xy, dir)]:
pass
else:
print(xy)


Eventually I would like to plot the coordinates onto a grid and then put a map image over the grid.



This is my first question so please excuse mistakes and code quotes.
Thanks in advance










share|improve this question


















  • 1





    I'm not completely following how each code block relates to each other. What I think you're looking for is a set. docs.python.org/3/tutorial/datastructures.html#sets . On each loop, check the set if the new value exists. if not, add it, else try again.

    – J'e
    Nov 22 '18 at 17:19
















1












1








1








I'm fairly new to using python and I'm seeking help on a small project I'm working on.



So firstly I want to make a 10x10 grid out of coordinates and then add a loop function that can randomly pick a cell and somehow store the cell coordinates it has picked without selecting the same cell twice.



So far I have come up with this.



x = 1
y = 1

scale = 10

nn =

for x in range(1,scale+1):
mm =
for y in range(1,scale+1):
xy = [x,y]
mm.append(xy)
#print(xy)
y=+1
nn.append(mm)
x=+1

Out:[[[1, 1],
[1, 2],
[1, 3],
[1, 4],
[1, 5],
[1, 6],
etc


The next part is where I'm struggling.



import random
r = random.randint(1,10)
x = 1
y = r

xy = [x,y]
print(xy)
while x < 10:

# direction North=1, East=2, South=3
if y == 1:
dir = [random.randint(0,1),random.randint(0,1)]
elif y == 10:
dir = [random.randint(0,1),random.randint(-1,0)]
else:
dir = [random.randint(0,1),random.randint(-1,1)]

xy = [(a + b) for (a, b) in zip(xy, dir)]

x = xy[0]
y = xy[1]

if xy == [(a + b) for (a, b) in zip(xy, dir)]:
pass
else:
print(xy)


Eventually I would like to plot the coordinates onto a grid and then put a map image over the grid.



This is my first question so please excuse mistakes and code quotes.
Thanks in advance










share|improve this question














I'm fairly new to using python and I'm seeking help on a small project I'm working on.



So firstly I want to make a 10x10 grid out of coordinates and then add a loop function that can randomly pick a cell and somehow store the cell coordinates it has picked without selecting the same cell twice.



So far I have come up with this.



x = 1
y = 1

scale = 10

nn =

for x in range(1,scale+1):
mm =
for y in range(1,scale+1):
xy = [x,y]
mm.append(xy)
#print(xy)
y=+1
nn.append(mm)
x=+1

Out:[[[1, 1],
[1, 2],
[1, 3],
[1, 4],
[1, 5],
[1, 6],
etc


The next part is where I'm struggling.



import random
r = random.randint(1,10)
x = 1
y = r

xy = [x,y]
print(xy)
while x < 10:

# direction North=1, East=2, South=3
if y == 1:
dir = [random.randint(0,1),random.randint(0,1)]
elif y == 10:
dir = [random.randint(0,1),random.randint(-1,0)]
else:
dir = [random.randint(0,1),random.randint(-1,1)]

xy = [(a + b) for (a, b) in zip(xy, dir)]

x = xy[0]
y = xy[1]

if xy == [(a + b) for (a, b) in zip(xy, dir)]:
pass
else:
print(xy)


Eventually I would like to plot the coordinates onto a grid and then put a map image over the grid.



This is my first question so please excuse mistakes and code quotes.
Thanks in advance







python matplotlib grid






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 17:09









Kam-ALIENKam-ALIEN

42




42








  • 1





    I'm not completely following how each code block relates to each other. What I think you're looking for is a set. docs.python.org/3/tutorial/datastructures.html#sets . On each loop, check the set if the new value exists. if not, add it, else try again.

    – J'e
    Nov 22 '18 at 17:19
















  • 1





    I'm not completely following how each code block relates to each other. What I think you're looking for is a set. docs.python.org/3/tutorial/datastructures.html#sets . On each loop, check the set if the new value exists. if not, add it, else try again.

    – J'e
    Nov 22 '18 at 17:19










1




1





I'm not completely following how each code block relates to each other. What I think you're looking for is a set. docs.python.org/3/tutorial/datastructures.html#sets . On each loop, check the set if the new value exists. if not, add it, else try again.

– J'e
Nov 22 '18 at 17:19







I'm not completely following how each code block relates to each other. What I think you're looking for is a set. docs.python.org/3/tutorial/datastructures.html#sets . On each loop, check the set if the new value exists. if not, add it, else try again.

– J'e
Nov 22 '18 at 17:19














1 Answer
1






active

oldest

votes


















0














From my understanding, you're trying to get non repetitive random sample from a grid of coordinates, This can be done much more easily like this:



import random
grid = [(x,y) for x in range(10) for y in range(10)] # grid points
randomset = random.sample(grid, 5) # get 5 random element from grid


Result:



>>> randomset
[(6, 6), (4, 7), (7, 7), (1, 7), (0, 3)] # each coordinate will be unique.





share|improve this answer
























  • Many thanks for your answer it did indeed help! Can you help me plot the x,y onto a 10x10 grid like format. Thank you!

    – Kam-ALIEN
    Nov 23 '18 at 10:55













  • 2D grid for visualisation. grid_array = np.array(grid) -- grid=np.meshgrid(grid_array)

    – Kam-ALIEN
    Nov 23 '18 at 11:19













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435630%2fgrid-plotting-in-python-with-coordinates-and-a-map-image%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














From my understanding, you're trying to get non repetitive random sample from a grid of coordinates, This can be done much more easily like this:



import random
grid = [(x,y) for x in range(10) for y in range(10)] # grid points
randomset = random.sample(grid, 5) # get 5 random element from grid


Result:



>>> randomset
[(6, 6), (4, 7), (7, 7), (1, 7), (0, 3)] # each coordinate will be unique.





share|improve this answer
























  • Many thanks for your answer it did indeed help! Can you help me plot the x,y onto a 10x10 grid like format. Thank you!

    – Kam-ALIEN
    Nov 23 '18 at 10:55













  • 2D grid for visualisation. grid_array = np.array(grid) -- grid=np.meshgrid(grid_array)

    – Kam-ALIEN
    Nov 23 '18 at 11:19


















0














From my understanding, you're trying to get non repetitive random sample from a grid of coordinates, This can be done much more easily like this:



import random
grid = [(x,y) for x in range(10) for y in range(10)] # grid points
randomset = random.sample(grid, 5) # get 5 random element from grid


Result:



>>> randomset
[(6, 6), (4, 7), (7, 7), (1, 7), (0, 3)] # each coordinate will be unique.





share|improve this answer
























  • Many thanks for your answer it did indeed help! Can you help me plot the x,y onto a 10x10 grid like format. Thank you!

    – Kam-ALIEN
    Nov 23 '18 at 10:55













  • 2D grid for visualisation. grid_array = np.array(grid) -- grid=np.meshgrid(grid_array)

    – Kam-ALIEN
    Nov 23 '18 at 11:19
















0












0








0







From my understanding, you're trying to get non repetitive random sample from a grid of coordinates, This can be done much more easily like this:



import random
grid = [(x,y) for x in range(10) for y in range(10)] # grid points
randomset = random.sample(grid, 5) # get 5 random element from grid


Result:



>>> randomset
[(6, 6), (4, 7), (7, 7), (1, 7), (0, 3)] # each coordinate will be unique.





share|improve this answer













From my understanding, you're trying to get non repetitive random sample from a grid of coordinates, This can be done much more easily like this:



import random
grid = [(x,y) for x in range(10) for y in range(10)] # grid points
randomset = random.sample(grid, 5) # get 5 random element from grid


Result:



>>> randomset
[(6, 6), (4, 7), (7, 7), (1, 7), (0, 3)] # each coordinate will be unique.






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 17:46









Rocky LiRocky Li

3,4531719




3,4531719













  • Many thanks for your answer it did indeed help! Can you help me plot the x,y onto a 10x10 grid like format. Thank you!

    – Kam-ALIEN
    Nov 23 '18 at 10:55













  • 2D grid for visualisation. grid_array = np.array(grid) -- grid=np.meshgrid(grid_array)

    – Kam-ALIEN
    Nov 23 '18 at 11:19





















  • Many thanks for your answer it did indeed help! Can you help me plot the x,y onto a 10x10 grid like format. Thank you!

    – Kam-ALIEN
    Nov 23 '18 at 10:55













  • 2D grid for visualisation. grid_array = np.array(grid) -- grid=np.meshgrid(grid_array)

    – Kam-ALIEN
    Nov 23 '18 at 11:19



















Many thanks for your answer it did indeed help! Can you help me plot the x,y onto a 10x10 grid like format. Thank you!

– Kam-ALIEN
Nov 23 '18 at 10:55







Many thanks for your answer it did indeed help! Can you help me plot the x,y onto a 10x10 grid like format. Thank you!

– Kam-ALIEN
Nov 23 '18 at 10:55















2D grid for visualisation. grid_array = np.array(grid) -- grid=np.meshgrid(grid_array)

– Kam-ALIEN
Nov 23 '18 at 11:19







2D grid for visualisation. grid_array = np.array(grid) -- grid=np.meshgrid(grid_array)

– Kam-ALIEN
Nov 23 '18 at 11:19






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435630%2fgrid-plotting-in-python-with-coordinates-and-a-map-image%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini