Python Forum
"Desperate" in need of help plotting...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"Desperate" in need of help plotting...
#1
Hi,

I am undertaking an undergraduate in physics and need help plotting measurements for a curve. I am at an absolute novice level in Python.

The curve, itself, is not an issue, per say... It's the curve fitting I'm interested in.

My code is not beautiful, I know:
import matplotlib.pyplot as plt

ydata = [34, 33.3, 32.6, 31.8, 30.9, 29.9, 28.9, 27.9, 26.9, 26, 25.1, 24.3, 23.6, 22.8, 22.4, 21.9, 21.7, 21.6, 21.6, 21.8, 22, 22.4, 22.9, 23.5, 24.2, 24.9, 25.6, 26.4, 27.2, 28, 28.7, 29.4, 30, 30.6, 31.1, 31.5, 31.7, 31.9, 31.9, 31.8, 31.7, 31.4, 31.1, 30.7, 30.2, 29.7, 29, 28.4, 27.8, 27.2, 26.1, 26, 25.5, 25, 24.6, 24.3, 24.1, 23.9, 23.8, 23.8, 23.9, 24.1, 24.3, 24.6, 25, 25.4, 25.8, 26.3, 26.8, 27.3, 27.7, 28.2, 28.7, 29, 29.4, 29.7, 29.9, 30, 30.1, 30.2, 30.1, 30, 29.8, 29.7, 29.5, 29.3, 28.9, 28.6, 28.2, 27.8, 27.4, 27, 26.7, 26.3, 26, 25.7, 25.5, 25.3, 25.2, 25.2, 25.2, 25.2, 25.3, 25.5, 25.6, 25.9, 26.1, 26.4, 26.7, 27, 27.3, 27.6, 27.9, 28.2, 28.4, 28.7, 28.9, 29, 29.2, 29.2, 29.2, 29.2, 29.2, 29.1, 28.9, 28.8, 28.6, 28.4, 28.2, 28, 27.7, 27.5, 27.3, 27, 26.8, 26.6, 26.5, 26.4, 26.3, 26.2, 26.2, 26.2, 26.2, 26.3, 26.4, 26.4, 26.6, 26.7, 26.9, 27.1, 27.2, 27.4, 27.6, 27.8, 28, 28.1, 28.2, 28.3, 28.4, 28.5, 28.5, 28.6, 28.6, 28.5, 28.3, 28.3, 28.2, 28.1, 28, 27.8, 27.7, 27.5, 27.4, 27.2, 27.1, 26.9, 26.8, 26.7, 26.7, 26.6]
xdata = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180]
plt.scatter(xdata, ydata)

cur_axes = plt.gca()
cur_axes.axes.get_xaxis().set_visible(False)


plt.title("")
xerr = 0
yerr = 0


plt.savefig("m1.pdf")

plt.show()
It outputs:
[Image: 93a24f574765927117a7f545d4c26d53.png]

It might jag be me who's picky but I would very much like a smooth curve to fit my measurements.

The date for the rapport is due tomorrow, so help would be VERY apprecieated!
PS. I do not have time for any "deeper" lectures in Python at this moment, I really need someone to hold my hand while I'm doing this so I can focus on my actual lectures which for this lab is electro magnetism...

Thanks a lot in advance!
Reply
#2
How did you generate the numbers, and are you sure they're right?
Reply
#3
All the y-values are measurements in CM and the values on the x-axis marks the y-values taken with 15 seconds intervals. Everything done by hand with "human" miscalculations... If you understand what I mean...
Reply
#4
Use interpolation with scipy.interpolate
import numpy as np
from scipy.interpolate import interp1d
plt.scatter(xdata, ydata, marker='.')
f = interp1d(xdata, ydata, kind='cubic')
x = np.linspace(min(xdata), max(xdata), num=200)
plt.plot(x, f(x))
Reply
#5
Wow! Super nice.. That's progress!
Is it possible to get the lines to be smooth kind of like in the screenshot below or is that a completely different approach?
[Image: 8412eb0a56f3cbc0a52ea130829d6616.png]

It's kind of like a trend line for a linear curve, only the curve is not linear... =P
Reply
#6
You could perhaps use scipy.optimize.curve_fit . It may work well on your data because the result looks like a damped sine wave
Reply
#7
Yes, I know. It's just that it lacks an actual function. ;)

After adding that package, how would I actually use it to add the fitted curve?
Reply


Forum Jump:

User Panel Messages

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