Create a temperature and precipitation chart using Python on Google Colab



Creating a Temperature and Precipitation Chart Using Python on Google Colab:

With code source at the end of the article💥💥




The Python programming language is one of the powerful and useful tools for data analysis and creating visualizations, thanks to its rich libraries like matplotlib and pandas. In this article, we will demonstrate the steps to create a chart showing monthly temperature and precipitation data.

Google Colab is a free platform that provides an integrated environment for programming in Python online, with access to powerful libraries like matplotlib and pandas. Colab enables you to analyze data and create visualizations easily using cloud resources, eliminating the need to install software on your device.

In this article, we will explain how to set up and create a temperature and precipitation chart step by step using Google Colab.


Detailed Steps

Step 1: Setting up Google Colab

  1. Open Colab:

  2. Create a new project:

    • Click on New Notebook to create a new notebook.

Step 2: Importing Necessary Libraries

In the first cell, import the libraries you will need using the following code:

import matplotlib.pyplot as plt  
import pandas as pd  
import numpy as np  
  • matplotlib: For creating visualizations.
  • pandas: For data processing.
  • numpy: For handling numerical data.

Run the cell by clicking the Run button (or pressing Shift + Enter).


Step 3: Preparing the Data

You can input the data manually or load it from the Power Data website (instructions for downloading from the website are included in the video below the article).


Step 4: Creating the Chart

In this step, we will create a combined chart that includes:

  • A line representing temperature variations.
  • Bars representing precipitation amounts.

Download code source: 

import matplotlib.pyplot as plt

# Sample data (replace with your data if needed)
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
precipitation = [0.0, 0.0, 2.21, 0.0, 0.0, 2.08, 0.32, 1.12, 0.22, 27.79, 0.06, 0.11]  # Precipitation in mm
temperature = [16.96, 18.62, 18.2, 18.65, 19.24, 20.39, 20.43, 21.27, 21.83, 21.33, 19.73, 18.3]  # Actual temperature data (°C)

# Create the figure and axis
fig, ax1 = plt.subplots(figsize=(10, 6))

# Left axis (precipitation)
bars = ax1.bar(months, precipitation, color='skyblue', alpha=0.7, label='Precipitation (mm)')
ax1.set_ylabel('Precipitation (mm)', color='blue')
ax1.set_ylim(0, max(precipitation) + 10)  # Adjust maximum limit for precipitation
ax1.tick_params(axis='y', labelcolor='blue')

# Right axis (temperature)
ax2 = ax1.twinx()
line, = ax2.plot(months, temperature, color='red', marker='o', linestyle='-', label='Temperature (°C)')
ax2.set_ylabel('Temperature (°C)', color='red')
ax2.set_ylim(0, max(temperature) + 5)  # Adjust maximum limit for temperature
ax2.tick_params(axis='y', labelcolor='red')

# Add a title
plt.title('Température moyenne et précipitations à Fès en 2019', fontsize=14)

# Add legends for both precipitation and temperature
plt.legend([bars, line], ['Precipitation (mm)', 'Temperature (°C)'], loc='upper left', bbox_to_anchor=(0.75, 0.95), fontsize=10)

# Adjust layout
fig.tight_layout()

# Show the plot
plt.show()

                        Download                  

                 Watch the video tutorial   
___________________________________________




Next Post Previous Post
No Comment
Add Comment
comment url