Introduction: 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.
- A Code Editor (I use the community version of PyCharm)
- Python v3.8 or newer
Step 1: 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.
Step 2: 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 = [ ]
Step 3: 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:
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  at the end to get the first element in the array, the year.
Step 4: 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(',').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))
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.
Step 5: 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().
Step 6: Graphing
To graph it we call the plot function. We then call the xlabel and ylabel to label our graph.
The show function displays the graph.