1

I have created a program that gets data and creates 2 lists:

1) Date_list - which contains dates.

2) Data - which contains some data.

At the moment, it plots the date_list in the x axis and the data in the y axis. But my graph is for some reason connecting the start and end points with an additional line. I would like to disable that... How can I do it?

The image below shows my issue.

enter image description here

Here's my code:

from notion.client import NotionClient
from datetime import datetime
from matplotlib import pyplot as plt
import matplotlib.dates as mdates
import numpy
import pandas as pd

# Obtain the `token_v2` value by inspecting your browser cookies on a logged-in session on Notion.so
client = NotionClient(token_v2="")

# Replace this URL with the URL of the page you want to edit
page = client.get_collection_view("")

all_rows = list(page.build_query().execute())
all_rows_data = [row.get_all_properties() for row in all_rows]
date_list = []
data = []

# Add the data of the dates and the respective screen time into 2 separate lists.
for item in all_rows_data:
    date_time = item.get('title') + ' 2020'
    screen_time = item.get('screen_time')
    datetime_object = datetime.strptime(date_time, '%d %B %Y')
    #print(datetime_object, screen_time)
    date_list.append(datetime_object)
    data.append(screen_time)

# Plot a graph using Matplotlib
ax = plt.gca() #get axes

formatter = mdates.DateFormatter("%d %B %Y") #format as a date
ax.xaxis.set_major_formatter(formatter)

locator = mdates.DayLocator() #set locator
ax.xaxis.set_major_locator(locator)

fig = plt.figure(figsize=(15, 5)) # Plot graph
ax = fig.add_subplot(111)
ax.plot(date_list, data, color='red', marker='o', markerfacecolor='blue', markersize=8)
ax.plot()
plt.show()

Any code suggestions are welcome too!

Thanks in advance,

Karthik

JohanC
  • 71,591
  • 8
  • 33
  • 66
  • 2
    The problem seems to be that your dates aren't sorted. The first date seems to be very recent, while the rest are ordered from old to new. When correcting this, be careful not to lose the correspondence between dates and values. – JohanC Jun 07 '20 at 11:41
  • Brilliant! That worked! I just did - sorted_list = sorted(date_list) and then I used the sorted_list instead. Thank you! – Karthik Nandula Jun 07 '20 at 11:47
  • 1
    But if you only sort the date_list, the values don't correspond anymore to their dates. You could try something like `date_list, data = zip(*sorted(zip(date_list, data)))`, see e.g. [How to sort two lists (which reference each other) in the exact same way ?](https://stackoverflow.com/questions/9764298/how-to-sort-two-lists-which-reference-each-other-in-the-exact-same-way) – JohanC Jun 07 '20 at 11:53
  • See e.g. [matplotlib: data points connected in wrong order in line graph](https://stackoverflow.com/questions/17786785/matplotlib-data-points-connected-in-wrong-order-in-line-graph) about the main cause of weird connections – JohanC Jun 07 '20 at 11:59
  • Thanks for that! I completely forgot about the correpondance. It works now. – Karthik Nandula Jun 07 '20 at 15:49

0 Answers0