|
1- |
Py-LM-DivPlot
Python program,
2D Surface
plotting used data from .CSV file |
|
|
| |
|
|
-
Py-LM-DivPlot Python program, , 2D Surface
plotting used data from .CSV file
-
the Visualizing data through bar plot
-
the Visualizing data through Line Solid
plot
-
the Visualizing data through Line(Solid)-Grid
plot
-
the Visualizing data through Line(Dashed)-Marker
plot
-
the Visualizing data through Scatter plot
-
the Visualizing data through pie plot
-
the Visualizing Random values through
Hitogram plot
|
- Create a new Python project In Visual Studio
2022
- the
Microsoft Visual Studio (... or 2019 or 2022) is
a powerful IDE for Python language
- Open/Run
Microsoft Visual Studio 2022
- To view
Python templates, search for python.
 |
Select the
Python
Application template, and select
Next. |
- Create a new Python project In
Visual Studio 2022
On the Configure your new project screen
- (specify a name and file location
for the project, and then select Create)
Project name:
Py-LM-DivPlot
Location:
C:\Users\...\source\repos
(default location for Visual
Studio 2022)
|
- The new project opens in Visual Studio
2022 - (Visual Studio 2022
Compiler - IDE, to compile Python project /
file )
- The
Visual Studio Solution Explorer window shows the
project structure
- Python
Project Properieties - Py-LM-DivPlot
- Projct
Folder: C:\Users\...\source\repos\Py-LM-DivPlot
- Startup
File: Py_LM_DivPlot.py
- Project
file: Py-LM-DivPlot.sln
|
- Download Python
Project : Py-LM-DivPlot.zip -
(16.4 KB zip file) Download
- Project
consist of:
One Python Form -
(Py_LM_DivPlot.py )
Important:
winequality-N1.csv file - it is part of
Py-LM-DivPlot.zip |
 | |
|
Download Python
Project : Py-LM-DivPlot.zip -
(16.4 KB zip file) Download
|
|
winequality-N1.csv file - it is part of Py-LM-DivPlot.zip ; |
|
|
- Install pandas, matplotlib and csv in Python on Windows Operating System,
Using Command Prompt
Source Code of Python Project
Py-LM-DivPlot:
|
Download this Source Code at
python file: Py_LM_DivPlot.py - (2.35 KB Python file)
download
|
 |
Continue
|
Py-LM-DivPlot Python program, 2D Surface
plotting used data from .CSV file | |
|
|
| |
import
pandas
as pd
import
matplotlib.pyplot
as
plt
import
csv
# reading CSV file
df = pd.read_csv( 'winequalityN1.csv',
delimiter=',')
# converting column data to list
q = df[ 'quality'].tolist()
a = df[ 'alcohol'].tolist()
# printing list data
print ('Quality,
24 values:',
q)
print ("")
print ("")
print ('Alcohol,
24 values:',
a)
#the Visualizing data through bar plot.
plt.figure(1)
plt.xlabel( 'quality')
plt.ylabel( 'alcohol')
plt.title( 'Qualities
of Alcohol, 24 Values - Bar Plot')
plt.bar(df[ 'quality'],
df['alcohol'],
color =
'g')
plt.show()
#the Visualizing data through line solid plot
plt.figure(2)
plt.xlabel( 'quality')
plt.ylabel( 'alcohol')
plt.title( 'Quality
and Alcohol Report, 24 values - Line Solid Plot',
fontsize = 12)
plt.plot(q, a, color =
'g',
linestyle =
'solid',
label =
"Quality-Alcohol Data")
plt.xticks(rotation = 25),
#plt.legend()
plt.show()
#the Visualizing data through line(dashed)-Marker
plot
plt.figure(3)
plt.xlabel( 'quality')
plt.ylabel( 'alcohol')
plt.title( 'Quality
and Alcohol Report, 24 values - Line(Dashed)-Marker
Plot',
fontsize = 12)
plt.plot(q, a, color =
'g',
marker =
'x',
linestyle =
'dashed',
label =
"Quality-Alcohol Data")
plt.xticks(rotation = 25)
plt.show()
#the Visualizing data through line(solid)-Grid
plot
plt.figure(4)
plt.xlabel( 'quality')
plt.ylabel( 'alcohol')
plt.title( 'Quality
and Alcohol Report, 24 values - Line(Solid)-Grid
Plot',
fontsize = 12)
plt.plot(q, a, color =
'g',
linestyle =
'solid',
label =
"Quality-Alcohol Data")
plt.xticks(rotation = 25)
plt.grid()
plt.show()
#the Visualizing data through Scatter-Grid
plot
plt.figure(5)
plt.scatter(q, a, color =
'g',s
= 100)
plt.xlabel( 'quality')
plt.ylabel( 'alcohol')
plt.title( 'Quality
& Alcohol Report, 24 values - Scatter-Grid Plot',
fontsize = 12)
#plt.Scatter(q, a, color = 'g',s = 100)
plt.xticks(rotation = 25)
plt.grid()
plt.show()
#the Visualizing data through pie plot
plt.figure(6)
plt.pie(a,labels = q,autopct =
'%.2f%%')
plt.title( 'Quality
& Alcohol Marks, 24 values - Pie Plot',
fontsize = 12)
plt.show()
#the Visualizing data through Histogram plot
import
numpy
as np
x = np.random.normal(170, 10, 250)
plt.figure(7)
plt.title( 'Histogram
Plot, Random values - Hitogram Plot',
fontsize = 12)
plt.hist(x)
plt.show() |
|
| | |
|
|
|
|
- Debug of Python Project
Py-LM-DivPlot, 0 Errors
Output of Python Project
Py-LM-DivPlot:
| |
-
24 values of inequalityN1.csv file of Columns
(Quality and Alcohol)
|
 |
|
|
-
the Visualizing data of .cvs file of
Columns (Quality and Alcohol), 24 values - bar plot
|
|
 |
|
|
-
the Visualizing data of .cvs file of Columns
(Quality and Alcohol), 24 values - Line Solid plot
|
|
 |
|
|
- the Visualizing data of .cvs file of Columns
(Quality and Alcohol), 24 values -
Line(Solid)-Grid plot
|
|
|
|
|
- the Visualizing data
of .cvs file of Columns (Quality and Alcohol),
24 values - Line(Dashed)-Marker plot
|
|
 |
|
|
-
the Visualizing data of .cvs file of
Columns (Quality and Alcohol), 24 values -
Scatter plot
|
|
 |
|
|
-
the Visualizing data of .cvs file of
Columns (Quality and Alcohol), 24 values -
pie plot
|
|
 |
|
|
-
Random values - Hitogram plot
|
|
 |
|
| | | |
| |
|
| |
|
|
2- |
Py-LM-3d
Python program, 3D Surface plotting |
|
|
| |
|
|
-
Py-LM- Python program, 3D Surface plotting
3D Surface plotting in Python using Matplotlib,
A Surface Plot is a representation of
three-dimensional dataset. used to create 3D
surface plots Functions:
-
Plot with grid values
-
Plot with averaged values
-
3D Plot Projections
-
3D surface having 2D contour plot
projections
-
Surface plot
-
3D surface ...
|
- Create a new Python project In Visual Studio
2022
- the
Microsoft Visual Studio (... or 2019 or 2022) is
a powerful IDE for Python language
- Open/Run
Microsoft Visual Studio 2022
- To view
Python templates, search for python.
 |
Select the
Python
Application template, and select
Next. |
- Create a new Python project In
Visual Studio 2022
On the Configure your new project screen
- (specify a name and file location
for the project, and then select Create)
Project name:
Py-LM-3d
Location:
C:\Users\...\source\repos
(default location for Visual
Studio 2022)
|
- The new project opens in Visual Studio
2022 - (Visual Studio 2022
Compiler - IDE, to compile Python project /
file )
- The
Visual Studio Solution Explorer window shows the
project structure
- Python
Project Properieties - Py-LM-3d
- Projct
Folder: C:\Users\...\source\repos\Py-LM-3d
- Startup
File: Py_LM_3d.py
- Project
file: Py-LM-3d.sln
|
- Download Python
Project : Py-LM-3d.zip -
(13.1 KB zip file) Download
- Project
consist of:
One Python Form -
(Py_LM_3d.py )
|
 | |
|
Download Python
Project : Py-LM-3d.zip -
(13.1 KB zip file) Download
|
|
|
- Install matplotlib and numpy in Python on Windows Operating System,
Using Command Prompt
| 1- Install matplotlib in Python on Windows Operating System,
Using Command Prompt |
Step
1 - Run the Command Prompt on Windows
Operating System:
Command Prompt is an
application on most Windows computers that
directly communicates with the operating
system
How to open the Command Prompt:
Press the Windows key + r.
In the Run box, type cmd,
and then click Ok. |
|
 |
| |
Step
2 - install matplotlib:
In the Command prompt, use the following pip
command to install the matplotlib library
pip install matplotlib
|
|
 |
| |
| 2- Install numpy in Python on Windows Operating System,
Using Command Prompt |
|
pip install
numpy |
 |
| |
| |
Source Code of Python Project
Py-LM-3d:
|
Download this Source Code at
python file: Py_LM_3d.py - (5.98 KB Python file)
download
|
 |
Continue
|
Py-LM-3d
Python program, 3D Surface plotting | |
|
|
| |
# 3D Surface plotting in Python using Matplotlib
#1-
import
matplotlib
as
mpl
import
matplotlib.pyplot
as
plt
import
numpy
as np
#1.1
x_lst = np.linspace(-5, 5, 4)
y_lst = np.linspace(-5, 5, 4)
x_grid, y_grid = np.meshgrid(x_lst, y_lst)
z_grid = x_grid + y_grid
# values at face corners (at the grid points)
c_grid = np.abs(x_grid + y_grid)
# same size like x_val, y_val, z_val
print ('1-
Values at Face Corners, at the Grid Points'
)
print ("c_grid
= ...",
c_grid)
print ('')
# color map and normalization
c_min, c_max = np.min(c_grid), np.max(c_grid)
# could also be min and max of c_faces
norm = mpl.colors.Normalize(vmin=c_min, vmax=c_max)
cmap = mpl.cm.viridis
fig = plt.figure(1)
plt.title( 'plot
with grid values')
ax = fig.add_subplot(111, projection= '3d')
surf = ax.plot_surface(x_grid, y_grid, z_grid,
rstride=1, cstride=1,
# no downsampling
facecolors = cmap(norm(c_grid)), shade= False)
cbar =
plt.colorbar(mpl.cm.ScalarMappable(norm=norm,
cmap=cmap), ax=ax, pad=0.2)
ax.dist = 8
plt.show()
#1.2
# averaged corner values for each face
c_faces =
np.lib.stride_tricks.sliding_window_view(c_grid,
(2,2))
c_faces = np.mean(c_faces, axis=(2, 3))
# size reduces by 1
print ('2-
Averaged Corner Values for Each Face'
)
print ("c_faces
...",
c_faces)
print ('')
# color map and normalization c_grid
##c_min, c_max = np.min(c_faces),
np.max(c_faces) # could also be min and max of
c_faces
norm = mpl.colors.Normalize(vmin=c_min, vmax=c_max)
cmap = mpl.cm.viridis
###-stand
fig = plt.figure(2)
plt.title( "Plot
with averaged values")
ax = fig.add_subplot(111, projection= '3d')
surf = ax.plot_surface(x_grid, y_grid, z_grid,
rstride=1, cstride=1,
# no downsampling
facecolors = cmap(norm(c_faces)), shade= False)
cbar =
plt.colorbar(mpl.cm.ScalarMappable(norm=norm,
cmap=cmap), ax=ax, pad=0.2)
ax.dist = 8
plt.show()
#3-
from
mpl_toolkits.mplot3d
import
Axes3D
from
matplotlib
import
cm
from
matplotlib.ticker
import
LinearLocator,
FormatStrFormatter
#import matplotlib.pyplot as plt
#import numpy as np
fig = plt.figure(3)
ax = fig.add_subplot(111, projection= '3d')
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
my_col = cm.jet(Z/np.amax(Z))
print ('3-
3D Plot Projections'
)
print ("R...",
R)
print ('')
surf = ax.plot_surface(X, Y, Z, rstride=1,
cstride=1, facecolors = my_col,
linewidth=0, antialiased= False)
ax.set_zlim(-1.01, 1.01)
ax.set_title( '3D
Plot Projections')
plt.show()
#4-
# 3D surface having 2D contour plot
projections
# Import libraries
from
mpl_toolkits
import
mplot3d
#import numpy as np
#import matplotlib.pyplot as plt
# Creating dataset
x = np.outer(np.linspace(-3, 3, 32),
np.ones(32))
y = x.copy().T
# transpose
z = (np.sin(x **2) + np.cos(y **2) )
# Creating figure
fig = plt.figure(4)
#figsize =(14, 9))
ax = plt.axes(projection = '3d')
# Creating color map
my_cmap = plt.get_cmap( 'hot')
# Creating plot
surf = ax.plot_surface(x, y, z,
rstride = 8,
cstride = 8,
alpha = 0.8,
cmap = my_cmap)
cset = ax.contourf(x, y, z,
zdir = 'z',
offset = np.min(z),
cmap = my_cmap)
cset = ax.contourf(x, y, z,
zdir = 'x',
offset =-5,
cmap = my_cmap)
cset = ax.contourf(x, y, z,
zdir = 'y',
offset = 5,
cmap = my_cmap)
fig.colorbar(surf, ax = ax,
shrink = 0.5,
aspect = 5)
print ('4-
3D surface having 2D contour plot projections'
)
print (surf)
print ('')
# Adding labels
ax.set_xlabel( 'X-axis')
ax.set_xlim(-5, 5)
ax.set_ylabel( 'Y-axis')
ax.set_ylim(-5, 5)
ax.set_zlabel( 'Z-axis')
ax.set_zlim(np.min(z), np.max(z))
ax.set_title( '3D
surface having 2D contour plot projections')
# show plot
plt.show()
#5-
# Surface plot
# Import libraries
from
mpl_toolkits
import
mplot3d
#import numpy as np
#import matplotlib.pyplot as plt
# Creating dataset
x = np.outer(np.linspace(-3, 3, 32),
np.ones(32))
y = x.copy().T
# transpose
z = (np.sin(x **2) + np.cos(y **2) )
# Creating figure
fig = plt.figure(5)
#figsize =(14, 9))
ax = plt.axes(projection = '3d')
# Creating color map
my_cmap = plt.get_cmap( 'hot')
# Creating plot
surf = ax.plot_surface(x, y, z,
cmap = my_cmap,
edgecolor = 'none')
print ('5-
Surface plot'
)
print (surf)
print ('')
fig.colorbar(surf, ax = ax,
shrink = 0.5, aspect = 5)
ax.set_title( 'Surface
plot')
# show plot
plt.show()
#6
#- 3D surface ...
# Import libraries
from
mpl_toolkits
import
mplot3d
##import numpy as np
#import matplotlib.pyplot as plt
# Creating dataset
x = np.outer(np.linspace(-3, 3, 32),
np.ones(32))
y = x.copy().T
# transpose
z = (np.sin(x **2) + np.cos(y **2) )
# Creating figure
fig = plt.figure(6)
ax = plt.axes(projection = '3d')
# Creating plot
ax.plot_surface(x, y, z)
cpl= ax.plot_surface(x, y, z)
print ('6-
3D surface ...'
)
print (cpl)
print ('')
ax.set_title( '3D
surface ...')
# show plot
plt.show()
-------------------------------------------------------------------------------------------
#7-
from
mpl_toolkits.mplot3d
import
Axes3D
from
matplotlib
import
cm
from
matplotlib.ticker
import
LinearLocator,
FormatStrFormatter
#import matplotlib.pyplot as plt
#import numpy as np
fig = plt.figure(7)
ax = fig.add_subplot(111, projection= '3d')
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
my_col =
cm.jet(np.random.rand(Z.shape[0],Z.shape[1]))
surf = ax.plot_surface(X, Y, Z, rstride=1,
cstride=1, facecolors = my_col,
linewidth=0, antialiased= False)
ax.set_zlim(-1.01, 1.01)
print ('7-
linear colormap ...'
)
print ('R
= ...',
R)
print ('')
ax.set_title( 'linear
colormap')
plt.show()
|
|
| | |
|
|
|
|
- Debug of Python Project
Py-LM-3d, 0 Errors
Output of Python Project
Py-LM-3d:
| |
|
|
|
 |
| |
|
grid values
|
|
 |
| |
|
|
-
Plot with averaged values
|
|
 |
|
|
|
|
|
 |
|
|
|
|
-
3D surface having 2D contour plot
projections
|
|
 |
|
|
|
|
|
|
|
 |
|
|
|
|
|
 |
|
|
|
| | | |
| |
|
| |
|
| |
|
|
3- |
Py-LM-3d2dAdam
Python program,
to
implement the gradient descent optimization algorithm with Adam. |
|
|
| |
|
|
-
Py-LM-3d2dAdam- Python program, to
implement the gradient descent optimization
algorithm with Adam.
We will use a
simple two-dimensional function that squares the
input of each dimension and define the range of
valid inputs,
# objective function
def objective(x, y):
return x**2.0 + y**2.0
- creates a three-dimensional surface
plot of the objective function.
- create a two-dimensional plot of the
function, creates a contour plot of the
objective function
|
|
|
- Create a new Python project In Visual Studio
2022
- the
Microsoft Visual Studio (... or 2019 or 2022) is
a powerful IDE for Python language
- Open/Run
Microsoft Visual Studio 2022
- To view
Python templates, search for python.
 |
Select the
Python
Application template, and select
Next. |
- Create a new Python project In
Visual Studio 2022
On the Configure your new project screen
- (specify a name and file location
for the project, and then select Create)
Project name:
Py-LM-3d2dAdam
Location:
C:\Users\...\source\repos
(default location for Visual
Studio 2022)
|
- The new project opens in Visual Studio
2022 - (Visual Studio 2022
Compiler - IDE, to compile Python project /
file )
- The
Visual Studio Solution Explorer window shows the
project structure
- Python
Project Properieties - Py-LM-3d2dAdam
- Projct
Folder: C:\Users\...\source\repos\Py-LM-3d2dAdam
- Startup
File: Py_LM_3d2dAdam.py
- Project
file: Py-LM-3d2dAdam.sln
|
- Download Python
Project : Py-LM-3d2dAdam.zip -
(10.2 KB zip file) Download
- Project
consist of:
One Python Form -
(Py_LM_3d2dAdam.py )
|
 | |
|
Download Python
Project : Py-LM-3d2dAdam.zip -
(10.2 KB zip file) Download
|
|
|
- Install matplotlib and numpy in
Python on Windows Operating System,
Using Command Prompt
| 1- Install matplotlib in Python on Windows Operating System,
Using Command Prompt |
Step
1 - Run the Command Prompt on Windows
Operating System:
Command Prompt is an
application on most Windows computers that
directly communicates with the operating
system
How to open the Command Prompt:
Press the Windows key + r.
In the Run box, type cmd,
and then click Ok. |
|
 |
| |
Step
2 - install matplotlib:
In the Command prompt, use the following pip
command to install the matplotlib library
pip install matplotlib
|
|
 |
| |
| 2- Install numpy in Python on Windows Operating System,
Using Command Prompt |
| pip
install numpy |
 |
| |
| |
Source Code of Python Project
Py-LM-3d2dAdam:
|
Download this Source Code at
python file: Py_LM_3d2dAdam.py - (1.87 KB Python file)
download
|
 |
Continue
|
Py-LM-3d2dAdam Python program, to implement the
gradient descent optimization algorithm with
Adam | |
|
|
| |
# creates a three-dimensional surface plot of
the objective function -- x**2.0 + y**2.0 # 3d
plot of the test function
from
numpy
import
arange
from
numpy
import
meshgrid
from
matplotlib
import
pyplot
# objective function
def
objective(x,
y):
return
x**2.0 + y**2.0
# define range for input
r_min, r_max = -1.0, 1.0
# sample input range uniformly at 0.1
increments
xaxis = arange(r_min, r_max, 0.1)
yaxis = arange(r_min, r_max, 0.1)
# create a mesh from the axis
x, y = meshgrid(xaxis, yaxis)
# compute targets
results = objective(x, y)
print ('1-
3D surface plot of the objective function --
x**2.0 + y**2.0'
)
print ("results
= ...",
results)
print ('')
# create a surface plot with the jet color
scheme
figure = pyplot.figure(1)
#axis = figure.gca(projection='3d')
axis= figure.add_subplot(111, projection= '3d')
axis.plot_surface(x, y, results, cmap= 'jet')
axis.set_title( '3D
surface plot')
# show the plot
pyplot.show()
# create a two-dimensional plot of the
objective function -- x**2.0 + y**2.0,
# contour plot of the test function
from
numpy
import
asarray
#from numpy import arange
#from numpy import meshgrid
#from matplotlib import pyplot
# objective function
def
objective(x,
y):
return
x**2.0 + y**2.0
# define range for input
bounds = asarray([[-1.0, 1.0], [-1.0, 1.0]])
# sample input range uniformly at 0.1
increments
xaxis = arange(bounds[0,0], bounds[0,1], 0.1)
yaxis = arange(bounds[1,0], bounds[1,1], 0.1)
# create a mesh from the axis
x, y = meshgrid(xaxis, yaxis)
# compute targets
results = objective(x, y)
print ('2-
2D surface plot of objective function -- x**2.0
+ y**2.0'
)
print ("results
= ...",
results)
print ('')
# create a filled contour plot with 50 levels
and jet color scheme
figure = pyplot.figure(2)
pyplot.contourf(x, y, results, levels=50,
cmap= 'jet')
pyplot.title( '2D
surface plot')
# show the plot
pyplot.show() |
|
| | |
|
|
|
|
- Debug of Python Project
Py-LM-3d2dAdam, 0 Errors
Output of Python Project
Py-LM-3d2dAdam:
| |
|
|
-
the Visualizing data through bar plot
|
|
|
|
|
-
3D surface plot of the objective function
-- x**2.0 + y**2.0
|
|
 |
-
2D plot of the objective function --
x**2.0 + y**2.0
|
|
 |
|
|
|
| | | |
|