Create a surface plot of xyz altitude data in Python The Next CEO of Stack OverflowHow can I safely create a nested directory in Python?Create a dictionary with list comprehension in PythonWay to create multiline comments in Python?Why is reading lines from stdin much slower in C++ than Python?“Large data” work flows using pandasPython - have a pcolor plot; now want a surfacepython surface plot from 3 1D arraysImprove surface plot visualisation of scatter pointsSurface plot with matplotlib, PythonSurface plot from txt file in python

Why does standard notation not preserve intervals (visually)

Can you replace a racial trait cantrip when leveling up?

Elegant way to replace substring in a regex with optional groups in Python?

Is it possible to search for a directory/file combination?

Example of a Mathematician/Physicist whose Other Publications during their PhD eclipsed their PhD Thesis

What does "Its cash flow is deeply negative" mean?

How do I avoid eval and parse?

sp_blitzCache results Memory grants

Inappropriate reference requests from Journal reviewers

Why am I allowed to create multiple unique pointers from a single object?

Is it ever safe to open a suspicious html file (e.g. email attachment)?

Is micro rebar a better way to reinforce concrete than rebar?

Why is the US ranked as #45 in Press Freedom ratings, despite its extremely permissive free speech laws?

WOW air has ceased operation, can I get my tickets refunded?

How to invert MapIndexed on a ragged structure? How to construct a tree from rules?

Preparing Indesign booklet with .psd graphics for print

Is 'diverse range' a pleonastic phrase?

How do I make a variable always equal to the result of some calculations?

To not tell, not take, and not want

What does convergence in distribution "in the Gromov–Hausdorff" sense mean?

Would a galaxy be visible from outside, but nearby?

How do we know the LHC results are robust?

How to solve a differential equation with a term to a power?

Novel about a guy who is possessed by the divine essence and the world ends?



Create a surface plot of xyz altitude data in Python



The Next CEO of Stack OverflowHow can I safely create a nested directory in Python?Create a dictionary with list comprehension in PythonWay to create multiline comments in Python?Why is reading lines from stdin much slower in C++ than Python?“Large data” work flows using pandasPython - have a pcolor plot; now want a surfacepython surface plot from 3 1D arraysImprove surface plot visualisation of scatter pointsSurface plot with matplotlib, PythonSurface plot from txt file in python










0















I am trying to create a surface plot of a mountain in python, of which I have some xyz data. The end result should look something like that. The file is formatted as follows:



616000.0 90500.0 3096.712
616000.0 90525.0 3123.415
616000.0 90550.0 3158.902
616000.0 90575.0 3182.109
616000.0 90600.0 3192.991
616025.0 90500.0 3082.684
616025.0 90525.0 3116.597
616025.0 90550.0 3149.812
616025.0 90575.0 3177.607
616025.0 90600.0 3191.986


and so on. The first column represents the x coordinate, the middle one the y coordinate, and z the altitude that belongs to the xy coordinate.



I read in the data using pandas and then convert the columns to individual x, y, z NumPy 1D arrays. So far I managed to create a simple 3D scatter plot with a for loop iterating over each index of each 1D array, but that takes ages and makes the appearance of being quite inefficient.



I've tried to work with scipy.interpolate.griddata and plt.plot_surface, but for z data I always get the error that data should be in a 2D array, but I cannot figure out why or how it should be 2D data. I assume that given I have xyz data, there should be a way to simply create a surface from it. Is there a simple way?










share|improve this question




























    0















    I am trying to create a surface plot of a mountain in python, of which I have some xyz data. The end result should look something like that. The file is formatted as follows:



    616000.0 90500.0 3096.712
    616000.0 90525.0 3123.415
    616000.0 90550.0 3158.902
    616000.0 90575.0 3182.109
    616000.0 90600.0 3192.991
    616025.0 90500.0 3082.684
    616025.0 90525.0 3116.597
    616025.0 90550.0 3149.812
    616025.0 90575.0 3177.607
    616025.0 90600.0 3191.986


    and so on. The first column represents the x coordinate, the middle one the y coordinate, and z the altitude that belongs to the xy coordinate.



    I read in the data using pandas and then convert the columns to individual x, y, z NumPy 1D arrays. So far I managed to create a simple 3D scatter plot with a for loop iterating over each index of each 1D array, but that takes ages and makes the appearance of being quite inefficient.



    I've tried to work with scipy.interpolate.griddata and plt.plot_surface, but for z data I always get the error that data should be in a 2D array, but I cannot figure out why or how it should be 2D data. I assume that given I have xyz data, there should be a way to simply create a surface from it. Is there a simple way?










    share|improve this question


























      0












      0








      0








      I am trying to create a surface plot of a mountain in python, of which I have some xyz data. The end result should look something like that. The file is formatted as follows:



      616000.0 90500.0 3096.712
      616000.0 90525.0 3123.415
      616000.0 90550.0 3158.902
      616000.0 90575.0 3182.109
      616000.0 90600.0 3192.991
      616025.0 90500.0 3082.684
      616025.0 90525.0 3116.597
      616025.0 90550.0 3149.812
      616025.0 90575.0 3177.607
      616025.0 90600.0 3191.986


      and so on. The first column represents the x coordinate, the middle one the y coordinate, and z the altitude that belongs to the xy coordinate.



      I read in the data using pandas and then convert the columns to individual x, y, z NumPy 1D arrays. So far I managed to create a simple 3D scatter plot with a for loop iterating over each index of each 1D array, but that takes ages and makes the appearance of being quite inefficient.



      I've tried to work with scipy.interpolate.griddata and plt.plot_surface, but for z data I always get the error that data should be in a 2D array, but I cannot figure out why or how it should be 2D data. I assume that given I have xyz data, there should be a way to simply create a surface from it. Is there a simple way?










      share|improve this question
















      I am trying to create a surface plot of a mountain in python, of which I have some xyz data. The end result should look something like that. The file is formatted as follows:



      616000.0 90500.0 3096.712
      616000.0 90525.0 3123.415
      616000.0 90550.0 3158.902
      616000.0 90575.0 3182.109
      616000.0 90600.0 3192.991
      616025.0 90500.0 3082.684
      616025.0 90525.0 3116.597
      616025.0 90550.0 3149.812
      616025.0 90575.0 3177.607
      616025.0 90600.0 3191.986


      and so on. The first column represents the x coordinate, the middle one the y coordinate, and z the altitude that belongs to the xy coordinate.



      I read in the data using pandas and then convert the columns to individual x, y, z NumPy 1D arrays. So far I managed to create a simple 3D scatter plot with a for loop iterating over each index of each 1D array, but that takes ages and makes the appearance of being quite inefficient.



      I've tried to work with scipy.interpolate.griddata and plt.plot_surface, but for z data I always get the error that data should be in a 2D array, but I cannot figure out why or how it should be 2D data. I assume that given I have xyz data, there should be a way to simply create a surface from it. Is there a simple way?







      python surface geography topography






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 17 '18 at 11:11







      user10202193

















      asked Aug 17 '18 at 8:42









      sflucksfluck

      203




      203






















          2 Answers
          2






          active

          oldest

          votes


















          0














          There is an easier way to achieve your goal without using pandas.



          import numpy as np 
          import matplotlib.pyplot as plt
          import mpl_toolkits.mplot3d

          x, y = np.mgrid[-2 : 2 : 20j, -2 : 2 : 20j]
          z = 50 * np.sin(x + y) # test data
          output = plt.subplot(111, projection = '3d') # 3d projection
          output.plot_surface(x, y, z, rstride = 2, cstride = 1, cmap = plt.cm.Blues_r)
          output.set_xlabel('x') # axis label
          output.set_xlabel('y')
          output.set_xlabel('z')

          plt.show()


          plot example






          share|improve this answer

























          • hi thanks for your answer. When I try to use my x,y,z data, it also says that Z must be 2-dimensional. My z data are altitude values, so they cant be computed by inputs x and y.

            – sfluck
            Aug 17 '18 at 9:08


















          0














          Using functions plot_trisurf and scatter from matplotlib, given X Y Z data can be plotted similar to given plot.



          import sys
          import csv
          import numpy as np
          import matplotlib.pyplot as plt
          from mpl_toolkits.mplot3d import axes3d

          # Read CSV
          csvFileName = sys.argv[1]
          csvData = []
          with open(csvFileName, 'r') as csvFile:
          csvReader = csv.reader(csvFile, delimiter=' ')
          for csvRow in csvReader:
          csvData.append(csvRow)

          # Get X, Y, Z
          csvData = np.array(csvData)
          csvData = csvData.astype(np.float)
          X, Y, Z = csvData[:,0], csvData[:,1], csvData[:,2]

          # Plot X,Y,Z
          fig = plt.figure()
          ax = fig.add_subplot(111, projection='3d')
          ax.plot_trisurf(X, Y, Z, color='white', edgecolors='grey', alpha=0.5)
          ax.scatter(X, Y, Z, c='red')
          plt.show()


          Here,



          • file containing X Y Z data provided as argument to above script

          • in plot_trisurf, parameters used to control appearance. e.g. alpha used to control opacity of surface

          • in scatter, c parameter specifies color of points plotted on surface

          For given data file, following plot is generated



          enter image description here



          Note: Here, the terrain is formed by triangulation of given set of 3D points. Hence, contours along surface in plot are not aligned to X- and Y- axes






          share|improve this answer

























            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%2f51891538%2fcreate-a-surface-plot-of-xyz-altitude-data-in-python%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









            0














            There is an easier way to achieve your goal without using pandas.



            import numpy as np 
            import matplotlib.pyplot as plt
            import mpl_toolkits.mplot3d

            x, y = np.mgrid[-2 : 2 : 20j, -2 : 2 : 20j]
            z = 50 * np.sin(x + y) # test data
            output = plt.subplot(111, projection = '3d') # 3d projection
            output.plot_surface(x, y, z, rstride = 2, cstride = 1, cmap = plt.cm.Blues_r)
            output.set_xlabel('x') # axis label
            output.set_xlabel('y')
            output.set_xlabel('z')

            plt.show()


            plot example






            share|improve this answer

























            • hi thanks for your answer. When I try to use my x,y,z data, it also says that Z must be 2-dimensional. My z data are altitude values, so they cant be computed by inputs x and y.

              – sfluck
              Aug 17 '18 at 9:08















            0














            There is an easier way to achieve your goal without using pandas.



            import numpy as np 
            import matplotlib.pyplot as plt
            import mpl_toolkits.mplot3d

            x, y = np.mgrid[-2 : 2 : 20j, -2 : 2 : 20j]
            z = 50 * np.sin(x + y) # test data
            output = plt.subplot(111, projection = '3d') # 3d projection
            output.plot_surface(x, y, z, rstride = 2, cstride = 1, cmap = plt.cm.Blues_r)
            output.set_xlabel('x') # axis label
            output.set_xlabel('y')
            output.set_xlabel('z')

            plt.show()


            plot example






            share|improve this answer

























            • hi thanks for your answer. When I try to use my x,y,z data, it also says that Z must be 2-dimensional. My z data are altitude values, so they cant be computed by inputs x and y.

              – sfluck
              Aug 17 '18 at 9:08













            0












            0








            0







            There is an easier way to achieve your goal without using pandas.



            import numpy as np 
            import matplotlib.pyplot as plt
            import mpl_toolkits.mplot3d

            x, y = np.mgrid[-2 : 2 : 20j, -2 : 2 : 20j]
            z = 50 * np.sin(x + y) # test data
            output = plt.subplot(111, projection = '3d') # 3d projection
            output.plot_surface(x, y, z, rstride = 2, cstride = 1, cmap = plt.cm.Blues_r)
            output.set_xlabel('x') # axis label
            output.set_xlabel('y')
            output.set_xlabel('z')

            plt.show()


            plot example






            share|improve this answer















            There is an easier way to achieve your goal without using pandas.



            import numpy as np 
            import matplotlib.pyplot as plt
            import mpl_toolkits.mplot3d

            x, y = np.mgrid[-2 : 2 : 20j, -2 : 2 : 20j]
            z = 50 * np.sin(x + y) # test data
            output = plt.subplot(111, projection = '3d') # 3d projection
            output.plot_surface(x, y, z, rstride = 2, cstride = 1, cmap = plt.cm.Blues_r)
            output.set_xlabel('x') # axis label
            output.set_xlabel('y')
            output.set_xlabel('z')

            plt.show()


            plot example







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Aug 17 '18 at 10:31









            TimH

            2,52321626




            2,52321626










            answered Aug 17 '18 at 8:57









            ko samuelko samuel

            114




            114












            • hi thanks for your answer. When I try to use my x,y,z data, it also says that Z must be 2-dimensional. My z data are altitude values, so they cant be computed by inputs x and y.

              – sfluck
              Aug 17 '18 at 9:08

















            • hi thanks for your answer. When I try to use my x,y,z data, it also says that Z must be 2-dimensional. My z data are altitude values, so they cant be computed by inputs x and y.

              – sfluck
              Aug 17 '18 at 9:08
















            hi thanks for your answer. When I try to use my x,y,z data, it also says that Z must be 2-dimensional. My z data are altitude values, so they cant be computed by inputs x and y.

            – sfluck
            Aug 17 '18 at 9:08





            hi thanks for your answer. When I try to use my x,y,z data, it also says that Z must be 2-dimensional. My z data are altitude values, so they cant be computed by inputs x and y.

            – sfluck
            Aug 17 '18 at 9:08













            0














            Using functions plot_trisurf and scatter from matplotlib, given X Y Z data can be plotted similar to given plot.



            import sys
            import csv
            import numpy as np
            import matplotlib.pyplot as plt
            from mpl_toolkits.mplot3d import axes3d

            # Read CSV
            csvFileName = sys.argv[1]
            csvData = []
            with open(csvFileName, 'r') as csvFile:
            csvReader = csv.reader(csvFile, delimiter=' ')
            for csvRow in csvReader:
            csvData.append(csvRow)

            # Get X, Y, Z
            csvData = np.array(csvData)
            csvData = csvData.astype(np.float)
            X, Y, Z = csvData[:,0], csvData[:,1], csvData[:,2]

            # Plot X,Y,Z
            fig = plt.figure()
            ax = fig.add_subplot(111, projection='3d')
            ax.plot_trisurf(X, Y, Z, color='white', edgecolors='grey', alpha=0.5)
            ax.scatter(X, Y, Z, c='red')
            plt.show()


            Here,



            • file containing X Y Z data provided as argument to above script

            • in plot_trisurf, parameters used to control appearance. e.g. alpha used to control opacity of surface

            • in scatter, c parameter specifies color of points plotted on surface

            For given data file, following plot is generated



            enter image description here



            Note: Here, the terrain is formed by triangulation of given set of 3D points. Hence, contours along surface in plot are not aligned to X- and Y- axes






            share|improve this answer





























              0














              Using functions plot_trisurf and scatter from matplotlib, given X Y Z data can be plotted similar to given plot.



              import sys
              import csv
              import numpy as np
              import matplotlib.pyplot as plt
              from mpl_toolkits.mplot3d import axes3d

              # Read CSV
              csvFileName = sys.argv[1]
              csvData = []
              with open(csvFileName, 'r') as csvFile:
              csvReader = csv.reader(csvFile, delimiter=' ')
              for csvRow in csvReader:
              csvData.append(csvRow)

              # Get X, Y, Z
              csvData = np.array(csvData)
              csvData = csvData.astype(np.float)
              X, Y, Z = csvData[:,0], csvData[:,1], csvData[:,2]

              # Plot X,Y,Z
              fig = plt.figure()
              ax = fig.add_subplot(111, projection='3d')
              ax.plot_trisurf(X, Y, Z, color='white', edgecolors='grey', alpha=0.5)
              ax.scatter(X, Y, Z, c='red')
              plt.show()


              Here,



              • file containing X Y Z data provided as argument to above script

              • in plot_trisurf, parameters used to control appearance. e.g. alpha used to control opacity of surface

              • in scatter, c parameter specifies color of points plotted on surface

              For given data file, following plot is generated



              enter image description here



              Note: Here, the terrain is formed by triangulation of given set of 3D points. Hence, contours along surface in plot are not aligned to X- and Y- axes






              share|improve this answer



























                0












                0








                0







                Using functions plot_trisurf and scatter from matplotlib, given X Y Z data can be plotted similar to given plot.



                import sys
                import csv
                import numpy as np
                import matplotlib.pyplot as plt
                from mpl_toolkits.mplot3d import axes3d

                # Read CSV
                csvFileName = sys.argv[1]
                csvData = []
                with open(csvFileName, 'r') as csvFile:
                csvReader = csv.reader(csvFile, delimiter=' ')
                for csvRow in csvReader:
                csvData.append(csvRow)

                # Get X, Y, Z
                csvData = np.array(csvData)
                csvData = csvData.astype(np.float)
                X, Y, Z = csvData[:,0], csvData[:,1], csvData[:,2]

                # Plot X,Y,Z
                fig = plt.figure()
                ax = fig.add_subplot(111, projection='3d')
                ax.plot_trisurf(X, Y, Z, color='white', edgecolors='grey', alpha=0.5)
                ax.scatter(X, Y, Z, c='red')
                plt.show()


                Here,



                • file containing X Y Z data provided as argument to above script

                • in plot_trisurf, parameters used to control appearance. e.g. alpha used to control opacity of surface

                • in scatter, c parameter specifies color of points plotted on surface

                For given data file, following plot is generated



                enter image description here



                Note: Here, the terrain is formed by triangulation of given set of 3D points. Hence, contours along surface in plot are not aligned to X- and Y- axes






                share|improve this answer















                Using functions plot_trisurf and scatter from matplotlib, given X Y Z data can be plotted similar to given plot.



                import sys
                import csv
                import numpy as np
                import matplotlib.pyplot as plt
                from mpl_toolkits.mplot3d import axes3d

                # Read CSV
                csvFileName = sys.argv[1]
                csvData = []
                with open(csvFileName, 'r') as csvFile:
                csvReader = csv.reader(csvFile, delimiter=' ')
                for csvRow in csvReader:
                csvData.append(csvRow)

                # Get X, Y, Z
                csvData = np.array(csvData)
                csvData = csvData.astype(np.float)
                X, Y, Z = csvData[:,0], csvData[:,1], csvData[:,2]

                # Plot X,Y,Z
                fig = plt.figure()
                ax = fig.add_subplot(111, projection='3d')
                ax.plot_trisurf(X, Y, Z, color='white', edgecolors='grey', alpha=0.5)
                ax.scatter(X, Y, Z, c='red')
                plt.show()


                Here,



                • file containing X Y Z data provided as argument to above script

                • in plot_trisurf, parameters used to control appearance. e.g. alpha used to control opacity of surface

                • in scatter, c parameter specifies color of points plotted on surface

                For given data file, following plot is generated



                enter image description here



                Note: Here, the terrain is formed by triangulation of given set of 3D points. Hence, contours along surface in plot are not aligned to X- and Y- axes







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Mar 8 at 15:48

























                answered Mar 8 at 15:42









                programmerprogrammer

                1819




                1819



























                    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%2f51891538%2fcreate-a-surface-plot-of-xyz-altitude-data-in-python%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

                    Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

                    Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

                    2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived