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

How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?