How to plot graph including y axis =value and x axis = datetime with timedelta (2 hrs) reading from csv file...












0












$begingroup$


I have dataset import from csv file. Here I want to plot graph with datetime including time delta (two hours). In x axis datetime should be visible like %d/%m/%Y %H:%M. y is value. I wrote the code and it gave me so many errors. Can anyone help me to solve this problem?
Here is my code:



condition = ""
date_time =
x1 =
x2 =
x3 =
def convertTime(s):
tm = time.strptime(s, " %d/%m/%Y %H:%M")
return datetime.datetime(tm.tm_year,tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec)
with open('data43.csv','r') as csv_file:
csv_data = csv.reader(csv_file, delimiter=',')
row_num = 0
for row in csv_data:
if(row_num == 0):
condition = row[0]
elif(row_num > 1): #Data starts here
if(row[0] != ''):
date_time.append(convertTime(row[0]))
if(row[1] != ''):
x1.append(int(row[1]))
if(row[2] != ''):
x2.append(int(row[2]))
if(row[3] != ''):
x3.append(int(row[3]))
row_num = row_num + 1
fig1 = plt.figure(1)
ax = fig1.add_subplot(2,1,1)
ax.plot(date_time,x1)
ax.stem(date_time,x2,'C1--','C1o',linefmt=None, markerfmt=None, basefmt=None)
ax.stem(date_time,x3,'C2--','C2o',linefmt=None, markerfmt=None, basefmt=None)
ax.legend()
ax.xaxis_date()
ax.get_xaxis().set_major_formatter(DateFormatter('%d/%m/%Y %H:%M:%S'))
plt.xlabel('t')
plt.ylabel('k')
leg = plt.legend( loc = 'upper right')
plt.draw() # Draw the figure so you can find the positon of the legend.
bb = leg.get_bbox_to_anchor().inverse_transformed(ax.transAxes)
xOffset = 0.3
bb.x0 += xOffset
bb.x1 += xOffset
leg.set_bbox_to_anchor(bb, transform = ax.transAxes)
plt.rcParams["figure.figsize"] = [20,20]
ax.plot(style='.-')
plt.show()


enter image description here



error :



enter image description here









share









$endgroup$

















    0












    $begingroup$


    I have dataset import from csv file. Here I want to plot graph with datetime including time delta (two hours). In x axis datetime should be visible like %d/%m/%Y %H:%M. y is value. I wrote the code and it gave me so many errors. Can anyone help me to solve this problem?
    Here is my code:



    condition = ""
    date_time =
    x1 =
    x2 =
    x3 =
    def convertTime(s):
    tm = time.strptime(s, " %d/%m/%Y %H:%M")
    return datetime.datetime(tm.tm_year,tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec)
    with open('data43.csv','r') as csv_file:
    csv_data = csv.reader(csv_file, delimiter=',')
    row_num = 0
    for row in csv_data:
    if(row_num == 0):
    condition = row[0]
    elif(row_num > 1): #Data starts here
    if(row[0] != ''):
    date_time.append(convertTime(row[0]))
    if(row[1] != ''):
    x1.append(int(row[1]))
    if(row[2] != ''):
    x2.append(int(row[2]))
    if(row[3] != ''):
    x3.append(int(row[3]))
    row_num = row_num + 1
    fig1 = plt.figure(1)
    ax = fig1.add_subplot(2,1,1)
    ax.plot(date_time,x1)
    ax.stem(date_time,x2,'C1--','C1o',linefmt=None, markerfmt=None, basefmt=None)
    ax.stem(date_time,x3,'C2--','C2o',linefmt=None, markerfmt=None, basefmt=None)
    ax.legend()
    ax.xaxis_date()
    ax.get_xaxis().set_major_formatter(DateFormatter('%d/%m/%Y %H:%M:%S'))
    plt.xlabel('t')
    plt.ylabel('k')
    leg = plt.legend( loc = 'upper right')
    plt.draw() # Draw the figure so you can find the positon of the legend.
    bb = leg.get_bbox_to_anchor().inverse_transformed(ax.transAxes)
    xOffset = 0.3
    bb.x0 += xOffset
    bb.x1 += xOffset
    leg.set_bbox_to_anchor(bb, transform = ax.transAxes)
    plt.rcParams["figure.figsize"] = [20,20]
    ax.plot(style='.-')
    plt.show()


    enter image description here



    error :



    enter image description here









    share









    $endgroup$















      0












      0








      0





      $begingroup$


      I have dataset import from csv file. Here I want to plot graph with datetime including time delta (two hours). In x axis datetime should be visible like %d/%m/%Y %H:%M. y is value. I wrote the code and it gave me so many errors. Can anyone help me to solve this problem?
      Here is my code:



      condition = ""
      date_time =
      x1 =
      x2 =
      x3 =
      def convertTime(s):
      tm = time.strptime(s, " %d/%m/%Y %H:%M")
      return datetime.datetime(tm.tm_year,tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec)
      with open('data43.csv','r') as csv_file:
      csv_data = csv.reader(csv_file, delimiter=',')
      row_num = 0
      for row in csv_data:
      if(row_num == 0):
      condition = row[0]
      elif(row_num > 1): #Data starts here
      if(row[0] != ''):
      date_time.append(convertTime(row[0]))
      if(row[1] != ''):
      x1.append(int(row[1]))
      if(row[2] != ''):
      x2.append(int(row[2]))
      if(row[3] != ''):
      x3.append(int(row[3]))
      row_num = row_num + 1
      fig1 = plt.figure(1)
      ax = fig1.add_subplot(2,1,1)
      ax.plot(date_time,x1)
      ax.stem(date_time,x2,'C1--','C1o',linefmt=None, markerfmt=None, basefmt=None)
      ax.stem(date_time,x3,'C2--','C2o',linefmt=None, markerfmt=None, basefmt=None)
      ax.legend()
      ax.xaxis_date()
      ax.get_xaxis().set_major_formatter(DateFormatter('%d/%m/%Y %H:%M:%S'))
      plt.xlabel('t')
      plt.ylabel('k')
      leg = plt.legend( loc = 'upper right')
      plt.draw() # Draw the figure so you can find the positon of the legend.
      bb = leg.get_bbox_to_anchor().inverse_transformed(ax.transAxes)
      xOffset = 0.3
      bb.x0 += xOffset
      bb.x1 += xOffset
      leg.set_bbox_to_anchor(bb, transform = ax.transAxes)
      plt.rcParams["figure.figsize"] = [20,20]
      ax.plot(style='.-')
      plt.show()


      enter image description here



      error :



      enter image description here









      share









      $endgroup$




      I have dataset import from csv file. Here I want to plot graph with datetime including time delta (two hours). In x axis datetime should be visible like %d/%m/%Y %H:%M. y is value. I wrote the code and it gave me so many errors. Can anyone help me to solve this problem?
      Here is my code:



      condition = ""
      date_time =
      x1 =
      x2 =
      x3 =
      def convertTime(s):
      tm = time.strptime(s, " %d/%m/%Y %H:%M")
      return datetime.datetime(tm.tm_year,tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec)
      with open('data43.csv','r') as csv_file:
      csv_data = csv.reader(csv_file, delimiter=',')
      row_num = 0
      for row in csv_data:
      if(row_num == 0):
      condition = row[0]
      elif(row_num > 1): #Data starts here
      if(row[0] != ''):
      date_time.append(convertTime(row[0]))
      if(row[1] != ''):
      x1.append(int(row[1]))
      if(row[2] != ''):
      x2.append(int(row[2]))
      if(row[3] != ''):
      x3.append(int(row[3]))
      row_num = row_num + 1
      fig1 = plt.figure(1)
      ax = fig1.add_subplot(2,1,1)
      ax.plot(date_time,x1)
      ax.stem(date_time,x2,'C1--','C1o',linefmt=None, markerfmt=None, basefmt=None)
      ax.stem(date_time,x3,'C2--','C2o',linefmt=None, markerfmt=None, basefmt=None)
      ax.legend()
      ax.xaxis_date()
      ax.get_xaxis().set_major_formatter(DateFormatter('%d/%m/%Y %H:%M:%S'))
      plt.xlabel('t')
      plt.ylabel('k')
      leg = plt.legend( loc = 'upper right')
      plt.draw() # Draw the figure so you can find the positon of the legend.
      bb = leg.get_bbox_to_anchor().inverse_transformed(ax.transAxes)
      xOffset = 0.3
      bb.x0 += xOffset
      bb.x1 += xOffset
      leg.set_bbox_to_anchor(bb, transform = ax.transAxes)
      plt.rcParams["figure.figsize"] = [20,20]
      ax.plot(style='.-')
      plt.show()


      enter image description here



      error :



      enter image description here







      python time-series csv





      share












      share










      share



      share










      asked 7 mins ago









      awaawa

      475




      475






















          0






          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "557"
          };
          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: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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%2fdatascience.stackexchange.com%2fquestions%2f45994%2fhow-to-plot-graph-including-y-axis-value-and-x-axis-datetime-with-timedelta%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
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Data Science Stack Exchange!


          • 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.


          Use MathJax to format equations. MathJax reference.


          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%2fdatascience.stackexchange.com%2fquestions%2f45994%2fhow-to-plot-graph-including-y-axis-value-and-x-axis-datetime-with-timedelta%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

          Ponta tanko

          Tantalo (mitologio)

          Erzsébet Schaár