Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi! I have a question about Python. For my project, I have to read temperatures

ID: 3917370 • Letter: H

Question

Hi! I have a question about Python. For my project, I have to read temperatures from a csv file and create a graph with those values. My graph needs to use the years 2000-2017 as x-axis labels, but also have tickmarks for the months. I also need to convert all the temperatures from Celsius to Fahrenheit. I assume I need to use a nested for loop to extract the data (one loop for the year and one for the month), but I am unsure how to write the loop, especially to keep the previous value when the loop runs again. Here is what I have so far:

import numpy as np
import matplotlib.pyplot as plt

def main():
temps = open(data.csv, "r")
for year == "2000":
for month == "1":
Oslo =
Harrisburg =
Honolulu =
Santaiago =
  
years = np.linspace(2000, 2017, 17)

plt.plot(years, Oslo, 'b-', label = "Oslo")
plt.plot(years, Harrisburg, 'r-', label = "Harriburg")
plt.plot(years, Honolulu, 'g-', label = "Honolulu")
plt.plot(years, Santiago, 'y-', label = "Santiago")
  
plt.legend(shadow = True, loc = "best")

plt.xticks(np.arange(0, 12, 1.0))
plt.yticks(np.arange(0, max(temperature), 10.0))

plt.xlabel('Year')
plt.ylabel('Temperature')

plt.title("Average Monthly Temperature in Four Cities")

plt.show()
  
  
main()

Thank you!

Explanation / Answer

Hello there is an alternate solution without drawing each time for a different loop

Oslo =[]

Harrisburg =[]

Honolulu =[]

Santaiago =[]

new_array=[]

for year in range(2000,2018):

for month in range(1,12):

Oslo.append(value1)

Harrisburg.append(value2)

Honolulu.append(value3)

Santaiaga.append(value4)

new_array.append(year)

plt.plot(new_array, Oslo, 'b-', label = "Oslo")

plt.plot(new_array, Harrisburg, 'r-', label = "Harriburg")

plt.plot(new_array, Honolulu, 'g-', label = "Honolulu")

plt.plot(new_array, Santiago, 'y-', label = "Santiago")

I am creating a new_array which has 12 times the same year value. So the first 12 values of the same country are mapped to the same year. So you can use tick symbol directly here itself and get the graph. Here I am assuming that you are filling the data into country arrays by year and month wise. So this algorithm will give you the required solution.

If you have any other doubts please ask it in comments session.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote