How to create a BSpline or polyfit graph where the x values are datetime objects?How do I sort a list of dictionaries by a value of the dictionary?How to return only the Date from a SQL Server DateTime datatypeGiven a DateTime object, how do I get an ISO 8601 date in string format?How do you set a default value for a MySQL Datetime column?How can I safely create a nested directory?How to know if an object has an attribute in PythonHow do I sort a dictionary by value?How do I convert datetime to date (in Python)?How to access environment variable values?Issue with xticklabels when saving a figure with matplotlib

2010 (?) science fiction TV show with new world

What is more environmentally friendly? An A320 or a car?

Why is my fluorescent tube orange on one side, white on the other and dark in the middle?

Do the books ever say oliphaunts aren’t elephants?

Omnidirectional LED, is it possible?

Is SecureRandom.ints() secure?

Assuring luggage isn't lost with short layover

Did Vladimir Lenin have a cat?

Why is it considered acid rain with pH <5.6?

Self-deportation of American Citizens from US

What are the cons of stateless password generators?

How to store my pliers and wire cutters on my desk?

A cubeful of three-dimensional devilry

Wrapping IMemoryCache with SemaphoreSlim

Scam? Checks via Email

A variant of the Multiple Traveling Salesman Problem

Blank spaces in a font

Why did House of Representatives need to condemn Trumps Tweets?

How to efficiently shred a lot of cabbage?

Why does aggregate initialization not work anymore since C++20 if a constructor is explicitly defaulted or deleted?

Do 3/8 (37.5%) of Quadratics Have No x-Intercepts?

Does dual boot harm a laptop battery or reduce its life?

How well would the Moon protect the Earth from an Asteroid?

What would the United Kingdom's "optimal" Brexit deal look like?



How to create a BSpline or polyfit graph where the x values are datetime objects?


How do I sort a list of dictionaries by a value of the dictionary?How to return only the Date from a SQL Server DateTime datatypeGiven a DateTime object, how do I get an ISO 8601 date in string format?How do you set a default value for a MySQL Datetime column?How can I safely create a nested directory?How to know if an object has an attribute in PythonHow do I sort a dictionary by value?How do I convert datetime to date (in Python)?How to access environment variable values?Issue with xticklabels when saving a figure with matplotlib






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















So for a school project I am designing a program that creates a graph of tide heights over time. BSpline doesn't take in datetime objects, and neither does np.linspace which from what I have seen is necessary for BSpline to run.



I have tried using pandas to convert the datetime to a value np can read, or using datetime64. In the code below I have removed all my failed methods and just left the bare bones.



import matplotlib.pyplot as pyplot
from matplotlib.dates import date2num, datestr2num
import numpy as np
import matplotlib.dates as mdates
from scipy.interpolate import make_interp_spline,BSpline

test_xvals = [datetime(1900, 1, 1, 1, 28), datetime(1900, 1, 1, 5, 47), datetime(1900, 1, 1, 13, 7), datetime(1900, 1, 1, 20, 43)] #times which correspond to the height of the tides
test_yvals = [3.3, 3.9, 0.8, 4.5] #height of the tides in meters

times = date2num(test_xvals) #converts to object compatible with numpy and matplotlib
heights = np.array(test_yvals) #array of 4 floats

x_smooth = np.linspace(times.min(), times.max(), 300)
spl = make_interp_spline(times,heights , k=3) #BSpline object
y_smooth = spl(x_smooth)


fig, ax = plt.subplots()
ax.plot(x_smooth, y_smooth)
ax.scatter(times,heights)
ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))
#pyplot.xticks(dates,xlabels)
pyplot.ylim(0,6)


The sine graph does not match the data points. What attributes of BSpline should I tweak to make the data points match up with local max/mins?
Graph with out of sync data points










share|improve this question


























  • Do you expect us to guess where the error occurs?

    – hpaulj
    Mar 26 at 20:58











  • Sorry! Updated it.

    – Inbar AS
    Mar 26 at 21:05






  • 1





    Reread the BSpline docs.

    – hpaulj
    Mar 26 at 21:17











  • Rereading the BSpline docs was not helpful, but I finally found a stack overflow question that answered a similar issue and got my graph to curve. However, those curves do not line up with the actual data points (I updated the code and included a picture of the output). How can I fix that?

    – Inbar AS
    Mar 27 at 0:34











  • The thing that stood out to me was that third argument, k, is supposed to be a single number. You gave it an array, from linspace.

    – hpaulj
    Mar 27 at 0:49

















1















So for a school project I am designing a program that creates a graph of tide heights over time. BSpline doesn't take in datetime objects, and neither does np.linspace which from what I have seen is necessary for BSpline to run.



I have tried using pandas to convert the datetime to a value np can read, or using datetime64. In the code below I have removed all my failed methods and just left the bare bones.



import matplotlib.pyplot as pyplot
from matplotlib.dates import date2num, datestr2num
import numpy as np
import matplotlib.dates as mdates
from scipy.interpolate import make_interp_spline,BSpline

test_xvals = [datetime(1900, 1, 1, 1, 28), datetime(1900, 1, 1, 5, 47), datetime(1900, 1, 1, 13, 7), datetime(1900, 1, 1, 20, 43)] #times which correspond to the height of the tides
test_yvals = [3.3, 3.9, 0.8, 4.5] #height of the tides in meters

times = date2num(test_xvals) #converts to object compatible with numpy and matplotlib
heights = np.array(test_yvals) #array of 4 floats

x_smooth = np.linspace(times.min(), times.max(), 300)
spl = make_interp_spline(times,heights , k=3) #BSpline object
y_smooth = spl(x_smooth)


fig, ax = plt.subplots()
ax.plot(x_smooth, y_smooth)
ax.scatter(times,heights)
ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))
#pyplot.xticks(dates,xlabels)
pyplot.ylim(0,6)


The sine graph does not match the data points. What attributes of BSpline should I tweak to make the data points match up with local max/mins?
Graph with out of sync data points










share|improve this question


























  • Do you expect us to guess where the error occurs?

    – hpaulj
    Mar 26 at 20:58











  • Sorry! Updated it.

    – Inbar AS
    Mar 26 at 21:05






  • 1





    Reread the BSpline docs.

    – hpaulj
    Mar 26 at 21:17











  • Rereading the BSpline docs was not helpful, but I finally found a stack overflow question that answered a similar issue and got my graph to curve. However, those curves do not line up with the actual data points (I updated the code and included a picture of the output). How can I fix that?

    – Inbar AS
    Mar 27 at 0:34











  • The thing that stood out to me was that third argument, k, is supposed to be a single number. You gave it an array, from linspace.

    – hpaulj
    Mar 27 at 0:49













1












1








1








So for a school project I am designing a program that creates a graph of tide heights over time. BSpline doesn't take in datetime objects, and neither does np.linspace which from what I have seen is necessary for BSpline to run.



I have tried using pandas to convert the datetime to a value np can read, or using datetime64. In the code below I have removed all my failed methods and just left the bare bones.



import matplotlib.pyplot as pyplot
from matplotlib.dates import date2num, datestr2num
import numpy as np
import matplotlib.dates as mdates
from scipy.interpolate import make_interp_spline,BSpline

test_xvals = [datetime(1900, 1, 1, 1, 28), datetime(1900, 1, 1, 5, 47), datetime(1900, 1, 1, 13, 7), datetime(1900, 1, 1, 20, 43)] #times which correspond to the height of the tides
test_yvals = [3.3, 3.9, 0.8, 4.5] #height of the tides in meters

times = date2num(test_xvals) #converts to object compatible with numpy and matplotlib
heights = np.array(test_yvals) #array of 4 floats

x_smooth = np.linspace(times.min(), times.max(), 300)
spl = make_interp_spline(times,heights , k=3) #BSpline object
y_smooth = spl(x_smooth)


fig, ax = plt.subplots()
ax.plot(x_smooth, y_smooth)
ax.scatter(times,heights)
ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))
#pyplot.xticks(dates,xlabels)
pyplot.ylim(0,6)


The sine graph does not match the data points. What attributes of BSpline should I tweak to make the data points match up with local max/mins?
Graph with out of sync data points










share|improve this question
















So for a school project I am designing a program that creates a graph of tide heights over time. BSpline doesn't take in datetime objects, and neither does np.linspace which from what I have seen is necessary for BSpline to run.



I have tried using pandas to convert the datetime to a value np can read, or using datetime64. In the code below I have removed all my failed methods and just left the bare bones.



import matplotlib.pyplot as pyplot
from matplotlib.dates import date2num, datestr2num
import numpy as np
import matplotlib.dates as mdates
from scipy.interpolate import make_interp_spline,BSpline

test_xvals = [datetime(1900, 1, 1, 1, 28), datetime(1900, 1, 1, 5, 47), datetime(1900, 1, 1, 13, 7), datetime(1900, 1, 1, 20, 43)] #times which correspond to the height of the tides
test_yvals = [3.3, 3.9, 0.8, 4.5] #height of the tides in meters

times = date2num(test_xvals) #converts to object compatible with numpy and matplotlib
heights = np.array(test_yvals) #array of 4 floats

x_smooth = np.linspace(times.min(), times.max(), 300)
spl = make_interp_spline(times,heights , k=3) #BSpline object
y_smooth = spl(x_smooth)


fig, ax = plt.subplots()
ax.plot(x_smooth, y_smooth)
ax.scatter(times,heights)
ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))
#pyplot.xticks(dates,xlabels)
pyplot.ylim(0,6)


The sine graph does not match the data points. What attributes of BSpline should I tweak to make the data points match up with local max/mins?
Graph with out of sync data points







python numpy datetime matplotlib bspline






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 27 at 0:32







Inbar AS

















asked Mar 26 at 20:33









Inbar ASInbar AS

62 bronze badges




62 bronze badges















  • Do you expect us to guess where the error occurs?

    – hpaulj
    Mar 26 at 20:58











  • Sorry! Updated it.

    – Inbar AS
    Mar 26 at 21:05






  • 1





    Reread the BSpline docs.

    – hpaulj
    Mar 26 at 21:17











  • Rereading the BSpline docs was not helpful, but I finally found a stack overflow question that answered a similar issue and got my graph to curve. However, those curves do not line up with the actual data points (I updated the code and included a picture of the output). How can I fix that?

    – Inbar AS
    Mar 27 at 0:34











  • The thing that stood out to me was that third argument, k, is supposed to be a single number. You gave it an array, from linspace.

    – hpaulj
    Mar 27 at 0:49

















  • Do you expect us to guess where the error occurs?

    – hpaulj
    Mar 26 at 20:58











  • Sorry! Updated it.

    – Inbar AS
    Mar 26 at 21:05






  • 1





    Reread the BSpline docs.

    – hpaulj
    Mar 26 at 21:17











  • Rereading the BSpline docs was not helpful, but I finally found a stack overflow question that answered a similar issue and got my graph to curve. However, those curves do not line up with the actual data points (I updated the code and included a picture of the output). How can I fix that?

    – Inbar AS
    Mar 27 at 0:34











  • The thing that stood out to me was that third argument, k, is supposed to be a single number. You gave it an array, from linspace.

    – hpaulj
    Mar 27 at 0:49
















Do you expect us to guess where the error occurs?

– hpaulj
Mar 26 at 20:58





Do you expect us to guess where the error occurs?

– hpaulj
Mar 26 at 20:58













Sorry! Updated it.

– Inbar AS
Mar 26 at 21:05





Sorry! Updated it.

– Inbar AS
Mar 26 at 21:05




1




1





Reread the BSpline docs.

– hpaulj
Mar 26 at 21:17





Reread the BSpline docs.

– hpaulj
Mar 26 at 21:17













Rereading the BSpline docs was not helpful, but I finally found a stack overflow question that answered a similar issue and got my graph to curve. However, those curves do not line up with the actual data points (I updated the code and included a picture of the output). How can I fix that?

– Inbar AS
Mar 27 at 0:34





Rereading the BSpline docs was not helpful, but I finally found a stack overflow question that answered a similar issue and got my graph to curve. However, those curves do not line up with the actual data points (I updated the code and included a picture of the output). How can I fix that?

– Inbar AS
Mar 27 at 0:34













The thing that stood out to me was that third argument, k, is supposed to be a single number. You gave it an array, from linspace.

– hpaulj
Mar 27 at 0:49





The thing that stood out to me was that third argument, k, is supposed to be a single number. You gave it an array, from linspace.

– hpaulj
Mar 27 at 0:49












0






active

oldest

votes










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55365768%2fhow-to-create-a-bspline-or-polyfit-graph-where-the-x-values-are-datetime-objects%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes




Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.







Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55365768%2fhow-to-create-a-bspline-or-polyfit-graph-where-the-x-values-are-datetime-objects%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript