I have web scraped some dates from the worldmeter website and i want to display it on a graph. However, it is coming up with
ValueError: x and y must have same first dimension, but have shapes (1,) and (8000,)
Can somebody please help me amend this code to make it work.
This is my code so far:
import tkinter as tk #this imports the tkinter library
from tkinter import ttk
from bs4 import BeautifulSoup
import requests
import matplotlib.pyplot as plt
root= tk.Tk()
URL = "https://www.worldometers.info/coronavirus/country/us/"
page = requests.get(URL)
soup = BeautifulSoup(page.content, "html.parser")
results=soup.find_all("div", {"class": "col-md-12"})
data=results[4]
script = data.find('script')
stringCon = script.string
x = stringCon.strip()[291:-9258] #these are the dates i have web scraped that i want on the
x-axis
y=[]
for i in range(0, len(x)):
y.append(i+1) #this for loop is to produce data for the y axis
plt.style.use('seaborn-dark')
plt.plot(x, y, label = "US")
plt.title('Daily Cases')
plt.xlabel('Date')
plt.ylabel('Cases')
plt.grid(True)
plt.legend()
plt.show()
root.mainloop()
Error:
Traceback (most recent call last):
File "C:\Users\teera\OneDrive\Desktop\School\Subjects\Computer Science\Python Practice\Python Scrape\creating graphs.py", line 156, in <module>
plt.plot(x, y, label = "US") #plots the re against the dates and labels the graph as the country's name
File "C:\Users\teera\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\pyplot.py", line 3019, in plot
return gca().plot(
File "C:\Users\teera\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\axes\_axes.py", line 1605, in plot
lines = [*self._get_lines(*args, data=data, **kwargs)]
File "C:\Users\teera\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\axes\_base.py", line 315, in __call__
yield from self._plot_args(this, kwargs)
File "C:\Users\teera\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\axes\_base.py", line 501, in _plot_args
raise ValueError(f"x and y must have same first dimension, but "
ValueError: x and y must have same first dimension, but have shapes (1,) and (8000,)
The elements of your x
variable are strings (print(type(x[0]))
returns <class 'str'>
), so plt is not sure how to interpret that.