#! /usr/bin/env python3 # def volcano_contour_fill ( ): #*****************************************************************************80 # ## volcano_contour_fill() draws a filled contour plot of volcano (X,Y,Z) data. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 03 May 2019 # # Author: # # John Burkardt # import matplotlib.pyplot as plt import numpy as np from matplotlib import cm print ( '' ) print ( 'volcano_contour_fill():' ) print ( ' Make a filled contour plot Z(X,Y) of volcano elevation data.' ) # # Read the Z data from the file. # zmat = np.loadtxt ( 'volcano_data.txt', dtype = 'f', delimiter = ' ' ) # # We have to create a grid of X and Y values corresponding to Z. # m, n = zmat.shape print ( ' Z data has %d rows and %d columns' % ( m, n ) ) xvec = np.linspace ( 0, n, n ) yvec = np.linspace ( 0, m, m ) xmat, ymat = np.meshgrid ( xvec, yvec ) # # Form the figure. # levels = 15 plt.contourf ( xmat, ymat, zmat, levels, cmap = cm.coolwarm ) plt.title ( 'Filled contour plot of Volcano', fontsize = 16 ) plt.xlabel ( '<--- X --->', fontsize = 16 ) plt.ylabel ( '<--- Y --->', fontsize = 16 ) filename = 'volcano_contour_fill.png' plt.savefig ( filename ) print ( ' Graphics saved as "%s"' % ( filename ) ) plt.show ( ) plt.close ( ) return def volcano_contour_line ( ): #*****************************************************************************80 # ## volcano_contour_line() draws a contour line plot of volcano (X,Y,Z) data. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 03 May 2019 # # Author: # # John Burkardt # import matplotlib.pyplot as plt import numpy as np from matplotlib import cm print ( '' ) print ( 'volcano_contour_line():' ) print ( ' Make a contour line plot Z(X,Y) of volcano elevation data.' ) # # Read the Z data from the file. # zmat = np.loadtxt ( 'volcano_data.txt', dtype = 'f', delimiter = ' ' ) # # We have to create a grid of X and Y values corresponding to Z. # m, n = zmat.shape print ( ' Z data has %d rows and %d columns' % ( m, n ) ) xvec = np.linspace ( 0, n - 1, n ) yvec = np.linspace ( 0, m - 1, m ) xmat, ymat = np.meshgrid ( xvec, yvec ) # # Form the figure. # levels = 15 plt.contour ( xmat, ymat, zmat, levels, cmap = cm.coolwarm ) plt.title ( 'Contour line plot of Volcano', fontsize = 16 ) plt.xlabel ( '<--- X --->', fontsize = 16 ) plt.ylabel ( '<--- Y --->', fontsize = 16 ) filename = 'volcano_contour_line.png' plt.savefig ( filename ) print ( ' Graphics saved as "%s"' % ( filename ) ) plt.show ( ) plt.close ( ) return def volcano_contour_both ( ): #*****************************************************************************80 # ## volcano_contour_both() draws a filled and lined contour plot of volcano (X,Y,Z) data. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 28 February 2023 # # Author: # # John Burkardt # import matplotlib.pyplot as plt import numpy as np from matplotlib import cm print ( '' ) print ( 'volcano_contour_both():' ) print ( ' Make a filled contour plot Z(X,Y) of volcano elevation data.' ) # # Read the Z data from the file. # zmat = np.loadtxt ( 'volcano_data.txt', dtype = 'f', delimiter = ' ' ) # # We have to create a grid of X and Y values corresponding to Z. # m, n = zmat.shape print ( ' Z data has %d rows and %d columns' % ( m, n ) ) xvec = np.linspace ( 0, n, n ) yvec = np.linspace ( 0, m, m ) xmat, ymat = np.meshgrid ( xvec, yvec ) # # Form the figure. # levels = 15 plt.contourf ( xmat, ymat, zmat, levels, cmap = cm.coolwarm ) plt.contour ( xmat, ymat, zmat, levels, colors = 'black' ) plt.title ( 'Filled and lined contour plot of Volcano', fontsize = 16 ) plt.xlabel ( '<--- X --->', fontsize = 16 ) plt.ylabel ( '<--- Y --->', fontsize = 16 ) filename = 'volcano_contour_both.png' plt.savefig ( filename ) print ( ' Graphics saved as "%s"' % ( filename ) ) plt.show ( ) plt.close ( ) return if ( __name__ == '__main__' ): volcano_contour_line ( ) volcano_contour_fill ( ) volcano_contour_both ( )