Python Forum
Plotting simple graph (AttributeError)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Plotting simple graph (AttributeError)
#1
Hey,

I'm trying to plot energy as a function of My. I want to plot the function for values of n between 1-10, but the code returns an AttributeError. I tried putting the My value straight into the equation (without defining 'bragg' first), but that doesn't work either, the graphs are all flat.

import numpy as np
import matplotlib.pyplot as plt

h = 2
k = 2
l = 0

a = 5.431020511 * 0.0001

R = 250 * 1000

d = a / np.sqrt(h**2+k**2+l**2)

hc = 1.23984198 

My = range(0,100)

def bragg(My):
    bragg = 2*np.pi + np.arccos(np.sqrt(My)/np.sqrt(R))
    return bragg

def energy(bragg, n):
    E = (2 * d * np.sin(bragg) / (n*hc))
    return E

fig = plt.figure()
fig.suptitle('Energy')
plt.plot(My, energy(bragg,1), label='1st')
plt.plot(My, energy(bragg,2), label='2nd')
plt.plot(My, energy(bragg,3), label='3rd')
plt.plot(My, energy(bragg,4), label='4th')
plt.plot(My, energy(bragg,5), label='5th')
plt.plot(My, energy(bragg,6), label='6th')
plt.xlabel('My')
plt.ylabel('Energy (eV)')
plt.legend()
The error given is this:

E = (2 * d * np.sin(bragg) / (n*hc))

AttributeError: 'function' object has no attribute 'sin'

<matplotlib.figure.Figure at 0x218b44eb208>
How can I proceed from this? I've never gotten an AttributeError before, so I don't know how to fix it. Any help is greatly appreciated!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Exclamation Error when plotting a graph. Oshadha 3 3,637 Mar-14-2022, 06:47 PM
Last Post: deanhystad
  Plotting sum of data files using simple code Laplace12 3 3,036 Jun-16-2021, 02:06 PM
Last Post: BashBedlam
  Plotting “simple” diagram with gridlines at specified intervals schniefen 1 2,407 Dec-03-2020, 05:54 PM
Last Post: schniefen
  Simple plotting from txt file Laplace12 2 2,259 Aug-23-2020, 09:58 AM
Last Post: Laplace12
  Graph not plotting Hass 1 1,952 Sep-14-2019, 09:52 AM
Last Post: luoheng
  Plotting number on graph william888 1 1,706 Sep-02-2019, 02:36 AM
Last Post: scidam
  Graph Plotting Help Talch 1 2,228 Aug-16-2018, 10:29 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020