matplotlib - randomly pick N points from 2D array, and plot spatial scatter plot
I have plots like the following:

Left Plot : original 100 * 100 numpy data
Right Plot : What I want - randomly choose N data from the original data, and plot them on a surface plot
How can I randomly choose N number of data from the left plot, and plot the chosen data on a scatter plot like the right plot?
I used ax.imshow(data) to generate the surface plot on the left. data is a 2D numpy array.
python matplotlib 2d surface
add a comment |
I have plots like the following:

Left Plot : original 100 * 100 numpy data
Right Plot : What I want - randomly choose N data from the original data, and plot them on a surface plot
How can I randomly choose N number of data from the left plot, and plot the chosen data on a scatter plot like the right plot?
I used ax.imshow(data) to generate the surface plot on the left. data is a 2D numpy array.
python matplotlib 2d surface
add a comment |
I have plots like the following:

Left Plot : original 100 * 100 numpy data
Right Plot : What I want - randomly choose N data from the original data, and plot them on a surface plot
How can I randomly choose N number of data from the left plot, and plot the chosen data on a scatter plot like the right plot?
I used ax.imshow(data) to generate the surface plot on the left. data is a 2D numpy array.
python matplotlib 2d surface
I have plots like the following:

Left Plot : original 100 * 100 numpy data
Right Plot : What I want - randomly choose N data from the original data, and plot them on a surface plot
How can I randomly choose N number of data from the left plot, and plot the chosen data on a scatter plot like the right plot?
I used ax.imshow(data) to generate the surface plot on the left. data is a 2D numpy array.
python matplotlib 2d surface
python matplotlib 2d surface
edited Nov 21 '18 at 0:53
Eric Kim
asked Nov 21 '18 at 0:41
Eric KimEric Kim
534418
534418
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
If you want to colorize the randomly chosen points according to the image you can use the same colormap and normalization for the scatter as you have for the image.
import numpy as np
import matplotlib.pyplot as plt
original_data = np.random.rand(100,100)
fig, (ax, ax2) = plt.subplots(ncols=2)
im = ax.imshow(original_data, cmap="summer")
N = 89
x = np.random.randint(0,100,size=N)
y = np.random.randint(0,100,size=N)
random_sample = original_data[x,y]
sc = ax2.scatter(x,y,c=random_sample, cmap=im.cmap, norm=im.norm)
ax2.set_aspect("equal")
ax2.set(xlim=ax.get_xlim(), ylim=ax.get_ylim())
fig.colorbar(sc, ax=[ax,ax2], orientation="horizontal")
plt.show()

you are the best! this is literally the perfect example i was looking for. thank you!!!
– Eric Kim
Nov 21 '18 at 1:16
add a comment |
You just need to choose N numbers from 10,000 (100 x 100) unique points on the 2d plot. I assume you want without replacement. Then you can "unravel" them onto your x,y coordinate.
random_choices = np.random.choice(10000, size=N, replace=False)
x, y = np.unravel_index(random_choices, (100, 100))
You can use these indices to create your scatter plot and size points appropriately:
data = np.random.random((100, 100))
plt.scatter(x, y, s=data[y, x])
i still don't know sure how to plot that indices on the surface plot.
– Eric Kim
Nov 21 '18 at 0:52
Well what you have on the right is really just a scatter plot. I don't know how your data is formatted so can't really tell you how to do it. But I will update with a generic execution.
– busybear
Nov 21 '18 at 0:56
just a scatter plot won't change the marker size based on the value of the data? and the data is just a 2-d numpy array
– Eric Kim
Nov 21 '18 at 0:58
You can use thesargument to change the size of data points.
– busybear
Nov 21 '18 at 1:01
I also made a change to choose without replacement, which I commented on originally but didn't actually show.
– busybear
Nov 21 '18 at 1:07
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403729%2fmatplotlib-randomly-pick-n-points-from-2d-array-and-plot-spatial-scatter-plot%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you want to colorize the randomly chosen points according to the image you can use the same colormap and normalization for the scatter as you have for the image.
import numpy as np
import matplotlib.pyplot as plt
original_data = np.random.rand(100,100)
fig, (ax, ax2) = plt.subplots(ncols=2)
im = ax.imshow(original_data, cmap="summer")
N = 89
x = np.random.randint(0,100,size=N)
y = np.random.randint(0,100,size=N)
random_sample = original_data[x,y]
sc = ax2.scatter(x,y,c=random_sample, cmap=im.cmap, norm=im.norm)
ax2.set_aspect("equal")
ax2.set(xlim=ax.get_xlim(), ylim=ax.get_ylim())
fig.colorbar(sc, ax=[ax,ax2], orientation="horizontal")
plt.show()

you are the best! this is literally the perfect example i was looking for. thank you!!!
– Eric Kim
Nov 21 '18 at 1:16
add a comment |
If you want to colorize the randomly chosen points according to the image you can use the same colormap and normalization for the scatter as you have for the image.
import numpy as np
import matplotlib.pyplot as plt
original_data = np.random.rand(100,100)
fig, (ax, ax2) = plt.subplots(ncols=2)
im = ax.imshow(original_data, cmap="summer")
N = 89
x = np.random.randint(0,100,size=N)
y = np.random.randint(0,100,size=N)
random_sample = original_data[x,y]
sc = ax2.scatter(x,y,c=random_sample, cmap=im.cmap, norm=im.norm)
ax2.set_aspect("equal")
ax2.set(xlim=ax.get_xlim(), ylim=ax.get_ylim())
fig.colorbar(sc, ax=[ax,ax2], orientation="horizontal")
plt.show()

you are the best! this is literally the perfect example i was looking for. thank you!!!
– Eric Kim
Nov 21 '18 at 1:16
add a comment |
If you want to colorize the randomly chosen points according to the image you can use the same colormap and normalization for the scatter as you have for the image.
import numpy as np
import matplotlib.pyplot as plt
original_data = np.random.rand(100,100)
fig, (ax, ax2) = plt.subplots(ncols=2)
im = ax.imshow(original_data, cmap="summer")
N = 89
x = np.random.randint(0,100,size=N)
y = np.random.randint(0,100,size=N)
random_sample = original_data[x,y]
sc = ax2.scatter(x,y,c=random_sample, cmap=im.cmap, norm=im.norm)
ax2.set_aspect("equal")
ax2.set(xlim=ax.get_xlim(), ylim=ax.get_ylim())
fig.colorbar(sc, ax=[ax,ax2], orientation="horizontal")
plt.show()

If you want to colorize the randomly chosen points according to the image you can use the same colormap and normalization for the scatter as you have for the image.
import numpy as np
import matplotlib.pyplot as plt
original_data = np.random.rand(100,100)
fig, (ax, ax2) = plt.subplots(ncols=2)
im = ax.imshow(original_data, cmap="summer")
N = 89
x = np.random.randint(0,100,size=N)
y = np.random.randint(0,100,size=N)
random_sample = original_data[x,y]
sc = ax2.scatter(x,y,c=random_sample, cmap=im.cmap, norm=im.norm)
ax2.set_aspect("equal")
ax2.set(xlim=ax.get_xlim(), ylim=ax.get_ylim())
fig.colorbar(sc, ax=[ax,ax2], orientation="horizontal")
plt.show()

answered Nov 21 '18 at 1:05
ImportanceOfBeingErnestImportanceOfBeingErnest
135k13151226
135k13151226
you are the best! this is literally the perfect example i was looking for. thank you!!!
– Eric Kim
Nov 21 '18 at 1:16
add a comment |
you are the best! this is literally the perfect example i was looking for. thank you!!!
– Eric Kim
Nov 21 '18 at 1:16
you are the best! this is literally the perfect example i was looking for. thank you!!!
– Eric Kim
Nov 21 '18 at 1:16
you are the best! this is literally the perfect example i was looking for. thank you!!!
– Eric Kim
Nov 21 '18 at 1:16
add a comment |
You just need to choose N numbers from 10,000 (100 x 100) unique points on the 2d plot. I assume you want without replacement. Then you can "unravel" them onto your x,y coordinate.
random_choices = np.random.choice(10000, size=N, replace=False)
x, y = np.unravel_index(random_choices, (100, 100))
You can use these indices to create your scatter plot and size points appropriately:
data = np.random.random((100, 100))
plt.scatter(x, y, s=data[y, x])
i still don't know sure how to plot that indices on the surface plot.
– Eric Kim
Nov 21 '18 at 0:52
Well what you have on the right is really just a scatter plot. I don't know how your data is formatted so can't really tell you how to do it. But I will update with a generic execution.
– busybear
Nov 21 '18 at 0:56
just a scatter plot won't change the marker size based on the value of the data? and the data is just a 2-d numpy array
– Eric Kim
Nov 21 '18 at 0:58
You can use thesargument to change the size of data points.
– busybear
Nov 21 '18 at 1:01
I also made a change to choose without replacement, which I commented on originally but didn't actually show.
– busybear
Nov 21 '18 at 1:07
add a comment |
You just need to choose N numbers from 10,000 (100 x 100) unique points on the 2d plot. I assume you want without replacement. Then you can "unravel" them onto your x,y coordinate.
random_choices = np.random.choice(10000, size=N, replace=False)
x, y = np.unravel_index(random_choices, (100, 100))
You can use these indices to create your scatter plot and size points appropriately:
data = np.random.random((100, 100))
plt.scatter(x, y, s=data[y, x])
i still don't know sure how to plot that indices on the surface plot.
– Eric Kim
Nov 21 '18 at 0:52
Well what you have on the right is really just a scatter plot. I don't know how your data is formatted so can't really tell you how to do it. But I will update with a generic execution.
– busybear
Nov 21 '18 at 0:56
just a scatter plot won't change the marker size based on the value of the data? and the data is just a 2-d numpy array
– Eric Kim
Nov 21 '18 at 0:58
You can use thesargument to change the size of data points.
– busybear
Nov 21 '18 at 1:01
I also made a change to choose without replacement, which I commented on originally but didn't actually show.
– busybear
Nov 21 '18 at 1:07
add a comment |
You just need to choose N numbers from 10,000 (100 x 100) unique points on the 2d plot. I assume you want without replacement. Then you can "unravel" them onto your x,y coordinate.
random_choices = np.random.choice(10000, size=N, replace=False)
x, y = np.unravel_index(random_choices, (100, 100))
You can use these indices to create your scatter plot and size points appropriately:
data = np.random.random((100, 100))
plt.scatter(x, y, s=data[y, x])
You just need to choose N numbers from 10,000 (100 x 100) unique points on the 2d plot. I assume you want without replacement. Then you can "unravel" them onto your x,y coordinate.
random_choices = np.random.choice(10000, size=N, replace=False)
x, y = np.unravel_index(random_choices, (100, 100))
You can use these indices to create your scatter plot and size points appropriately:
data = np.random.random((100, 100))
plt.scatter(x, y, s=data[y, x])
edited Nov 21 '18 at 1:09
answered Nov 21 '18 at 0:46
busybearbusybear
2,890825
2,890825
i still don't know sure how to plot that indices on the surface plot.
– Eric Kim
Nov 21 '18 at 0:52
Well what you have on the right is really just a scatter plot. I don't know how your data is formatted so can't really tell you how to do it. But I will update with a generic execution.
– busybear
Nov 21 '18 at 0:56
just a scatter plot won't change the marker size based on the value of the data? and the data is just a 2-d numpy array
– Eric Kim
Nov 21 '18 at 0:58
You can use thesargument to change the size of data points.
– busybear
Nov 21 '18 at 1:01
I also made a change to choose without replacement, which I commented on originally but didn't actually show.
– busybear
Nov 21 '18 at 1:07
add a comment |
i still don't know sure how to plot that indices on the surface plot.
– Eric Kim
Nov 21 '18 at 0:52
Well what you have on the right is really just a scatter plot. I don't know how your data is formatted so can't really tell you how to do it. But I will update with a generic execution.
– busybear
Nov 21 '18 at 0:56
just a scatter plot won't change the marker size based on the value of the data? and the data is just a 2-d numpy array
– Eric Kim
Nov 21 '18 at 0:58
You can use thesargument to change the size of data points.
– busybear
Nov 21 '18 at 1:01
I also made a change to choose without replacement, which I commented on originally but didn't actually show.
– busybear
Nov 21 '18 at 1:07
i still don't know sure how to plot that indices on the surface plot.
– Eric Kim
Nov 21 '18 at 0:52
i still don't know sure how to plot that indices on the surface plot.
– Eric Kim
Nov 21 '18 at 0:52
Well what you have on the right is really just a scatter plot. I don't know how your data is formatted so can't really tell you how to do it. But I will update with a generic execution.
– busybear
Nov 21 '18 at 0:56
Well what you have on the right is really just a scatter plot. I don't know how your data is formatted so can't really tell you how to do it. But I will update with a generic execution.
– busybear
Nov 21 '18 at 0:56
just a scatter plot won't change the marker size based on the value of the data? and the data is just a 2-d numpy array
– Eric Kim
Nov 21 '18 at 0:58
just a scatter plot won't change the marker size based on the value of the data? and the data is just a 2-d numpy array
– Eric Kim
Nov 21 '18 at 0:58
You can use the
s argument to change the size of data points.– busybear
Nov 21 '18 at 1:01
You can use the
s argument to change the size of data points.– busybear
Nov 21 '18 at 1:01
I also made a change to choose without replacement, which I commented on originally but didn't actually show.
– busybear
Nov 21 '18 at 1:07
I also made a change to choose without replacement, which I commented on originally but didn't actually show.
– busybear
Nov 21 '18 at 1:07
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403729%2fmatplotlib-randomly-pick-n-points-from-2d-array-and-plot-spatial-scatter-plot%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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