banner



How To Add A Line To A Contour Plot Python

20. Profile Plots with Matplotlib

By Bernd Klein. Terminal modified: 01 Feb 2022.

Contour Plot

A contour line or isoline of a function of ii variables is a curve along which the function has a constant value.

It is a cross-section of the 3-dimensional graph of the office f(x, y) parallel to the x, y aeroplane.

Contour Plot in an artistic way

Profile lines are used e.g. in geography and meteorology.

In cartography, a profile line joins points of equal tiptop (height) above a given level, such as hateful sea level.

We can also say in a more full general way that a contour line of a function with ii variables is a curve which connects points with the same values.

Creating a "meshgrid"

Creating a Meshgrid

              # the following line is only necessary if working with "ipython notebook"              %              matplotlib              inline              import              matplotlib.pyplot              as              plt              import              numpy              every bit              np              north              ,              m              =              7              ,              vii              start              =              -              3              x_vals              =              np              .              arange              (              starting time              ,              commencement              +              n              ,              one              )              y_vals              =              np              .              arange              (              start              ,              start              +              chiliad              ,              i              )              10              ,              Y              =              np              .              meshgrid              (              x_vals              ,              y_vals              )              print              (              X              )              impress              (              Y              )            

OUTPUT:

[[-3 -two -one  0  ane  2  3]  [-3 -2 -1  0  1  2  3]  [-3 -two -one  0  1  2  iii]  [-3 -2 -1  0  one  ii  3]  [-three -two -1  0  ane  2  three]  [-3 -2 -1  0  1  2  3]  [-iii -ii -1  0  1  2  three]] [[-iii -3 -three -3 -3 -three -3]  [-two -2 -ii -two -ii -2 -2]  [-one -i -i -1 -1 -i -i]  [ 0  0  0  0  0  0  0]  [ 1  1  one  ane  1  1  one]  [ 2  2  2  2  2  2  2]  [ 3  3  three  iii  3  3  iii]]            

We can visulize our meshgridif nosotros add the following code to our previous programme:

              fig              ,              ax              =              plt              .              subplots              ()              ax              .              scatter              (              X              ,              Y              ,              color              =              "green"              )              ax              .              set_title              (              'Regular Filigree, created by Meshgrid'              )              ax              .              set_xlabel              (              'x'              )              ax              .              set_ylabel              (              'y'              )              plt              .              show              ()            

data image

              import              numpy              equally              np              xlist              =              np              .              linspace              (              -              3.0              ,              3.0              ,              iii              )              ylist              =              np              .              linspace              (              -              iii.0              ,              3.0              ,              4              )              X              ,              Y              =              np              .              meshgrid              (              xlist              ,              ylist              )              Z              =              np              .              sqrt              (              X              **              2              +              Y              **              2              )              print              (              Z              )            

OUTPUT:

[[4.24264069 3.         four.24264069]  [3.16227766 1.         3.16227766]  [iii.16227766 1.         iii.16227766]  [four.24264069 3.         4.24264069]]            

Calculation of the Values

Calculation of the Contour Values

              import              numpy              as              np              xlist              =              np              .              linspace              (              -              three.0              ,              3.0              ,              3              )              ylist              =              np              .              linspace              (              -              3.0              ,              iii.0              ,              iv              )              X              ,              Y              =              np              .              meshgrid              (              xlist              ,              ylist              )              Z              =              np              .              sqrt              (              Ten              **              2              +              Y              **              2              )              impress              (              Z              )            

OUTPUT:

[[iv.24264069 3.         4.24264069]  [three.16227766 1.         iii.16227766]  [three.16227766 1.         3.16227766]  [iv.24264069 3.         iv.24264069]]            
              fig              =              plt              .              effigy              (              figsize              =              (              6              ,              5              ))              left              ,              bottom              ,              width              ,              height              =              0.1              ,              0.1              ,              0.8              ,              0.8              ax              =              fig              .              add_axes              ([              left              ,              lesser              ,              width              ,              summit              ])              Z              =              np              .              sqrt              (              Ten              **              two              +              Y              **              two              )              cp              =              ax              .              contour              (              X              ,              Y              ,              Z              )              ax              .              clabel              (              cp              ,              inline              =              True              ,              fontsize              =              x              )              ax              .              set_title              (              'Contour Plot'              )              ax              .              set_xlabel              (              '10 (cm)'              )              ax              .              set_ylabel              (              'y (cm)'              )              plt              .              prove              ()            

data image

Changing the Colours and the Line Manner

              import              matplotlib.pyplot              every bit              plt              plt              .              figure              ()              cp              =              plt              .              contour              (              X              ,              Y              ,              Z              ,              colors              =              'black'              ,              linestyles              =              'dashed'              )              plt              .              clabel              (              cp              ,              inline              =              True              ,              fontsize              =              ten              )              plt              .              title              (              'Contour Plot'              )              plt              .              xlabel              (              'x (cm)'              )              plt              .              ylabel              (              'y (cm)'              )              plt              .              prove              ()            

data image

Filled Contours

              import              matplotlib.pyplot              as              plt              import              numpy              as              np              fig              =              plt              .              figure              (              figsize              =              (              6              ,              5              ))              left              ,              bottom              ,              width              ,              height              =              0.1              ,              0.ane              ,              0.8              ,              0.8              ax              =              fig              .              add_axes              ([              left              ,              bottom              ,              width              ,              tiptop              ])              start              ,              cease              ,              n_values              =              -              viii              ,              8              ,              800              x_vals              =              np              .              linspace              (              start              ,              stop              ,              n_values              )              y_vals              =              np              .              linspace              (              start              ,              stop              ,              n_values              )              X              ,              Y              =              np              .              meshgrid              (              x_vals              ,              y_vals              )              Z              =              np              .              sqrt              (              X              **              2              +              Y              **              2              )              cp              =              plt              .              contourf              (              X              ,              Y              ,              Z              )              plt              .              colorbar              (              cp              )              ax              .              set_title              (              'Contour Plot'              )              ax              .              set_xlabel              (              'ten (cm)'              )              ax              .              set_ylabel              (              'y (cm)'              )              plt              .              show              ()            

data image

Private Colours

              import              numpy              as              np              import              matplotlib.pyplot              as              plt              xlist              =              np              .              linspace              (              -              3.0              ,              3.0              ,              100              )              ylist              =              np              .              linspace              (              -              three.0              ,              three.0              ,              100              )              10              ,              Y              =              np              .              meshgrid              (              xlist              ,              ylist              )              Z              =              np              .              sqrt              (              X              **              2              +              Y              **              2              )              plt              .              effigy              ()              profile              =              plt              .              profile              (              X              ,              Y              ,              Z              )              plt              .              clabel              (              contour              ,              colors              =              'yard'              ,              fmt              =              '              %2.1f              '              ,              fontsize              =              12              )              c              =              (              '#ff0000'              ,              '#ffff00'              ,              '#0000FF'              ,              '0.6'              ,              'c'              ,              'thou'              )              contour_filled              =              plt              .              contourf              (              Ten              ,              Y              ,              Z              ,              colors              =              c              )              plt              .              colorbar              (              contour_filled              )              plt              .              title              (              'Filled Contours Plot'              )              plt              .              xlabel              (              'x (cm)'              )              plt              .              ylabel              (              'y (cm)'              )              plt              .              savefig              (              'contourplot_own_colours.png'              ,              dpi              =              300              )              plt              .              testify              ()            

data image

Levels

The levels were decided automatically by contour and contourf and then far. They tin can be defined manually, by providing a list of levels equally a fourth parameter. Contour lines will exist fatigued for each value in the list, if we use contour. For contourf, at that place will be filled colored regions between the values in the list.

            import            numpy            every bit            np            import            matplotlib.pyplot            every bit            plt            xlist            =            np            .            linspace            (            -            3.0            ,            three.0            ,            100            )            ylist            =            np            .            linspace            (            -            3.0            ,            three.0            ,            100            )            X            ,            Y            =            np            .            meshgrid            (            xlist            ,            ylist            )            Z            =            np            .            sqrt            (            X            **            2            +            Y            **            ii            )            plt            .            effigy            ()            levels            =            [            0.0            ,            0.two            ,            0.five            ,            0.9            ,            1.5            ,            2.5            ,            3.5            ]            contour            =            plt            .            contour            (            X            ,            Y            ,            Z            ,            levels            ,            colors            =            'k'            )            plt            .            clabel            (            profile            ,            colors            =            'k'            ,            fmt            =            '            %two.1f            '            ,            fontsize            =            12            )            contour_filled            =            plt            .            contourf            (            X            ,            Y            ,            Z            ,            levels            )            plt            .            colorbar            (            contour_filled            )            plt            .            championship            (            'Plot from level list'            )            plt            .            xlabel            (            'x (cm)'            )            plt            .            ylabel            (            'y (cm)'            )            plt            .            show            ()          

The terminal example of this chapter volition exist a "lovely" contour plot:

            import            matplotlib.pyplot            as            plt            import            numpy            as            np            y            ,            x            =            np            .            ogrid            [            -            one            :            2            :            100            j            ,            -            ane            :            1            :            100            j            ]            plt            .            contour            (            x            .            ravel            (),            y            .            ravel            (),            x            **            ii            +            (            y            -            ((            x            **            ii            )            **            (            1.0            /            3            )))            **            ii            ,            [            one            ],            colors            =            'red'            ,)            plt            .            axis            (            'equal'            )            plt            .            testify            ()          

Alive Python training

instructor-led training course

Upcoming online Courses

Enrol here

How To Add A Line To A Contour Plot Python,

Source: https://python-course.eu/numerical-programming/contour-plots-with-matplotlib.php

Posted by: yuenbegamseley.blogspot.com

0 Response to "How To Add A Line To A Contour Plot Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel