matplotlib - randomly pick N points from 2D array, and plot spatial scatter plot












0















I have plots like the following:
enter image description here



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.










share|improve this question





























    0















    I have plots like the following:
    enter image description here



    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.










    share|improve this question



























      0












      0








      0








      I have plots like the following:
      enter image description here



      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.










      share|improve this question
















      I have plots like the following:
      enter image description here



      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 '18 at 0:53







      Eric Kim

















      asked Nov 21 '18 at 0:41









      Eric KimEric Kim

      534418




      534418
























          2 Answers
          2






          active

          oldest

          votes


















          1














          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()


          enter image description here






          share|improve this answer
























          • you are the best! this is literally the perfect example i was looking for. thank you!!!

            – Eric Kim
            Nov 21 '18 at 1:16



















          0














          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])





          share|improve this answer


























          • 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 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











          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%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









          1














          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()


          enter image description here






          share|improve this answer
























          • you are the best! this is literally the perfect example i was looking for. thank you!!!

            – Eric Kim
            Nov 21 '18 at 1:16
















          1














          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()


          enter image description here






          share|improve this answer
























          • you are the best! this is literally the perfect example i was looking for. thank you!!!

            – Eric Kim
            Nov 21 '18 at 1:16














          1












          1








          1







          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()


          enter image description here






          share|improve this answer













          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()


          enter image description here







          share|improve this answer












          share|improve this answer



          share|improve this answer










          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



















          • 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













          0














          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])





          share|improve this answer


























          • 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 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
















          0














          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])





          share|improve this answer


























          • 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 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














          0












          0








          0







          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])





          share|improve this answer















          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])






          share|improve this answer














          share|improve this answer



          share|improve this answer








          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 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 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 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 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


















          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%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





















































          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







          Popular posts from this blog

          How to send String Array data to Server using php in android

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

          Is anime1.com a legal site for watching anime?