Odd indentation near origin of graph (sometimes)

  • Thread starter Thread starter MathewsMD
  • Start date Start date
  • Tags Tags
    Graph Origin
Click For Summary

Discussion Overview

The discussion revolves around the appearance of an odd indentation in a 3D graph generated by a Gaussian function, particularly when the graph is centered near the origin. Participants explore the implications of using random values for the centroid and the choice of mesh type in the graphing process.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant notes that the graph sometimes shows an odd indentation when the centroid is set close to the origin, questioning if this is expected behavior for a 2D Gaussian.
  • Another participant suggests that setting the centroid before determining the mesh grid might help center the graph as desired, but questions whether this would defeat the purpose of the random generation.
  • A third participant asserts that the indentation is an artifact of the mesh used and provides modified code to allow for both polar and Cartesian meshes, along with options for specifying the centroid and variance.
  • Further clarification is provided on how the modified code can be executed with specific parameters to observe the effects on the graph.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the cause of the indentation, with some suggesting it is an artifact of the mesh while others explore different approaches to centering the graph. The discussion remains unresolved regarding the best method to achieve the desired graph appearance.

Contextual Notes

The discussion highlights the dependence on mesh choice and the random nature of the centroid, which may influence the visual output of the Gaussian plot. There are unresolved aspects regarding the implications of these choices on the graph's appearance.

MathewsMD
Messages
430
Reaction score
7
Code:
from mpl_toolkits.mplot3d
import Axes3D
import matplotlib
import numpy as np
from matplotlib import cm
from matplotlib import pyplot as plt
import numpy as np
import matplotlib.pylab as plt
import math
import random from scipy
import integrate

step = 0.1
maxval = 1.0
fig = plt.figure()
ax = Axes3D(fig)
R1 = 4.
R2 = 7.
r = np.linspace(0,20,100)
p = np.linspace(0,2*np.pi,100)
R,P = np.meshgrid(r,p)
X,Y = R*np.cos(P),R*np.sin(P)
sigma0 = random.randint(4000., 7000.)/1000.
r0 = random.randint(0, R1*1000.)/1000. #random centroid
theta0 = random.uniform(0, np.pi*2)

Z = (np.e**((-(R**2 + r0**2- 2*R*r0*(np.cos(P)*np.cos(theta0) +  np.sin(P)*np.sin(theta0)))/(2*sigma0**2))))

ax.plot_surface(X, Y, Z, rstride=2, cstride=2, cmap=cm.jet)
ax.set_zlim3d(0,1)
ax.set_zlabel('Intensity')
plt.show()

When I run this code, it works. But due to the random number generator, sometimes the input values have the graph centred near the origin (e.g. set r0 = 5), and when this occurs, there seems to be an odd indentation in the graph itself. Maybe I'm missing something, but if I'm trying to display a 2D Gaussian, this shouldn't be there, right? If anyone has any thoughts on why this is occurring and any ideas to approach this problem, that would be greatly appreciated!
 
Last edited:
Technology news on Phys.org
I don't understand what you mean.

If you set r0 first and THEN determine r and p around it...would that center it as you desired? or would that defeat the purpose of what you are trying to do?
 
MathewsMD said:
Maybe I'm missing something, but if I'm trying to display a 2D Gaussian, this shouldn't be there, right?
It's an artifact of your mesh. I've modified your code a bit so I can choose between a polar mesh or a cartesian mesh, and so I can optionally specify the centroid and variance:
Code:
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from matplotlib import cm
from matplotlib import pyplot as plt
from scipy import random
import argparse

parser = argparse.ArgumentParser(description='Plot a 2D Gaussian.')
parser.add_argument(
    '-p', '--polar',
    help = 'plot with polar X,Y mesh (default: cartesian mesh)',
    action = 'store_true')
parser.add_argument(
    '-c', '--centroid',
    help = 'centroid coordinates (default: random)',
    type = float,
    nargs = 2)
parser.add_argument(
    '-s', '--sigma',
    help = 'variance (sigma) (default: random)',
    type = float,
    nargs = 1)
args = parser.parse_args()

if args.polar :
    r = np.linspace(0.0,20.0,100.0)
    p = np.linspace(0.0,2*np.pi,100.0)
    R,P = np.meshgrid(r,p)
    X,Y = R*np.cos(P),R*np.sin(P)
else :
    x = np.linspace(-20.0,20.0,101.0)
    y = np.linspace(-20.0,20.0,101.0)
    X,Y = np.meshgrid(x,y)

if args.centroid is not None :
    x0,y0 = args.centroid
else :
    x0,y0 = random.uniform(-4.0,4.0), random.uniform(-4.0,4.0)

if args.sigma is not None :
    sigma0 = args.sigma[0]
else :
    sigma0 = random.uniform(4.0, 7.0)

Z = np.e**(-((X-x0)**2 + (Y-y0)**2)/(2*sigma0**2))

fig = plt.figure()
ax = Axes3D(fig)
ax.plot_surface(X, Y, Z, rstride=2, cstride=2, cmap=cm.jet)
ax.set_zlim3d(0,1)
ax.set_zlabel('Intensity')
plt.show()

I get the following plot when I run this via python plot.py --centroid -5 5:
cartesian.jpg
Using a polar mesh (python plot.py --centroid -5 5 --polar) yields the following:
polar.jpg
 
  • Like
Likes   Reactions: MathewsMD
D H said:
It's an artifact of your mesh. I've modified your code a bit so I can choose between a polar mesh or a cartesian mesh, and so I can optionally specify the centroid and variance:
Code:
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from matplotlib import cm
from matplotlib import pyplot as plt
from scipy import random
import argparse

parser = argparse.ArgumentParser(description='Plot a 2D Gaussian.')
parser.add_argument(
    '-p', '--polar',
    help = 'plot with polar X,Y mesh (default: cartesian mesh)',
    action = 'store_true')
parser.add_argument(
    '-c', '--centroid',
    help = 'centroid coordinates (default: random)',
    type = float,
    nargs = 2)
parser.add_argument(
    '-s', '--sigma',
    help = 'variance (sigma) (default: random)',
    type = float,
    nargs = 1)
args = parser.parse_args()

if args.polar :
    r = np.linspace(0.0,20.0,100.0)
    p = np.linspace(0.0,2*np.pi,100.0)
    R,P = np.meshgrid(r,p)
    X,Y = R*np.cos(P),R*np.sin(P)
else :
    x = np.linspace(-20.0,20.0,101.0)
    y = np.linspace(-20.0,20.0,101.0)
    X,Y = np.meshgrid(x,y)

if args.centroid is not None :
    x0,y0 = args.centroid
else :
    x0,y0 = random.uniform(-4.0,4.0), random.uniform(-4.0,4.0)

if args.sigma is not None :
    sigma0 = args.sigma[0]
else :
    sigma0 = random.uniform(4.0, 7.0)

Z = np.e**(-((X-x0)**2 + (Y-y0)**2)/(2*sigma0**2))

fig = plt.figure()
ax = Axes3D(fig)
ax.plot_surface(X, Y, Z, rstride=2, cstride=2, cmap=cm.jet)
ax.set_zlim3d(0,1)
ax.set_zlabel('Intensity')
plt.show()

I get the following plot when I run this via python plot.py --centroid -5 5:View attachment 83929Using a polar mesh (python plot.py --centroid -5 5 --polar) yields the following:
View attachment 83930


Thank you so much for the help!
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K