Graphing the Temperature Change From Climate Change in Python
by Quantalabs in Circuits > Software
1107 Views, 1 Favorites, 0 Comments
Graphing the Temperature Change From Climate Change in Python
Climate Change is a big problem. And a lot of people don't now how much it's risen. In this instructable, we'll graph the temperature change in climate due to climate change. For a cheat sheet, you can view the python file below.
Downloads
Supplies
You'll need:
- A Code Editor (I use the community version of PyCharm)
- Python v3.8 or newer
Downloading the Data
First, you'll need to download the data. If you want to graph something else, you can use a different dataset. I'm using a dataset from the NOAA. Here is the dataset. You can enter your own custom parameters and then click plot, scroll down, and you'll see an icon with a document and a X on it in the top left of the table. To make sure it's correct, hover over it and it should say Download data in CSV format. There are also some other csv files I've put below which you can use instead.
Uploading Your File to Your Python Project
To upload your file to the python project, first, make sure it's in the same folder on your computer. Next, type,
file = open("Name of the Dataset", "r")
data = file.readlines()
The open function opens a dataset and the r is for read. Although the file is opened, this just means that you are able to read it so we create another variable called data, which reads the file.
We next create a variable years. This is the years column of the dataset and will store them. So we type,
years = [ ]
Adding the Years Column to the Years Variable
To add the years column to the years variable, we run a for loop.
for line in data:
years.append(int(line.split(',')[0]))
The for loop runs the loop for every line. years.append appends whats in the parenthesis. The int function converts whats inside the parenthesis to an integer. Line.split(",") will split the contents of the line split at a comma and return an array, so we put [0] at the end to get the first element in the array, the year.
Creating a Temperature Variale and Adding the Temperatures to It
Because our .csv file is seperated by lines, to show there is a new line, we have \n at the end of every line to represent a new line. This means we have to do a little more work to get the temperature from the dataset. We start with the same code.
temp = [ ]
for line in data:
numlist = line.split(',')[1].split()
Notice that we have a second .split at the end of the last line. This will break it down to each character so if we have the word hello it will become h,e,l,l,o. We next have to get only the temperature from the array numlist.
num = float(''.join(numlist))
temp.append(num)
The variable num converts the joined version of the array numlist to a float. As we learned last lesson, the .append method appends it to the array.
Importing Pyplot From Matplotlib
To graph the temperatures, you have to import Pyplot.
from matplotlib import pyplot as plt
This now adds Pyplot to your project and to use any of its functions you call plt.functionName().
Graphing
To graph it we call the plot function. We then call the xlabel and ylabel to label our graph.
plt.plot(years,temp)
plt.ylabel('Temperature (C)')
plt.xlabel('Years')
plt.show()
The show function displays the graph.